A Critique on "Trim Quotes - Remove surrounding quotes"

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

A Critique on "Trim Quotes - Remove surrounding quotes"

#1 Post by ghostmachine4 » 05 Apr 2011 09:47

Source : Trim Quotes


It says
The FOR command can be used to safely remove quotes surrounding a string

The demonstration is supposed to remove double quotes surrounding a string. If that's the case, a simple remove might do, such as the method demonstrated here.

What if I feel that things are not so simple? For example, the batch code fails when there are strings such as

Code: Select all

@echo off
set str=test "cmd politic" test2
echo.%str%
for /f "useback tokens=*" %%a in ('%str%') do set str=%%~a
echo.%str%


So its supposed to remove the double quotes surrounding "cmd politic", but what i see is this

Code: Select all

C:\work>test.bat
test "cmd politic" test2
test "cmd politic" test2


when i should be getting

Code: Select all

test cmd politic test2


Again , a simple remove might be used for this case. Why a for loop? What does it mean by "safely remove double quotes surrounding a string"

DosItNotHelp

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

Re: A Critique on "Trim Quotes - Remove surrounding quotes"

#2 Post by DosItHelp » 05 Apr 2011 22:33

"safely remove double quotes surrounding a string" was meant to mean surrounding the whole string, not counting double quotes within the string.
Any suggestions on how to formulate this better are welcome.

Post Reply