带有空格的system()命令

时间:2018-12-05 14:21:01

标签: c++ powershell

我正在尝试通过system()命令执行PowerShell命令,但是由于字符串中的空格而遇到异常,我尝试了一些操作,但仍然遇到了相同的异常。

代码:

system("powershell.exe -command Invoke-WebRequest http://example.com/myEXE.exe -OutFile C:\\Program Files\\myEXE.exe");

这是我得到的例外:

Invoke-WebRequest : A positional parameter cannot be found that accepts
argument 'Files\myEXE.exe'.
At line:1 char:1
+ Invoke-WebRequest http://example.com/myEXE.exe -OutFile C:\Program File ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

1 个答案:

答案 0 :(得分:0)

我认为这应该可行:

#include <iostream>
#include <bits/stdc++.h>
int main(){
    std::cout << "trying system command" << std::endl;
    system("powershell.exe -command Invoke-WebRequest https://make.sure.valid.url.to.test.otherwise.will.fail.anyways -OutFile 'C:/Program` Files/myEXE.exe'");
}

`字符应允许您正确地逃脱空间。

相关问题