Prabhu. S
Prabhu. S

Reputation: 1793

How to overcome spaces in the path while passing to system()?

In the following statement :

system("%TESTCASES_PATH%SIP\\test.bat");

the %TESTCASES_PATH% gets resolved to "C:\Program Files..." .

As such the result of calling the system is :

"'C:\Program' is not recognized as an internal or external command.."

'C:\Program' is thought as a executable..

How to overcome the above issue?

EDIT: Trying out what is proposed in the answers, I get to see the same behavior. The following is the actual code:

#include <stdio.h>
#include<conio.h>

int main()
{
    system("\"%TESTCASES_PATH%SIP\\Provisioning\\CrHomeDnOfficeCodeDestCodeBySoap\\CreateHomeDnOfficeCode.bat\"");
    //system("\"%TESTCASES_PATH%SIP\\tests.bat\"");
    getch();

    return 0;
}

Upvotes: 1

Views: 1056

Answers (3)

Andomar
Andomar

Reputation: 238186

Use double quotes to pass the entire path as the executable / batch file:

system("\"%TESTCASES_PATH%SIP\\test.bat\"");

Otherwise, what's after a space becomes the first command-line parameter.

EDIT: Perhaps on your setup, %TESTCASES_PATH% is not expanded by the system() function. On most systems, you can retrieve the value of an environment variable with getenv():

char cmd[FILENAME_MAX];
snprintf(cmd, FILENAME_MAX, "\"%s\\test.bat\"", 
    getenv("TESTCASES_PATH"));
system(cmd);

Upvotes: 8

Johan Buret
Johan Buret

Reputation: 2634

With one caveat to both solutions : test them with a string that contains NO space too.

It might fail on some windows shells.

Upvotes: 1

Frank Bollack
Frank Bollack

Reputation: 25186

What about:

system("\"%TESTCASES_PATH%SIP\\test.bat\"");

The additional double quotes in the string allow to pass file names with white space to the system call.

Upvotes: 3

Related Questions