简体   繁体   中英

Function to execute batch file C++

I want to execute a bath 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\""

I assume you want something like that:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM