Search code examples
c++batch-filecmd

Function to execute batch file C++


I want to execute a batch file using system() and the path to the file will be passed to the function so it will look like this:

void executeBatch(char* BatchFile){
    system(BatchFile);
}

Now the issue is that the path passed in will not have the escape quotes to ignore spaces for example the user would input:

"C:\\Users\\500543\\Documents\\Batch File Project\\Testing.bat"

How do I add escape quotes to the path passed in?

So I essentually change:

"C:\\Users\\500543\\Documents\\Batch File Project\\Testing.bat"

to

"\"C:\\Users\\500543\\Documents\\Batch File Project\\Testing.bat\""

Solution

  • I assume you want something like that:

    void executeBatch(char* BatchFile){
    string cmd(BatchFile)
    string expandCmd = string("\"") + cmd + string("\"");
    system(expandCmd.c_str());
    }