System command with Two arguments

Hello,
Upto best of my knowledge, I know that system command takes string as input. This string can be of any thing, which works fine into command prompt. For example, we can call an executable using system command passing full path of executable as argument.

One thing I have observed is that system command does not work properly if argument string includes space into it. Solution for this is to use a double quote. For example, below works fine:
String strPathOfExe = “\”” + “C:\Program Files\Myexe.exe” + “\””;
System(strPathOfExe);

I am stuck in the case of executable asking for two arguments [One argument as specific word “YES” or “NO” and second argument as file path having space into it]. I have tried below but no success:
String strPathOfExe = “\”” + “C:\Program Files\Myexe.exe” + “ ” + “NO” + “ ” + “C:\Program Files\MyFile.xml” + “\””;
System(strPathOfExe);

Can anyone help me on this? I hope I have conveyed my problem statement. Feel free to ask for more details or further explanation if required.
Thanks in advance!
Regards
Ketan
“C:/Program Files/MyFile.xml” would be parsed as two separate command line arguments: “C:/Program" and "Files/MyFile.xml".

Quote it: "C:/\"Program Files/MyFile.xml\""

Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <cstdlib>
#include <string>

int main()
{
    const char quote = '"' ;
    const char space = ' ' ;
    
    const std::string root_path = "C:/" ; // favour using '/' as the directory separator (back-slash requires escape)

    const std::string executable_name = "Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/dumpbin.exe" ;
    const std::string program = root_path + quote + executable_name + quote ;
    std::cout << "program: " << program << '\n' ;
    
    const std::string arg1 = "-dependents" ;
    std::cout << "   arg1: " << arg1 << '\n' ;
    
    const std::string arg_file_name = "Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/cl.exe" ;
    const std::string arg2 = root_path + quote + arg_file_name + quote ;
    std::cout << "   arg2: " << arg2 << '\n' ;

   
    const std::string cmd = program + space + arg1 + space + arg2 ;
    std::cout << "command: " << cmd << "\n---------------\n\n" << std::flush ;    
    
    std::system( cmd.c_str() ) ;
}

http://rextester.com/NKP58090
Hello,

Thanks for this useful response.

I would like to know whether system command does behave same as command line or not.

I got it that “C:/Program Files/MyFile.xml” would be required to use it like "C:/\"Program Files/MyFile.xml\"".

What need to be done if file path is like below:
“C:/Program Files/Test Folder Today/ My Test/MyFile.xml”


However, is it worth to use short path instead of this path? I could not find anything related to this for 64 bit.

Thanks and Regards
Ketan
> I would like to know whether system command does behave same as command line or not.

Yes, same as the host environment's standard command processor.
For instance, in Unix, the command is passed to /bin/sh.


> is it worth to use short path instead of this path?

It is better not to rely on the relative path. If the root path is configurable, compose a fully qualified path from the root path and the relative path (as in the example above). Often, the root path for the program is specified as an environment variable or as an entry in a run control file.
Topic archived. No new replies allowed.