1

我想执行一个批处理文件system(),文件的路径将传递给函数,所以它看起来像这样:

void executeBatch(char* BatchFile){
    system(BatchFile);
}

现在的问题是传入的路径将没有转义引号以忽略空格,例如用户将输入:

"C:\\Users\\500543\\Documents\\Batch File Project\\Testing.bat"

如何在传入的路径中添加转义引号?

所以我基本上改变了:

"C:\\Users\\500543\\Documents\\Batch File Project\\Testing.bat"

"\"C:\\Users\\500543\\Documents\\Batch File Project\\Testing.bat\""
4

3 回答 3

2

尝试

system("\"C:\\Users\\500543\\Documents\\Batch File Project\\Testing.bat\"");

至于您评论中的其他问题,您必须使用:

char* it = "\"C:\\Users\\500543\\Documents\\Batch File Project\\Testing.bat\"";

system(it);

然后。

至于您编辑的问题,因为您已将问题标记为使用,所以这里有一个 c++ 解决方案如何正确实现您的功能:

#include <sstream>

int executeBatch(const char* fullBatchFileName)
{
    std::ostringstream oss;

    oss << '\"' << fullBatchFileName << '\"';
    return system(oss.str().c_str());
}

现在不要把它变成一个 XY 问题!我认为您应该从这些示例中理解了原理:只需将批处理文件名包含在一对双引号字符 ( '\"') 中,shell 就可以正确解释它。也有可用的纯 c 库方法来实现这一点(请参阅 参考资料<cstring>),但如果您可以使用 c++ 标准库,我不推荐这些方法。

于 2013-07-11T15:41:56.450 回答
0

您需要转义引号:

     system("\"C:\\Users\\500543\\Documents\\Batch File Project\\Testing.bat\"");
于 2013-07-11T15:46:12.407 回答
0

尝试在命令行周围添加转义双引号,即

system("\"C:\\Users\\500543\\Documents\\Batch File Project\\Testing.bat\"");
于 2013-07-11T15:42:03.237 回答