如何使用 boost::p rocess::child 处理路径中的空格
How to deal with spaces in path using boost::process::child?
我需要执行一个Windows Batch脚本。根据公司政策,我必须使用boost::process::child
。Windows 批处理脚本的路径包含空格(例如 C:Foo Barbatch.bat
(。
我正在使用以下代码:
namespace bp = boost::process;
error_code errorCode;
bp::ipstream errorStream;
auto child = bp::child("C:\Foo Bar\batch.bat",
errorCode,
bp::std_out > bp::null, // ignore standard output
bp::std_err > errorStream, // capture standard error
bp::windows::hide, // hide window
bp::shell); // use shell
vector<string> errorData;
string errorLine;
while (child.running() && getline(errorStream, errorLine) && !errorLine.empty())
{
errorData.push_back(errorLine);
}
child.wait();
问题是系统(boost::p rocess(找不到路径。错误消息如下所示:
"C:\Foo"不被识别为内部或外部命令、可操作程序或批处理文件。
我还尝试了以下掩蔽变体:
-
C:\Foo Bar\batch.bat
-
C:\Foo Bar\batch.bat
-
"C:\Foo Bar\batch.bat"
-
C:\Foo~1\batch.bat
如何正确屏蔽空格,以便child()
正确查找/执行批处理脚本?
将
"C:\Foo Bar\batch.bat"
包装成 boost::filesystem::path()
,以便它为您引用字符串:
auto child = bp::child(boost::filesystem::path("C:\Foo Bar\batch.bat"),
我建议遵循@Maxim的答案。
或者,使用反斜杠转义空格:
"C:\Foo\ Bar\batch.bat"
bp::child c(
bp::exe(boost::filesystem::path("C:\Foo Bar\batch.bat").c_str()),
bp::cmd("options here"),
bp::environment(env),
bp::std_in.close(),
bp::std_out > os,
bp::std_err > es,
bp::start_dir("workdir here")
);
假设这变成了 Windows CreateProcess 调用,那么应用程序的路径必须用双引号引起来,以便在路径中允许空格。事实上,建议路径始终用双引号括起来。
因此,您将使用:
auto child = bp::child(""C:\Foo Bar\batch.bat"",
我不知道 Boost::child 是否真的会允许它工作。
相关文章:
- 2D数组来自文本输入,中间有空格
- 如何将更多文件夹添加到c++include路径
- 带有特殊路径部分的"std::filesystem::weakly_canonical"失败
- C++A*算法并不总是在路径中具有目标节点
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- 如何在C++中读取空格分隔的输入 当我们不知道输入的数量时
- 从函数角度看ID到文件路径的内部与外部映射
- 如何在c++中使用system()来运行包含空格的python脚本
- 使用路径和空格的系统调用
- 崇高文本 3 - 在终端中编译并运行 C++ 程序(路径包含空格)
- 如何使用 boost::p rocess::child 处理路径中的空格
- 带有空格的C++系统()命令路径
- 使用包含给定空格的路径启动程序
- 当路径中有空格时,从与Qt应用程序相同的目录启动可执行文件
- QDesktopServices::openUrl() 如果路径包含空格("apt-get upgrade" 后)失败
- C++ 执行路径中带有空格的.exe
- 如何将带有空格作为参数的路径添加到CreateProcess批处理文件中
- 使用路径中的空格从C++执行批处理文件
- 如何在DevC++中运行程序,并在文件路径中使用空格
- ShellExecute:动词"runas"不适用于路径中带有空格的批处理文件