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

前端 未结 11 570
春和景丽
春和景丽 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:52

    All the answers are complete. But Wanted to add one thing,

    set FirstName=%~1

    set LastName=%~2

    This line should have worked, you needed a small change.

    set "FirstName=%~1"

    set "LastName=%~2"

    Include the complete assignment within quotes. It will remove quotes without an issue. This is a prefered way of assignment which fixes unwanted issues with quotes in arguments.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-04 11:58

    Spent a lot of time trying to do this in a simple way. After looking at FOR loop carefully, I realized I can do this with just one line of code:

    FOR /F "delims=" %%I IN (%Quoted%) DO SET Unquoted=%%I
    

    Example:

    @ECHO OFF
    SET Quoted="Test string"
    
    FOR /F "delims=" %%I IN (%Quoted%) DO SET Unquoted=%%I
    
    ECHO %Quoted%
    ECHO %Unquoted%
    

    Output:

    "Test string"
    Test string
    
    0 讨论(0)
  • 2020-12-04 11:59

    I usually just remove all quotes from my variables with:

    set var=%var:"=%
    

    And then apply them again wherever I need them e.g.:

    echo "%var%"
    
    0 讨论(0)
  • 2020-12-04 12:04

    This sounds like a simple bug where you are using %~ somewhere where you shouldn't be. The use if %~ doesn't fundamentally change the way batch files work, it just removes quotes from the string in that single situation.

    0 讨论(0)
提交回复
热议问题