【问题标题】:How do you strip quotes out of an ECHO'ed string in a Windows batch file?如何从 Windows 批处理文件中的 ECHO 字符串中去除引号?
【发布时间】:2010-10-22 17:21:42
【问题描述】:

我正在创建一个 Windows 批处理文件,但我必须 ECHO 一个大的复杂字符串,所以我必须在两端加上双引号。问题是引号也被回显到我正在写入的文件中。你如何回显这样的字符串并去掉引号?

更新:

我在过去的两天里一直在努力,终于能够将一些东西拼凑在一起。理查德的回答可以去掉引号,但即使我将 ECHO 放入子例程并直接输出字符串,Windows 仍然挂断在字符串中的字符上。我会接受理查德的回答,因为它回答了所提出的问题。

我最终使用了 Greg 的 sed 解决方案,但由于 sed/windows 的错误/功能而不得不对其进行修改(它没有提供文档并没有帮助)。在 Windows 中使用 sed 有一些注意事项:你必须使用双引号而不是单引号,你不能直接转义字符串中的双引号,你必须结束引用字符串,使用 ^ (so ^" ) 然后 beqin 引用下一节。此外,有人指出,如果您将输入通过管道传输到 sed,则字符串中存在管道错误(我没有得到验证,因为在我的最终解决方案中,我刚刚发现一种不在字符串中间包含所有引号的方法,并且只是删除了所有引号,我永远无法自行删除 endquote。)感谢所有帮助。

【问题讨论】:

    标签: windows batch-file sed quotes echo


    【解决方案1】:

    蛮力法:

    echo "foo <3 bar" | sed -e 's/\(^"\|"$\)//g'
    

    当然,这需要找到合适的 Win32 版本的sed

    【讨论】:

      【解决方案2】:

      http://unxutils.sourceforge.net/ 是一系列 GNU 实用程序的原生 win32 端口,包括 sed、gawk、grep 和 wget。 (抱歉,我没有足够的代表来发表评论!)

      【讨论】:

      • 好主意,在大多数情况下,我会推荐 unxutils 而不是 cygwin。
      【解决方案3】:

      这会将"C:\Program Files\somefile.txt" 变成C:\Program Files\somefile.txt 同时仍然保留Height=5'6"Symbols="!@#等案例

      :DeQuote
      
      SET _DeQuoteVar=%1
      CALL SET _DeQuoteString=%%!_DeQuoteVar!%%
      IF [!_DeQuoteString:~0^,1!]==[^"] (
      IF [!_DeQuoteString:~-1!]==[^"] (
      SET _DeQuoteString=!_DeQuoteString:~1,-1!
      ) ELSE (GOTO :EOF)
      ) ELSE (GOTO :EOF)
      SET !_DeQuoteVar!=!_DeQuoteString!
      SET _DeQuoteVar=
      SET _DeQuoteString=
      GOTO :EOF
      

      例子

      SetLocal EnableDelayedExpansion
      set _MyVariable = "C:\Program Files\ss64\"
      CALL :dequote _MyVariable
      echo %_MyVariable%
      

      【讨论】:

      • 这可以通过使用 setlocal 和 endlocal 而不是取消引用两个局部变量来简化。不过,我更喜欢我的解决方案。
      【解决方案4】:

      上述答案(以 :DeQuote 开头)假设延迟的环境变量扩展设置为开启。从 cmd /?:

      默认情况下不启用延迟环境变量扩展。你 可以启用或禁用延迟的环境变量扩展 使用 /V:ON 或 /V:OFF 开关特别调用 CMD.EXE。你 可以为所有 CMD.EXE 调用启用或禁用完成 机器和/或用户登录会话,通过设置其中一个或两个 使用 REGEDT32.EXE 在注册表中跟踪 REG_DWORD 值:

      HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\DelayedExpansion
      
          and/or
      
      HKEY_CURRENT_USER\Software\Microsoft\Command Processor\DelayedExpansion
      

      到 0x1 或 0x0。用户特定设置优先于 机器设置。命令行开关优先于 注册表设置。

      如果启用了延迟环境变量扩展,则感叹号 字符可用于替换环境变量的值 在执行时。

      【讨论】:

      • '上述'这里指的是JRL的答案。
      • 对于单个批次,您可能应该使用 setlocal enabledelayedexpansion。无需在注册表中乱搞或告诉用户如何调用 cmd。
      • 感谢“setlocal”提示,不知道那个提示。
      【解决方案5】:

      call 命令内置了这个功能。引用 call 的帮助:

       Substitution of batch parameters (%n) has been enhanced.  You can
       now use the following optional syntax:
      
       %~1         - expands %1 removing any surrounding quotes (")
      

      这是一个原始示例:

      @echo off
      setlocal
      set mystring="this is some quoted text"
      echo mystring=%mystring%
      call :dequote %mystring%
      echo ret=%ret%
      endlocal
      goto :eof
      
      :dequote
      setlocal
      rem The tilde in the next line is the really important bit.
      set thestring=%~1
      endlocal&set ret=%thestring%
      goto :eof
      

      输出:

      C:\>dequote
      mystring="this is some quoted text"
      ret=this is some quoted text
      

      我应该将“环境变量隧道”技术 (endlocal&set ret=%thestring%) 归功于 Tim Hill,'Windows NT Shell Scripting'。这是我发现的唯一一本涉及任何深度的批处理文件的书。

      【讨论】:

      • 太棒了!提供参数时是否有类似的简单方法来去除引号?对于我们只能控制调用方而不能控制处理参数的脚本的情况?
      • 在调用之前将调用包装在您自己的例程中进行剥离(如上所述)就足够了吗?
      • 有趣的是,这对我不起作用。 set thestring=%~1 产生“命令的语法不正确。”,即使我使用与 Richard 显示的相同的语法,尽管在“调用 /?”中看到相同的文本。部分。
      • 问题出在我的文字中。它是 xml,其中包含“”。所以理查德的解决方案确实有效,我只需要找出另一种处理该文本的方法。
      • 这是一个棘手的问题,@Jim2B,您也许可以在 进入批处理领域之前用 ^ 转义它。这取决于您的上下文。
      【解决方案6】:

      以下批处理文件启动一系列程序,每个程序都有延迟。

      问题是为每个程序传递带有参数的命令行。这需要在程序参数周围加上引号,在调用时将其删除。这说明了批处理文件处理中的一些技术。

      查看本地子例程 :mystart 以了解如何传入引号中的参数,并删除引号。

      @echo off
      
      rem http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/if.mspx?mfr=true
      
      rem Start programs with delay
      
      rem  Wait n seconds
      rem  n number retries to communicate with the IP address
      rem  1000 milliseconds between the retries
      rem  127.0.0.1 is the LocalHost
      rem  start /b (silent)  /min (minimized) /belownormal (lower priority)
      rem  /normal provides a no-op switch to hold the place of argument 1
      
      rem  start  /normal "Opinions"  %SystemRoot%\explorer.exe /e,d:\agar\jobs\opinion
      rem  ping 127.0.0.1 -n 8 -w 1000 > nul
      
      rem   Remove quotes in Batch
      rem     http://ss64.com/nt/syntax-dequote.html
      rem   String manipulation in Batch
      rem     http://www.dostips.com/DtTipsStringManipulation.php
      rem   ^ line continuation
      rem   
      rem   set p="One Two"      p has the exact value  "One Two" including the quotes           
      rem   set p=%p:~1,-1%      Removes the first and last characters
      rem   set p=%p:"=%         Removes all double-quotes
      rem   set p=%p:cat=mouse%  Replaces cat with mouse
      
      rem  ping 127.0.0.1 -n 12 -w 1000 > nul
      rem        1       2            3                                                         4
      
      @echo on
      call :mystart /b/min  "Opinions"   "%SystemRoot%\explorer.exe  /e,d:\agar\jobs\opinion"   8  
      @echo on
      call :mystart /b/min  "Notepad++"  D:\Prog_D\Notepad++\notepad++.exe  14
      @echo on
      call :mystart /normal "Firefox"    D:\Prog_D\Firefox\firefox.exe      20
      @rem call :mystart /b/min "ProcessExplorer"  D:\Prog_D\AntiVirus\SysInternals\procexp.exe  8
      @echo on
      call :mystart /b/min/belownormal "Outlook" D:\Prog_D\MSOffice\OFFICE11\outlook.exe  2
      @echo off
      goto:eof
      
      :mystart
      @echo off
       rem  %3 is "program-path  arguments" with the quotes. We remove the quotes
       rem  %4 is seconds to wait after starting that program
       set p=%3
       set p=%p:"=%
       start  %1  %2  %p% 
       ping 127.0.0.1 -n %4 -w 1000 > nul
       goto:eof
      

      【讨论】:

        【解决方案7】:

        您可以使用%var:x=y% 构造,将所有x 替换为y

        看看这个例子它能做什么:

        set I="Text in quotes"
        rem next line replaces " with blanks
        set J=%I:"=%
        echo original %I%
        rem next line replaces the string 'in' with the string 'without' 
        echo stripped %J:in=without%
        

        【讨论】:

          【解决方案8】:

          使用 FOR 命令去除周围的引号是我发现的最有效的方法。在紧凑的形式(示例 2)中,它是单行的。

          示例 1:5 行(已注释)解决方案。

          REM Set your string
          SET STR=" <output file>    (Optional) If specified this is the name of your edited file"
          
          REM Echo your string into the FOR loop
          FOR /F "usebackq tokens=*" %%A IN (`ECHO %STR%`) DO (
              REM Use the "~" syntax modifier to strip the surrounding quotation marks
              ECHO %%~A
          )
          

          示例 2:1 线现实世界示例。

          SET STR=" <output file>    (Optional) If specified this is the name of your edited file"
          
          FOR /F "usebackq tokens=*" %%A IN (`ECHO %STR%`) DO @ECHO %%~A
          

          有趣的是,内部回显忽略了重定向字符“”。
          如果您执行ECHO asdfsd&gt;asdfasd,您将写入文件而不是标准输出。

          希望这会有所帮助:)

          编辑:

          我想了想,意识到有一种更简单(而且不那么老套)的方法来完成同样的事情。像这样使用增强的变量替换/扩展(参见HELP SET):

          SET STR=" <output file>    (Optional) If specified this is the name of your edited file"
          
          ECHO %STR:~1,-1%
          

          这将打印除第一个和最后一个字符(您的引号)之外的所有字符。我也建议使用SETLOCAL ENABLEDELAYEDEXPANSION。如果您需要找出引号在字符串中的位置,您可以使用 FINDSTR 来获取字符 #s。

          【讨论】:

          • 我不认为这适用于像"This &amp; That"这样的STR
          【解决方案9】:

          我知道它实际上并不适合作者,但如果您需要向文件发送一些不带引号的文本 - 下面的解决方案适合我。您不需要在 echo 命令中使用引号,只需将完整的命令用括号括起来即可。

          (
              echo first very long line
              echo second very long line with %lots% %of% %values%
          ) >"%filename%"
          

          【讨论】:

            【解决方案10】:

            以下方法可用于打印不带引号的字符串:

            echo|set /p="<h1>Hello</h1>"
            

            将此字符串推入文件:

            echo|set /p="<h1>Hello</h1>" > test.txt
            

            将此字符串推入文件并附加 CR/LF:

            echo|(set /p="<h1>Hello</h1>" & echo.) > test.txt`
            

            检查:

            type test.txt
            

            【讨论】:

              【解决方案11】:

              要从集合变量中删除所有引号,您需要延迟变量扩展来安全地扩展变量并对其进行处理。使用百分号进行扩展(即%VAR%%1)本质上是不安全的(它们容易受到命令注入;read this for details)。

              SETLOCAL EnableDelayedExpansion
              SET VAR=A ^"quoted^" text.
              REM This strips all quotes from VAR:
              ECHO !VAR:^"=!
              REM Really that's it.
              

              要从文本文件或命令输出中去除引号,事情会变得复杂,因为使用延迟扩展时,文本文档中的 !VAR! 之类的字符串将被扩展(在 FOR /F 中的 %%i 扩展内)不应该。 (这是另一个漏洞——信息泄露——其他地方没有记录。)

              为了安全地解析文档,需要在启用延迟扩展和禁用的环境之间进行切换。

              REM Suppose we fetch the text from text.txt
              
              SETLOCAL DisableDelayedExpansion
              REM The FOR options here employs a trick to disable both "delims"
              REM characters (i.e. field separators) and "eol" character (i.e. comment
              REM character).
              FOR /F delims^=^ eol^= %%L IN (text.txt) DO (
              
                  REM This expansion is safe because cmd.exe expands %%L after quotes
                  REM parsing as long as DelayedExpansion is Disabled. Even when %%L
                  REM can contain quotes, carets and exclamation marks.
                  SET "line=%%L"
              
                  CALL :strip_quotes
                  REM Print out the result. (We can't use !line! here without delayed
                  REM expansion, so do so in a subroutine.)
                  CALL :print_line
              )
              ENDLOCAL
              GOTO :EOF
              
              REM Reads !line! variable and strips quotes from it.
              :strip_quotes
                  SETLOCAL EnableDelayedExpansion
                  SET line=!line:^"=!
              
                  REM Make the variable out of SETLOCAL
                  REM I'm expecting you know how this works:
                  REM (You may use ampersand instead:
                  REM `ENDLOCAL & SET "line=%line%"`
                  REM I just present another way that works.)
                  (
                  ENDLOCAL
                  SET "line=%line%"
                  )
              GOTO :EOF
              
              :print_line
                  SETLOCAL EnableDelayedExpansion
                  ECHO !line!
                  ENDLOCAL
              GOTO :EOF
              

              上面代码中的delims^=^ eol^= 可能需要解释一下: 这有效地禁用了“delims”字符(即字段分隔符)和“eol”字符(即注释字符)。没有它,“delims”将默认为制表符和空格,“eol”默认为分号。

              • eol= 标记始终读取等号之后的下一个字符。要禁用它,此标记必须位于选项字符串的末尾,这样“eol”就不能使用任何字符,从而有效地禁用它。如果选项字符串被引用,它可能使用引号(“)作为“eol”,所以我们不能引用选项字符串。
              • delims= 选项在不是选项字符串中的最后一个选项时,将以空格结尾。 (要在“delims”中包含空格,它必须是 FOR /F 选项的最后一个选项。)所以delims= 后跟一个空格,然后另一个选项禁用“delims”。

              【讨论】:

                【解决方案12】:

                Daniel Budzyński 的反应非常出色。它甚至在输出中有特殊字符的情况下也有效。例如:

                C:\> for /f "usebackq tokens=2 delims=:" %i in (`%comspec%\..\ping -n 1 -w 200 10.200.1.1 ^| \
                     findstr /c:"TTL="`) do echo|set /p="%i"
                
                bytes=32 time<1ms TTL=255
                

                如果您尝试不带引号的简单回显,由于变量中的“

                C:\> set "output=bytes=32 time<1ms TTL=255"
                C:\> echo %output%
                The system cannot find the file specified.
                
                C:\> echo|set /p="%output%"
                bytes=32 time<1ms TTL=255
                

                【讨论】:

                • 嗨,欢迎来到 SO —— 这在评论中比在答案中更合适(尽管作为新贡献者,您可能没有编写 cmets 的权限)。无论如何,请参阅 stackoverflow.com/help/how-to-answer 以获取答案。
                【解决方案13】:

                这对我有用:

                SET "SOMETHING=Complex (String) (of stuff!)"
                echo !SOMETHING! >> file.txt
                

                【讨论】:

                • 首先添加SETLOCAL ENABLEDELAYEDEXPANSION 后,此解决方案适用于我(在Windows 10 cmd.exe 上)
                • 我应该提到这是必需的,因为 !VARIABLE!而不是 %VARIABLE%
                猜你喜欢
                • 2014-03-13
                • 2019-05-06
                • 1970-01-01
                • 2014-03-03
                • 2011-01-15
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2011-07-13
                相关资源
                最近更新 更多