没有什么特别的原因,我目前正在开发一个使用system()提取..zip/..rar文件的程序。
我现在已经安装了WinRar,因为winrar.exe能够同时处理.zip和.rar文件。
int main()
{
vector<wstring> files;
if (ListFiles(L"folder", L"*", files))
{
string program = "\"C:\\Program Files\\WinRAR\\winrar.exe\"";
string args = "x -y";
string type = "*.*";
TCHAR dir[MAX_PATH];
GetCurrentDirectory(MAX_PATH, dir);
wstring current_directory(wstring(L"\"") + dir + wstring(L"\\"));
for (const auto& f : files)
{
if (wcscmp(PathFindExtension(f.c_str()), L".rar") == 0 ||
wcscmp(PathFindExtension(f.c_str()), L".zip") == 0)
{
string file = ws2s(f.c_str());
string output = "\"c:\\Users\\my name\\Desktop\\output\"";
string command = program + " " + args + " " + ws2s(current_directory) + file + "\"" + " " + type + " " + output;
cout << command << endl;
if (system(command.c_str()) != 0)
return GetLastError();
}
}
}
return 0;
}因为我使用的是命令行,而且不希望空格成为问题,所以我尽可能用引号包装:
-- `"C:/users/username/program files (x86)/"` -- -- `"folder/zipped folder.zip" vs folder/"zipped folder.zip"` --在构建了包含在command中的完整命令之后,我将其打印到屏幕上,以便编辑->标记:
"C:\Program Files\WinRAR\winrar.exe" x -y "C:\Users\my name\Documents\Visual Studio 2013\Projects\extractor\folder\unzip.zip" *.* "c:\Users\my name\Desktop\output"
然而,'C:\Program' is not recognized as an internal or external command, operable program or batch file.是我在system(command)调用之后遇到的。
如果我将完全相同的命令复制并粘贴到Start->Command提示符中,它就会像梦一样工作。
http://comptb.cects.com/using-the-winrar-command-line-tools-in-windows/
https://www.feralhosting.com/faq/view?question=36
是否有不同的方式来调用system()调用?
如果没有,那么如何使用命令行参数?
我宁愿完全避免使用Boost::或第三方库。
谢谢!
发布于 2015-03-20 19:48:56
这可能是因为在引用参数时使用了命令提示符的古怪行为。无论何时调用system("\"arg1\" \"arg2\""),都等同于调用:
cmd.exe /c "arg1" "arg2"由于链接post中描述的奇怪行为,命令提示符无法正确解释这一点。还需要一套额外的引号:
cmd.exe /c ""arg1" "arg2""对于调用可执行文件,CreateProcess提供了一种替代方法,使您能够更好地控制进程。您仍然需要引用参数,但是作为命令提示符的规则更简单一些不再妨碍您。
https://stackoverflow.com/questions/29161340
复制相似问题