Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Removing Quotes

100 views
Skip to first unread message

Simon Sheppard

unread,
Jun 9, 2002, 9:28:22 AM6/9/02
to
Thought I'd share this with the group - a simplified version of
Frank's routine to remove quotes from a variable.

This version takes a single parameter
e.g.
DeQuote MyVariable

All lines below are indented with 3 spaces

:: A routine that will reliably remove quotes from a variable's
contents.

:: This routine will only affects items that both begin AND end
with
:: a double quote.
:: e.g. will turn "C:\Program Files\somefile.txt"
:: into C:\Program Files\somefile.txt
:: while still preserving cases such as Height=5'6" and
Symbols="!@#


::BEGIN FUNCTION:::::::::::::::::::::::::::::::::::::::::::::::::
@ECHO OFF

:: Removes the outer set of double quotes from a variable.
:: Written by Frank P. Westlake, 2001.09.22, 2001.09.24
:: Modified by Simon Sheppard 2002.06.09

:: Usage as a function within a script:
:: CALL :DeQuote VariableName
::
:: Calling as a function from another batch file:
:: CALL DeQuote.cmd VariableName
::
:: If the first and last characters of the variable contents are
double
:: quotes then they will be removed. This function preserves cases
such as
:: Set Height=5'6" and Set Symbols="!@#
::
:: If a variable is quoted twice and has delimiters then you will
:: need to run the function twice to remove both sets.
:: Set var=""Two Quotes;And,Delimiters=Fails""
::
:: If the variable name itself contains spaces the routine will
fail
:: e.g. %v_my_variable% rather than %my variable%

:DeQuote
SET DeQuote.Variable=%1
CALL Set DeQuote.Contents=%%%DeQuote.Variable%%%
Echo.%DeQuote.Contents%|FindStr/brv ""^">NUL:&&Goto :EOF
Echo.%DeQuote.Contents%|FindStr/erv ""^">NUL:&&Goto :EOF

Set DeQuote.Contents=####%DeQuote.Contents%####
Set DeQuote.Contents=%DeQuote.Contents:####"=%
Set DeQuote.Contents=%DeQuote.Contents:"####=%
Set %DeQuote.Variable%=%DeQuote.Contents%

Set DeQuote.Variable=
Set DeQuote.Contents=
Goto :EOF
::END FUNCTION:::::::::::::::::::::::::::::::::::::::::::::::::


-
Simon Sheppard
Web: http://www.ss64.com
email: Simon@ "
-

Herbert Kleebauer

unread,
Jun 9, 2002, 3:25:10 PM6/9/02
to
Simon Sheppard wrote:
>
> Thought I'd share this with the group - a simplified version of
> Frank's routine to remove quotes from a variable.

Why not just:

set a="abc def gh"
for %%i in (%a%) do set b=%%~i
echo %b%

Frank

unread,
Jun 9, 2002, 6:56:44 PM6/9/02
to
Herbert <3D03AB96...@unibwm.de>...

^ Why not just:
^
^ set a="abc def gh"
^ for %%i in (%a%) do set b=%%~i

That question was asked after I submitted the function in September and I
answered by identifying some cases where the above fails. I had some
specific needs for the function to behave as it does, and after I went to
the trouble of writing it I offered it for anyone else who may have the
same needs.

Thank you for the improvement Simon, I'll have to study it.

Frank

Al Dunbar

unread,
Jun 9, 2002, 7:47:35 PM6/9/02
to

"Herbert Kleebauer" <kl...@unibwm.de> wrote in message
news:3D03AB96...@unibwm.de...

Perhaps because one is not able to upgrade from NT to 2K.

I have no NT machine to try this on, but an old copy of the output from
"help for" does NOT include the line:

%~I - expands %I removing any surrounding quotes (")


/Al

Frank

unread,
Jun 9, 2002, 10:14:45 PM6/9/02
to
Al Dunbar <rORM8.118701$Ka.84...@news2.calgary.shaw.ca>...

^ I have no NT machine to try this on, but an old copy of the output from
^ "help for" does NOT include the line:
^
^ %~I - expands %I removing any surrounding quotes (")

Thanks for that, I had to guess. It is not documented but does remove
quotes with NT4. What it does not do is properly handle some of the cases
identified in the comments of Simon's script (which works does work better
than mine by the way).

Frank

Ritchie Lawrence

unread,
Jun 10, 2002, 5:41:31 AM6/10/02
to
Hi Simon,
Thanks for posting this. How about a follow-up explaining the drawbacks
of previous de-quoting methods and how your method overcomes these?

--
Ritchie
Undo address for email.

"Simon Sheppard" <si...@spam.invalid> wrote in message news:3d03528c...@news.demon.co.uk...

Frank

unread,
Jun 10, 2002, 6:55:41 AM6/10/02
to
Ritchie Lawrence <3d047...@mk-nntp-1.news.uk.worldonline.com>...

^ How about a follow-up explaining the drawbacks of previous
^ de-quoting methods and how your method overcomes these?

His message is a follow-up to mine. The two should be read together. Below
is a link to the original thread. Note that I mention a case where it will
fail. That is what Simon fixed.

<http://groups.google.com/groups?hl=en&lr=&threadm=01c1442c%24e36ee530%24012
5250a%40jcnwhmultsiknfam&rnum=1&prev=/groups%3Fq%3D:DeQuote%2Bgroup:alt.msdo
s.batch.nt%26hl%3Den%26lr%3D%26selm%3D01c1442c%2524e36ee530%25240125250a%254
0jcnwhmultsiknfam%26rnum%3D1>

If that link fails, search Google for ":DeQuote" in this group.

Frank

Simon Sheppard

unread,
Jun 10, 2002, 2:54:30 PM6/10/02
to
On Mon, 10 Jun 2002 10:41:31 +0100, "Ritchie Lawrence"
<rlaw...@commanddoline.co.uk> wrote:

>Hi Simon,
>Thanks for posting this. How about a follow-up explaining the drawbacks
>of previous de-quoting methods and how your method overcomes these?
>
>--
>Ritchie

The big problem with any solution involving the FOR command is that
the presence/absence of quotes will affect the operation of FOR

e.g.
FOR /f %%a in (something)
is treated differently to
FOR /f %%a in ("something")

Generally resulting in the error "The system cannot find the file XYZ"

Obviously if you can guarantee that the variable will ALWAYS have
surrounding quotes then a simpler solution will do the job.

Here's a second script that demonstrates, again adapted from Frank's
original. Save DeQuoteTEST.cmd (below) in the same folder as
Dequote.cmd

You can then test any alternative dequoting routine by just renaming
it as DeQuote.cmd and re-running the test script.

:: DeQuoteTest.cmd

:: This script CALL's DEQUOTE.cmd and tests it with a variety of
strings

::START SCRIPT::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off
SetLocal ENABLEEXTENSIONS
::
:: Set some sample variables:
Set QuoteSpace="Quote And Spaces"
Set QuoteNoSpace="QuoteNoSpaces"
Set NoQuoteSpace=No Quote Spaces
Set NoQuoteNoSpace=NoQuoteNoSpaces
Set StartQuoteNoSpaceNoEndQuote="StartQuoteNoSpaceNoEndQuote
Set NoStartQuoteNoSpaceEndQuote=NoStartQuoteNoSpaceEndQuote"
Set StartQuoteSpaceNoEndQuote="Start Quote Spaces No End Quote
Set NoStartQuoteSpaceEndQuote=No Start Quote Spaces End Quote"
Set QuoteQuoteSpaceQuoteQuote=""Quote Quote Spaces Quote Quote""
Set QuoteQuoteNoSpaceQuoteQuote=""QuoteQuoteNoSpacesQuoteQuote""
Set TwoQuotesAndDelimiters=""Two Quotes;And,Delimiters=Fails""

Set Height=5'9"
Set Symbols="!@#

:: Print before and after DeQuote for each sample:
Echo.%QuoteSpace%
Call DeQuote QuoteSpace
Echo.%QuoteSpace%
Echo.
Echo.%QuoteNoSpace%
Call DeQuote QuoteNoSpace
Echo.%QuoteNoSpace%
Echo.
Echo.%NoQuoteSpace%
Call DeQuote NoQuoteSpace
Echo.%NoQuoteSpace%
Echo.
Echo.%NoQuoteNoSpace%
Call DeQuote NoQuoteNoSpace
Echo.%NoQuoteNoSpace%

Echo.
Echo.%StartQuoteNoSpaceNoEndQuote%
Call DeQuote StartQuoteNoSpaceNoEndQuote
Echo.%StartQuoteNoSpaceNoEndQuote%

Echo.
Echo.%NoStartQuoteNoSpaceEndQuote%
Call DeQuote NoStartQuoteNoSpaceEndQuote
Echo.%NoStartQuoteNoSpaceEndQuote%

Echo.
Echo.%StartQuoteSpaceNoEndQuote%
Call DeQuote StartQuoteSpaceNoEndQuote
Echo.%StartQuoteSpaceNoEndQuote%

Echo.
Echo.%NoStartQuoteSpaceEndQuote%
Call DeQuote NoStartQuoteSpaceEndQuote
Echo.%NoStartQuoteSpaceEndQuote%

Echo.
Echo.%QuoteQuoteSpaceQuoteQuote%
Call DeQuote QuoteQuoteSpaceQuoteQuote
Echo.%QuoteQuoteSpaceQuoteQuote%

Echo. Circumvent the problem:
Set QuoteQuoteSpaceQuoteQuote=%QuoteQuoteSpaceQuoteQuote:
=-S-P-A-C-E-%
Call DeQuote QuoteQuoteSpaceQuoteQuote
Set
QuoteQuoteSpaceQuoteQuote=%QuoteQuoteSpaceQuoteQuote:-S-P-A-C-E-= %
Echo. %QuoteQuoteSpaceQuoteQuote%
Echo. Call again if desired:
Call DeQuote QuoteQuoteSpaceQuoteQuote
Echo. %QuoteQuoteSpaceQuoteQuote%

Echo.
Echo.%QuoteQuoteNoSpaceQuoteQuote%
Call DeQuote QuoteQuoteNoSpaceQuoteQuote
Echo.%QuoteQuoteNoSpaceQuoteQuote%
Echo. Call again if desired:
Call DeQuote QuoteQuoteNoSpaceQuoteQuote
Echo. %QuoteQuoteNoSpaceQuoteQuote%

Echo.
Echo.%TwoQuotesAndDelimiters%
Call DeQuote TwoQuotesAndDelimiters
Echo.%TwoQuotesAndDelimiters%

Echo.
Echo.Height: %Height%
Call DeQuote Height
Echo.Height: %Height%
Echo.
Echo.Symbols: %Symbols%
Call DeQuote Symbols
Echo.Symbols: %Symbols%
::END SCRIPT:::::::::::::::::::::::::::::::::::::::::::::::::::::

Ritchie Lawrence

unread,
Jun 11, 2002, 6:32:49 AM6/11/02
to
(Cheers Frank, I found the thread)

Hi Simon,
Thanks for the reply. I like passing parameters this way (by variable
name), its rather like passing a pointer. Cont...

"Simon Sheppard" <si...@spam.invalid> wrote in message news:3d04f5d6...@news.demon.co.uk...


> On Mon, 10 Jun 2002 10:41:31 +0100, "Ritchie Lawrence"
> <rlaw...@commanddoline.co.uk> wrote:
>
> >Hi Simon,
> >Thanks for posting this. How about a follow-up explaining the drawbacks
> >of previous de-quoting methods and how your method overcomes these?
> >
> >--
> >Ritchie
>
> The big problem with any solution involving the FOR command is that
> the presence/absence of quotes will affect the operation of FOR
>
> e.g.
> FOR /f %%a in (something)
> is treated differently to
> FOR /f %%a in ("something")
>
> Generally resulting in the error "The system cannot find the file XYZ"


I'm probably missing the point, would that particular error not be avoided
by using the FOR/F 'command' option:-

FOR /f %%a in ('echo:%myvar%')


FOR /f %%a in ('type myfile')


Regarding multi-quoted variables (""Quote Quote Spaces Quote Quote""), I
noticed your code could be made recursive to deal with these by the
addition of one line. See below.

====================== start =======================================
@echo off&setlocal


Set QuoteQuoteSpaceQuoteQuote=""Quote Quote Spaces Quote Quote""

Echo.%QuoteQuoteSpaceQuoteQuote%
Call :DeQuote QuoteQuoteSpaceQuoteQuote %%QuoteQuoteSpaceQuoteQuote%%
Echo.%QuoteQuoteSpaceQuoteQuote%

goto:eof

:DeQuote
SET DeQuote.Variable=%1
CALL Set DeQuote.Contents=%%%DeQuote.Variable%%%
Echo.%DeQuote.Contents%|FindStr/brv ""^">NUL:&&Goto :EOF
Echo.%DeQuote.Contents%|FindStr/erv ""^">NUL:&&Goto :EOF

Set DeQuote.Contents=####%DeQuote.Contents%####
Set DeQuote.Contents=%DeQuote.Contents:####"=%
Set DeQuote.Contents=%DeQuote.Contents:"####=%
Set %DeQuote.Variable%=%DeQuote.Contents%

::Line below added to handle multi-quoted strings with single call
call :DeQuote %DeQuote.Variable%

Set DeQuote.Variable=
Set DeQuote.Contents=
================================== end ==================================

Thanks again for posting the code.

Cheers,
Ritchie

Ritchie Lawrence

unread,
Jun 11, 2002, 9:30:22 AM6/11/02
to
[Apologies if this post appears twice. I'm only seeing one copy, if any]


(Cheers Frank, I found the thread)

Hi Simon,
Thanks for the reply. I like passing parameters this way (by variable
name), its rather like passing a pointer. Cont...

"Simon Sheppard" <si...@spam.invalid> wrote in message news:3d04f5d6...@news.demon.co.uk...
> On Mon, 10 Jun 2002 10:41:31 +0100, "Ritchie Lawrence"
> <rlaw...@commanddoline.co.uk> wrote:
>

> >Hi Simon,
> >Thanks for posting this. How about a follow-up explaining the drawbacks
> >of previous de-quoting methods and how your method overcomes these?
> >
> >--
> >Ritchie
>

> The big problem with any solution involving the FOR command is that
> the presence/absence of quotes will affect the operation of FOR
>
> e.g.
> FOR /f %%a in (something)
> is treated differently to
> FOR /f %%a in ("something")
>
> Generally resulting in the error "The system cannot find the file XYZ"


I'm probably missing the point, would that particular error not be avoided
by using the FOR/F 'command' option:-

FOR /f %%a in ('echo:%myvar%')


FOR /f %%a in ('type myfile')


Regarding multi-quoted variables (""Quote Quote Spaces Quote Quote""), I
noticed your code could be made recursive to deal with these by the
addition of one line. See below.

====================== start =======================================
@echo off&setlocal
Set QuoteQuoteSpaceQuoteQuote=""Quote Quote Spaces Quote Quote""

Echo.%QuoteQuoteSpaceQuoteQuote%
Call :DeQuote QuoteQuoteSpaceQuoteQuote %%QuoteQuoteSpaceQuoteQuote%%
Echo.%QuoteQuoteSpaceQuoteQuote%

goto:eof

:DeQuote


SET DeQuote.Variable=%1
CALL Set DeQuote.Contents=%%%DeQuote.Variable%%%
Echo.%DeQuote.Contents%|FindStr/brv ""^">NUL:&&Goto :EOF
Echo.%DeQuote.Contents%|FindStr/erv ""^">NUL:&&Goto :EOF

Set DeQuote.Contents=####%DeQuote.Contents%####
Set DeQuote.Contents=%DeQuote.Contents:####"=%
Set DeQuote.Contents=%DeQuote.Contents:"####=%
Set %DeQuote.Variable%=%DeQuote.Contents%

::Line below added to handle multi-quoted strings with single call

Simon Sheppard

unread,
Jun 11, 2002, 4:26:17 PM6/11/02
to
On Tue, 11 Jun 2002 11:32:49 +0100, "Ritchie Lawrence"
<rlaw...@commanddoline.co.uk> wrote:

>I'm probably missing the point, would that particular error not be avoided
>by using the FOR/F 'command' option:-
>
> FOR /f %%a in ('echo:%myvar%')
>

Good point - although if the variable contains the text ON or OFF then
you get unexpected behaviour

A better one line routine is
SET myvar=%myvar:"=%

>
>Regarding multi-quoted variables (""Quote Quote Spaces Quote Quote""), I
>noticed your code could be made recursive to deal with these by the
>addition of one line.

nice addition - thanks

Ritchie Lawrence

unread,
Jun 11, 2002, 5:54:03 PM6/11/02
to
"Simon Sheppard" <si...@spam.invalid> wrote in message news:3d06598d...@news.demon.co.uk...

> On Tue, 11 Jun 2002 11:32:49 +0100, "Ritchie Lawrence"
> <rlaw...@commanddoline.co.uk> wrote:
>
> >I'm probably missing the point, would that particular error not be avoided
> >by using the FOR/F 'command' option:-
> >
> > FOR /f %%a in ('echo:%myvar%')
> >
> Good point - although if the variable contains the text ON or OFF then
> you get unexpected behaviour


Notice I used a colon immediately after ECHO. This prevents the ECHO command
from misbehaving as you describe, when the variable is set to 'ON', 'OFF' or
'/?'. The colon also eliminates the problem caused by a file named ECHO in
the working directroy.

0 new messages