tags:

views:

281

answers:

3

I'm trying to use 7-Zip to zip up a file via the system() function in C++ on a windows XP machine. I tried:

(formatted to be what system() would have received)

"C:\Program Files\7-Zip\7z.exe" a -tzip "bleh.zip" "addedFile.txt"

which spat the error

'C:\Program' is not recognized as an internal or external command, operable program or batch file.

I've tried several similar alternatives but have not yet found a solution.

I want to try to run it straight from its install directory so that as long as a user has 7-Zip installed it will be able to function. This is for an in house utility application.

EDIT: as requested these are the actual lines of code:

std::string systemString = "\"C:\\Program Files\\7-Zip\\7z.exe\" a -tzip \"" + outDir + projectName + ".zip" + "\" \"";
//...
std::string finalSystemString = systemString + *i + "\"";
system( finalSystemString.c_str() );

*i is an iterator to a particular file that is getting added.

+3  A: 

Have you tried escaping the spaces, i.e. "C:\Program\ Files\7-Zip\7z.exe"? That might work, although I don't know the specifics of system().

Ibrahim
+1, beat me to the punch.
Daniel
Same result, however that caused MSVS to throw this: warning C4129: ' ' : unrecognized character escape sequence :(
0xC0DEFACE
@0xC0DEFACE: Try "C:\\Program\\ Files\\7-Zip\\7z.exe"
Daniel
As expected that caused the \ to escape the \ resulting in "C:Program\ Files\7-Zip\7z.exe".damn
0xC0DEFACE
+3  A: 

it looks like something is stripping the quotes around the first argument. You could play around with extra quotes to try and fix this, or you can get the MS-DOS compatible short path name for 7z.exe with the Win32 API GetShortPathName

The short path will not have spaces in it, it will be something like "C:\PROGRA~1\7-ZIP\7Z.EXE"

John Knoeller
This might be a better solution than escaping it like I suggested, just because the double slashes are kind of awkward and the code might be clearer using the short path function.
Ibrahim
I pasted that straight into my app and it worked! Thanks! Great trick to know!
0xC0DEFACE
A: 

Another approach would be to use the CreateProcess function in the Windows API. It can deal with spaces in "C:\Program Files" according to its documentation.

Peter Jansson