system() call with paths containing spaces

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'");




Any ideas? Thanks
Last edited on
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\"\"   ");



Hopefully this will work 4 U..
Last edited on
1
2
// this works
system("\"\"C:\\Program Files\\Batch Image Commander\\imagecom.exe\" \"H:\\My Music\\iTunes\\Tommy Bolin\\Private Eyes\\Folder.jpg\"\" echo resizemethod=downsizebypixel width=600 height=600 newname=thumbnail");


Line 11 from my original code snippet does not work.

Thank you for your replies!
But to make it useful -

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	for(std::vector<std::string>::iterator iterJPG = vecJPGFiles.begin();iterJPG != vecJPGFiles.end();++iterJPG)
	{
		*iterJPG = replacein(*iterJPG, "\\", "\\\\");
		strFileName = "\"\"C:\\\\Program Files\\\\Batch Image Commander\\\\imagecom.exe\"\" \\\"";
		strFileName += *iterJPG;
		strFileName += "\\\"\\\"";
		strFileName += " echo resizemethod=downsizebypixel width=600 height=600 newname=thumbnail";
		std::cout << strFileName << std::endl;
		system("\"\"C:\\Program Files\\Batch Image Commander\\imagecom.exe\" \"H:\\My Music\\iTunes\\Tommy Bolin\\Private Eyes\\Folder.jpg\"\" echo resizemethod=downsizebypixel width=600 height=600 newname=thumbnail");
		//system("\"\"C:\\Program Files\\Batch Image Commander\\imagecom.exe\" \"*iterAvi\"\" echo resizemethod=downsizebypixel width=600 height=600 newname=thumbnail");
		//system("\"\"C:\\Program Files\\Batch Image Commander\\imagecom.exe\" *iterAvi\" echo resizemethod=downsizebypixel width=600 height=600 newname=thumbnail");
		//system("\"C:\\Program Files\\Batch Image Commander\\imagecom.exe\" strFileName.c_str() echo resizemethod=downsizebypixel width=600 height=600 newname=thumbnail");
		system(strFileName.c_str());
		break;
	}


produces following output

""C:\\Program Files\\Batch Image Commander\\imagecom.exe"" \"H:\\My Music\\iTunes\\Leela James\\A Change Is Gonna Come\\Folder.jpg\"\" echo resizemethod=downsizebypixel width=600 height=600 newname=thumbnail
Loading File List........
Number of Files to process : 1
Processing H:\My Music\iTunes\Tommy Bolin\Private Eyes\Folder.jpg
Resizing Image to 600 x 600
Saving .... H:\My Music\iTunes\Tommy Bolin\Private Eyes\thumbnail.jpg

The system cannot find the path specified.

So the first system call works and the second fails.

How do I get this to work with the variable string?
[I have tried many ways - the listed code is not the closest.]

thanks!

Last edited on
ok - finally got it, so I will post my solution.

1
2
3
4
5
6
7
8
9
	for(std::vector<std::string>::iterator iterJPG = vecJPGFiles.begin();iterJPG != vecJPGFiles.end();++iterJPG)
	{
		*iterJPG = replacein(*iterJPG, "\\", "\\\\");
		strFileName = "\"\"C:\\\\Program Files\\\\Batch Image Commander\\\\imagecom.exe\" ";
		strFileName += "\"" + *iterJPG + "\"\" echo resizemethod=downsizebypixel width=600 height=600 newname=thumbnail";
		std::cout << strFileName << std::endl;
		system(strFileName.c_str());
		break;
	}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
///
/// 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;
}


Topic archived. No new replies allowed.