Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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\""
like image 958
user1334858 Avatar asked Aug 15 '26 19:08

user1334858


1 Answers

I assume you want something like that:

void executeBatch(char* BatchFile){
string cmd(BatchFile)
string expandCmd = string("\"") + cmd + string("\"");
system(expandCmd.c_str());
}
like image 154
alexbuisson Avatar answered Aug 17 '26 10:08

alexbuisson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!