如何防止Rust的std :: process:Command将.exe的相对路径插入参数中?

时间:2019-01-11 23:08:47

标签: windows cmd rust

我正在使用Rust的std:process:Command在Windows中调用robocopy。不幸的是,似乎在执行robocopy的某个位置,.exe的相对路径已插入每个目录之前。

另外,尽管我只测试了不依赖任何目录的调用,但类似net use的简单调用也可以使用相同的方法。

编辑:更新了测试文件夹,使其名称中带有空格。

编辑2:代码已使用解决方案进行了更新。解决方案是直接调用robocopy,并分别调用所有内容中的.arg()。旧尝试仍在评论中。

代替发送

cmd.exe /c robocopy "C:\Test\Test 1" "C:\Test\Test 2" /MIR /copy:DAT /MT:32 /Z /R:2 /W:03 /v /LOG:"C:\Test\Test 3\logfile.log"

并且成功运行,输出有很多错误:

ERROR 123 (0x0000007B) Opening Log File C:\relative\path\where\exe\is\located\"C:\Test\Test 3\logfile.log"
The filename, directory name, or volume label syntax is incorrect.

-------------------------------------------------------------------------------
   ROBOCOPY     ::     Robust File Copy for Windows
-------------------------------------------------------------------------------

   Started : Tuesday, January 8, 2019 7:35:41 AM
    Source - C:\relative\path\where\exe\is\located\"C:\Test\Test 1"\
      Dest - C:\relative\path\where\exe\is\located\"C:\Test\Test 2"\

     Files :
   Options : /V /S /E /DCOPY:DA /COPY:DAT /PURGE /MIR /Z /MT:32 /R:2 /W:3

------------------------------------------------------------------------------

ERROR : Invalid Parameter #10 : "/LOG:"C:\Test\Test 3\logfile.log""

使用以下方法会发生此问题:

  • Rust版本1.3.1

  • Windows 10

对我来说导致该问题的代码如下所示。要使用它,您需要在 C:\ 中放置一个 Test 文件夹(或将其更改为任意位置),并在该目录中放置一个 Test 1 < / strong>文件夹,其中包含一些 Testfile.txt 来帮助显示复制,并且还有一个 Test 3 文件夹用于保存日志(不确定日志是否可以制作自己的文件夹-为了安全起见,请为其预先制作一个!)。基本上你想要:

C:\Test\Test 1\Testfile.txt (name or type of file doesn't matter)
C:\Test\Test 3\

robocopy应该运行并创建一个 C:\ Test \ Test 2 文件夹,其中包含 Testfile.txt ,并放置一个 logfile.log (位于C:\ Test \ Test 3 中的详细信息)。最后,目录应如下所示:

C:\Test\Test 1\Testfile.txt
C:\Test\Test 2\Testfile.txt
C:\Test\Test 3\logfile.log

代码如下:

//-----Import Built-in Libraries (not called crates)-----
use std::process::Command; //use cmd.exe

fn main()
{
    let commandOpt1 = "/MIR"; //mirror directories
    let commandOpt2 = "/copy:DAT"; //copy attributes
    let commandOpt3 = "/MT:32"; //use 32 I/O threads (low CPU still, but better bandwidth utilization)
    let commandOpt4 = "/Z"; //idr
    let commandOpt5 = "/R:2"; //Retry twice
    let commandOpt6 = "/W:03"; //Wait 3 sec between tries
    let commandOpt7 = "/v"; //verbose logging
    let commandLogStr = "/LOG:\"C:\\Test\\Test 3\\logfile.log\""; //record where the log file goes
    let commandLogNoParenthStr = "/LOG:C:\\Test\\Test 3\\logfile.log"; //record where the log file goes
    let command = format!("robocopy \"C:\\Test\\Test 1\" \"C:\\Test\\Test 2\" {} {} {} {} {} {} {} {}",
    commandOpt1,commandOpt2,commandOpt3,commandOpt4,commandOpt5,commandOpt6,
    commandOpt7,commandLogStr); //build the command
    let commandStr: &str = &*command; //these two types of strings are
    println!("TEST-Command for robocopy:{}",command);

    //let m = Command::new("cmd.exe").arg("/c").arg(commandStr).output().unwrap(); //run cmd.exe net use
    /*let m = Command::new("cmd.exe")
        .arg("/c")
        .arg("robocopy")
        .arg("\"C:\\Test\\Test 1\"")
        .arg("\"C:\\Test\\Test 2\"")
        .arg(commandOpt1)
        .arg(commandOpt2)
        .arg(commandOpt3)
        .arg(commandOpt4)
        .arg(commandOpt5)
        .arg(commandOpt6)
        .arg(commandOpt7)
        .arg(commandLogStr)
        .output().unwrap(); //run cmd.exe net use */
    let m = Command::new("robocopy")
        .arg("C:\\Test\\Test 1")
        .arg("C:\\Test\\Test 2")
        .arg(commandOpt1)
        .arg(commandOpt2)
        .arg(commandOpt3)
        .arg(commandOpt4)
        .arg(commandOpt5)
        .arg(commandOpt6)
        .arg(commandOpt7)
        .arg(commandLogNoParenthStr )
        .output().unwrap(); //run cmd.exe net use */
    let mOutput = String::from_utf8_lossy(&m.stdout); //get the output
    println!("TEST-cmd output: {}",mOutput);
    println!("TEST-cmd status: {}", m.status.success()); //reports success (true or false) of command
    println!("TEST-cmd stderr: {}", String::from_utf8_lossy(&m.stderr)); //reports error words of command
    let _ = Command::new("cmd.exe").arg("/c").arg("pause").status(); // wait for user input
}

代码提供了两种选择,一种是在另外一个robocopy中调用.arg(),另一个是将.arg()分开。对我来说,它们给出相同的结果-但是选项很好!

此外,请注意,我将所有内容都以Command的形式发送到&str,但这似乎不是必需的-Strings也可以是.arg()

1 个答案:

答案 0 :(得分:1)

我附近没有Windows计算机,但是我很确定问题出在引号字符"中。

CMD.exe中的转义规则非常怪异:有时您需要引号,有时您不需要,有时则不使用引号。

例如,使用以下选项选择日志文件(删除Rust逃逸符):/LOG:"C:\Test\Test3\logfile.log"。这是一个以引号开头和结尾的文件名。而且,由于它不是以驱动器号或\开头,因此OS认为:当然,这是相对路径,让我们寻找C:\...\"C:\Test..."

解决方案很简单:只需删除所有引号即可。

我不明白的是您为什么要打电话给cmd.exe /c而不是直接打电话给robocopy。命令行中的引号用于指导命令行解析器,并使用空格等正确管理文件名。但是,如果直接执行Command::new()中的robocopy,则无需引用,因为参数已经分开传递。