如何正确地将完整文件名传递给`cmd / c`?

时间:2014-03-19 19:26:02

标签: cmd

我正在尝试在CMD提示符中运行命令以从我的SVN(TortoiseSVN 1.7.8)获取信息。在cmd提示符中,我输入:

cmd /c C:\Program Files\TortoiseSVN\bin\svn.exe info C:\MyCode\CurrProj\Ry1

我明白了:

  

'C:\ Program'未被识别为内部或外部命令,   可操作程序或批处理文件。

这是有道理的,因为'C:\ Program'之后有一个空格。好的,如果我输入:

cmd /c "C:\Program Files\TortoiseSVN\bin\svn.exe" info C:\MyCode\CurrProj\Ry1

然后一切按预期工作。但是,如果位置(最后一个路径)包含空格:

cmd /c "C:\Program Files\TortoiseSVN\bin\svn.exe" info C:\MyCode\Curr Proj\Ry1

我明白了:

  

svn:E155007:'C:\ MyCode \ Curr'不是工作副本

所以这意味着SVN命令只运行并使用了部分代码位置。所以我也在这条道路上添加了引号:

cmd /c "C:\Program Files\TortoiseSVN\bin\svn.exe" info "C:\MyCode\Curr Proj\Ry1"

这让我回到了错误消息:

  

'C:\ Program'未被识别为内部或外部命令,   可操作程序或批处理文件。

什么??有人能解释为什么CMD提示没有正确解释这个问题吗?最后一个示例不应该使用“关闭”选项启动CMD提示并运行:

"C:\Program Files\TortoiseSVN\bin\svn.exe" info "C:\MyCode\Curr Proj\Ry1"

如果我跑: "C:\Program Files\TortoiseSVN\bin\svn.exe" info "C:\MyCode\Curr Proj\Ry1" 它本身就可以正常工作,但是这个命令将在一些C#代码中以编程方式创建的进程中运行:

System.Diagnostics.ProcessStartInfo procStartInfo =
                new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);

其中command = "C:\Program Files\TortoiseSVN\bin\svn.exe" info "C:\MyCode\Curr Proj\Ry1"

2 个答案:

答案 0 :(得分:2)

摘录自cmd /help

If /C or /K is specified, then the remainder of the command line after
the switch is processed as a command line, where the following logic is
used to process quote (") characters:

    1.  If all of the following conditions are met, then quote characters
        on the command line are preserved:

        - no /S switch
        - exactly two quote characters
        - no special characters between the two quote characters,
          where special is one of: &<>()@^|
        - there are one or more whitespace characters between the
          two quote characters
        - the string between the two quote characters is the name
          of an executable file.

    2.  Otherwise, old behavior is to see if the first character is
        a quote character and if so, strip the leading character and
        remove the last quote character on the command line, preserving
        any text after the last quote character.

答案 1 :(得分:0)

不要将你的参数与文件名

联系起来
using (Process p = new Process())
{
   p.StartInfo.FileName = "cmd.exe";
   p.StartInfo.Arguments = "/c \"c:\\Program Files\\..blah\"";
   p.Start();
}

您还需要使用转义字符

在路径周围添加引号
\" gives you "