【问题标题】:Passing quotes in quotes to cmd.exe将引号中的引号传递给 cmd.exe
【发布时间】:2011-01-20 15:40:00
【问题描述】:

我需要运行 appcmd.exe 但运行 cmd.exe 来查询 IIS 网站,我还需要重定向输出。

命令应该是这样的:

cmd /c "c:\windows\system32\inetsrv\appcmd.exe list site MySite" > c:\output.txt  

这很好用,但是当我的路径中有空格时会遇到问题,在这种情况下我需要使用引号。理想情况下,我会这样做:

cmd /c ""c:\windows\system32\inetsrv\appcmd.exe" list site "MySite"" > "c:\output.txt"  

但这不起作用 - 有什么想法吗?

【问题讨论】:

    标签: parameters cmd quotes


    【解决方案1】:

    “cmd /c”显示的帮助屏幕显示以下信息:

    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
          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.
    

    使用第 2 点中提到的“旧”行为。像往常一样键入 /c 之后的所有内容,但将 /c 之后的所有内容用引号括起来。例如:

    "c:\windows\system32\inetsrv\appcmd.exe" list site "MySite" > "c:\output.txt"
    

    变成

    cmd /c ""c:\windows\system32\inetsrv\appcmd.exe" list site "MySite" > "c:\output.txt""
    

    【讨论】:

      【解决方案2】:
      cmd /c c:\windows\system32\inetsrv\appcmd.exe list site MySite > c:\output.txt
      

      【讨论】:

        【解决方案3】:

        这可能有效:

        cmd /c "'c:\windows\system32\inetsrv\appcmd.exe' list site 'MySite'" > "c:\output.txt"
        

        【讨论】:

          猜你喜欢
          • 2010-11-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-20
          • 2016-11-08
          • 2017-10-31
          相关资源
          最近更新 更多