The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My work

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,346 other followers

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

Posted by jpluimers on 2018/02/06

Thanks [WayBackMr. Rick for the answer as this is exactly the bit I needed:

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

[WayBackRemoving double quotes from variables in batch file creates problems with CMD environment – Stack Overflow

–jeroen

3 Responses to “Removing double quotes from variables in batch file creates problems with CMD environment – Stack Overflow”

  1. Peter Wright said

    The syntax

    set "widget=a very useful item"

    is probably more useful. Here, the quotes delimit the string being assigned so that “editors” like notepad, which can leave spaces on the end of the line, don’t cause those extra (invisible) trailing spaces to be included in the value.

    In principle, avoid including quotes in the values assigned to variables and insert them as required, for instance

    for %%a in ( "%widget%" %widget%) do echo %%a

    would show

    a very useful item
    a
    very
    useful
    item

    This evades batch string manipulation, which is a minefield.

    • Peter Wright said

      Oops – that should be echo %%~a

      The ~ removes enclosing quotes for a metavariable.

      • jpluimers said

        The problem is that sometimes you do not have influence on the input variables because they come from elsewhere.

        So I’ve adopted your comments to show the code as code blocks (so quotes become OK), then adopted the script in it to show what happens without and with the variable content itself having quotes:

        @echo off
        echo No double quotes in widget:
        set "widget=a very useful item"
        set widget
        for %%a in ( "%widget%" %widget%) do echo %%~a
        echo With double quotes in widget:
        set widget="a very useful item"
        for %%a in ( "%widget%" %widget%) do echo %%~a

        Output:

        No double quotes in widget:
        widget=a very useful item
        a very useful item
        a
        very
        useful
        item
        With double quotes in widget:
        "a
        very
        useful
        item""
        a very useful item

        There you see that using a tilde (~) with double-quoting variable content already having double quotes, the output inserts an extra terminating double quote.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: