I am having trouble performing a system call where the path to the executable and the paramter to the executable both are path names containing spaces. I am unable to get the quotes/escape characters correct. Depending on how I quote the line below, the executable is either not found or the executable thinks that the second path, which is intended as a single parameter, is multiple parameters because the spaces are visible in the command line.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// exe not found - C:\\Program is not an executable
system("\"C:\\Program Files\\Batch Image Commander\\imagecom.exe H:\\My Music\\iTunes\\Tommy Bolin\\Private Eyes\\Folder.jpg\"");
// exe found, multiple parameters
system("\"C:\\Program Files\\Batch Image Commander\\imagecom.exe\" H:\\My Music\\iTunes\\Tommy Bolin\\Private Eyes\\Folder.jpg");
// exe not found - C:\\Program is not an executable
system("\"C:\\Program Files\\Batch Image Commander\\imagecom.exe H:\\My Music\\iTunes\\Tommy Bolin\\Private Eyes\\Folder.jpg\"");
// exe not found - C:\\Program is not an executable
system("\"C:\\Program Files\\Batch Image Commander\\imagecom.exe\" \"H:\\My Music\\iTunes\\Tommy Bolin\\Private Eyes\\Folder.jpg\"");
// exe found, multiple parameters
system("\"C:\\Program Files\\Batch Image Commander\\imagecom.exe\" 'H:\\My Music\\iTunes\\Tommy Bolin\\Private Eyes\\Folder.jpg'");
Line 11 should work... After all, the executable must be enclosed in double quotes, and the argument must also be wrapped in double quotes. In line 11 in your code above, that requirement is satisfied.
Hi ..., Follow the following steps... Lets see whether ur prob gets solved or not. 1. Enclose the executable in a double quote. i.e.
\" C:\\Program Files\\Batch Image Commander\\imagecom.exe \" 2. Enclose the parameter in a double quote. i.e.
\" H:\\My Music\\iTunes\\Tommy Bolin\\Private Eyes\\Folder.jpg \"
3. Now Append these two and enclose in another quote i.e.
\" \"C:\\Program Files\\Batch Image Commander\\imagecom.exe\" \"H:\\My Music\\iTunes\\Tommy Bolin\\Private Eyes\\Folder.jpg\" \"
4. Now ur command is ready to be executed. Pass the same to system() putting the whole string in another Quote. i.e.
system(" \"\"C:\\Program Files\\Batch Image Commander\\imagecom.exe\" \"H:\\My Music\\iTunes\\Tommy Bolin\\Private Eyes\\Folder.jpg\"\" ");
////// Replace all occurences of a substring in a string with another/// string, in-place.////// @param s String to replace in. Will be modified./// @param sub Substring to replace./// @param other String to replace substring with.////// @return The string after replacements.inline std::string &
replacein(std::string &s, const std::string &sub,
const std::string &other)
{
//assert(!sub.empty());
size_t b = 0;
for (;;)
{
b = s.find(sub, b);
if (b == s.npos) break;
s.replace(b, sub.size(), other);
b += other.size();
}
return s;
}