Removing double quotes from variables in batch file creates problems with CMD environment

前端 未结 11 575
春和景丽
春和景丽 2020-12-04 11:25

Can anybody help with effective and safe way of removing quotes from batch variables?

I have written a batch file which successfully imports a list of parameters %1,

11条回答
  •  渐次进展
    2020-12-04 11:58

    You have an extra double quote at the end, which is adding it back to the end of the string (after removing both quotes from the string).

    Input:

    set widget="a very useful item"
    set widget
    set widget=%widget:"=%
    set widget
    

    Output:

    widget="a very useful item"
    widget=a very useful item
    

    Note: To replace Double Quotes " with Single Quotes ' do the following:

    set widget=%widget:"='%
    

    Note: To replace the word "World" (not case sensitive) with BobB do the following:

    set widget="Hello World!"
    set widget=%widget:world=BobB%
    set widget
    

    Output:

    widget="Hello BobB!"
    

    As far as your initial question goes (save the following code to a batch file .cmd or .bat and run):

    @ECHO OFF
    ECHO %0
    SET BathFileAndPath=%~0
    ECHO %BathFileAndPath%
    ECHO "%BathFileAndPath%"
    ECHO %~0
    ECHO %0
    PAUSE
    

    Output:

    "C:\Users\Test\Documents\Batch Files\Remove Quotes.cmd"
    C:\Users\Test\Documents\Batch Files\Remove Quotes.cmd
    "C:\Users\Test\Documents\Batch Files\Remove Quotes.cmd"
    C:\Users\Test\Documents\Batch Files\Remove Quotes.cmd
    "C:\Users\Test\Documents\Batch Files\Remove Quotes.cmd"
    Press any key to continue . . .
    

    %0 is the Script Name and Path.
    %1 is the first command line argument, and so on.

提交回复
热议问题