How do you strip quotes out of an ECHO'ed string in a Windows batch file?

后端 未结 12 1898
萌比男神i
萌比男神i 2020-12-05 04:23

I have a Windows batch file I\'m creating, but I have to ECHO a large complex string, so I\'m having to put double quotes on either end. The problem is that the quotes are

12条回答
  •  孤街浪徒
    2020-12-05 05:10

    The call command has this functionality built in. To quote the help for call:

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

    Here is a primitive example:

    @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
    

    Output:

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

    I should credit the 'environment variable tunneling' technique (endlocal&set ret=%thestring%) to Tim Hill, 'Windows NT Shell Scripting'. This is the only book I have ever found that addresses batch files with any depth.

提交回复
热议问题