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

De-Quoting Variable Contents

67 views
Skip to first unread message

Frank

unread,
Sep 23, 2001, 8:40:17 AM9/23/01
to
I just wrote a routine that reliably remove quotes from a variable's
contents for a need that I have here. The routine, with a demonstration
script, is included below. This routine will test for and ignore the
variable if its contents do not begin with and end with a double quote.
There is one case where it will fail but a routine is provided that permits
success if it is used before calling the function.

Frank

::BEGIN SCRIPT::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@Echo OFF
::Goto :Main &:: Uncomment to run a demonstration.

::BEGIN FUNCTION::::::::::::::::::::::::::::::::::::::::::::::::::::::
:DeQuote
:: De-quotes a variable.
:: Written by Frank P. Westlake, 2001.09.22
::
:: Usage as a function within a script:
:: CALL :DeQuote VariableName %%VariableName%%
::
:: Usage as a script from the command line:
:: DeQuote.cmd VariableName %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="!@#
::
:: This function fails when a variable is quoted twice and has spaces:
:: Set var=""Two Quotes And Spaces Fails""
:: If a variable is known to be in this state it can be circumvented:
:: Set var=%var: =-S-P-A-C-E-%
:: Call :DeQuote var %%var%%
:: Set var=%var:-S-P-A-C-E-= %
::
Set DeQuote-Variable=%1
Set DeQuote-Contents=%2
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-Contents=%DeQuote-Contents:####=%
Set %DeQuote-Variable%=%DeQuote-Contents%
Goto :EOF
::END FUNCTION::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:: Delete remainder of text if demo is not required.

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: NOTE:
:: Placing frequently called functions near the top of the script
:: speeds execution.
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:Main
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 Height=5'9"
Set Symbols="!@#

:: Print before and after DeQuote for each sample:
Echo.%QuoteSpace%
Call :DeQuote QuoteSpace %%QuoteSpace%%
Echo.%QuoteSpace%
Echo.
Echo.%QuoteNoSpace%
Call :DeQuote QuoteNoSpace %%QuoteNoSpace%%
Echo.%QuoteNoSpace%
Echo.
Echo.%NoQuoteSpace%
Call :DeQuote NoQuoteSpace %%NoQuoteSpace%%
Echo.%NoQuoteSpace%
Echo.
Echo.%NoQuoteNoSpace%
Call :DeQuote NoQuoteNoSpace %%NoQuoteNoSpace%%
Echo.%NoQuoteNoSpace%
Echo.
Echo.%StartQuoteNoSpaceNoEndQuote%
Call :DeQuote StartQuoteNoSpaceNoEndQuote %%StartQuoteNoSpaceNoEndQuote%%
Echo.%StartQuoteNoSpaceNoEndQuote%
Echo.
Echo.%NoStartQuoteNoSpaceEndQuote%
Call :DeQuote NoStartQuoteNoSpaceEndQuote %%NoStartQuoteNoSpaceEndQuote%%
Echo.%NoStartQuoteNoSpaceEndQuote%
Echo.
Echo.%StartQuoteSpaceNoEndQuote%
Call :DeQuote StartQuoteSpaceNoEndQuote %%StartQuoteSpaceNoEndQuote%%
Echo.%StartQuoteSpaceNoEndQuote%
Echo.
Echo.%NoStartQuoteSpaceEndQuote%
Call :DeQuote NoStartQuoteSpaceEndQuote %%NoStartQuoteSpaceEndQuote%%
Echo.%NoStartQuoteSpaceEndQuote%
Echo.
Echo.%QuoteQuoteSpaceQuoteQuote%
Call :DeQuote QuoteQuoteSpaceQuoteQuote %%QuoteQuoteSpaceQuoteQuote%%
Echo.%QuoteQuoteSpaceQuoteQuote%
Echo. Circumvent the problem:
Set QuoteQuoteSpaceQuoteQuote=%QuoteQuoteSpaceQuoteQuote: =-S-P-A-C-E-%
Call :DeQuote QuoteQuoteSpaceQuoteQuote %%QuoteQuoteSpaceQuoteQuote%%
Set QuoteQuoteSpaceQuoteQuote=%QuoteQuoteSpaceQuoteQuote:-S-P-A-C-E-= %
Echo. %QuoteQuoteSpaceQuoteQuote%
Echo. Call again if desired:
Call :DeQuote QuoteQuoteSpaceQuoteQuote %%QuoteQuoteSpaceQuoteQuote%%
Echo. %QuoteQuoteSpaceQuoteQuote%
Echo.
Echo.%QuoteQuoteNoSpaceQuoteQuote%
Call :DeQuote QuoteQuoteNoSpaceQuoteQuote %%QuoteQuoteNoSpaceQuoteQuote%%
Echo.%QuoteQuoteNoSpaceQuoteQuote%
Echo. Call again if desired:
Call :DeQuote QuoteQuoteNoSpaceQuoteQuote %%QuoteQuoteNoSpaceQuoteQuote%%
Echo. %QuoteQuoteNoSpaceQuoteQuote%
Echo.
Echo.Height: %Height%
Call :DeQuote Height %%Height%%
Echo.Height: %Height%
Echo.
Echo.Symbols: %Symbols%
Call :DeQuote Symbols %%Symbols%%
Echo.Symbols: %Symbols%
::END SCRIPT::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Frank

unread,
Sep 23, 2001, 1:36:48 PM9/23/01
to
Frank <01c1442c$e36ee530$0125250a@jcnwhmultsiknfam>...

^ :: This function fails when a variable is quoted twice and has spaces:

Actually, a string with two quotes preceding and following is two empty
strings with the remainder of the contents in-between. This will cause
problems with any of the delimiters (space, tab, comma, semicolon, and
equals) and any of the other special characters (ampersand, pipe, caret,
and percent, ...) when the variable is passed to a function. Special cases
such as this may be better de-quoted in line rather than through a function
that accepts the name of the variable as input. To include this ability in
the function I posted would require reading the entire command line (%*)
and extracting %1 from the beginning of it. That would require a test to
determine if the OS is NT4 or NT5. In my case the short, fast function is
good enough.


^ Set DeQuote-Contents=%DeQuote-Contents:####=%

That line was made unnecessary by the FindStr lines so it can be removed.

Frank

Richard Fink

unread,
Sep 23, 2001, 11:05:48 PM9/23/01
to
"Frank" <lxyuk...@jcnwhmultsiknfam.com> wrote in message news:<01c1442c$e36ee530$0125250a@jcnwhmultsiknfam>...

> I just wrote a routine that reliably remove quotes from a variable's
> contents for a need that I have here. The routine, with a demonstration
> script, is included below. This routine will test for and ignore the
> variable if its contents do not begin with and end with a double quote.
> There is one case where it will fail but a routine is provided that permits
> success if it is used before calling the function.
>
> Frank
>
In keeping with what you are trying to achieve, I'd like to point out
that you can easily strip the leading and ending quotes from nearly
any variable with one line:

set var="sometext"
for /f "tokens=*" %%a in (%var%) do set var=%%a

The only two instances I know of where this will not work is if the
string contains these reserved characters: &,<,>,|,^ or when the
variable contains quotes within quotes. In other words, if the
variable value is: "Hello there!" it works fine. But if the variable
value is: "He said, "Hello there!"", then it won't work. The for
command will see the the value of 'set' as ("He said,") plus some
other stuff that it can't parse.

Phil Robyn

unread,
Sep 24, 2001, 1:45:45 AM9/24/01
to
Frank wrote:

Hi, Frank, how's it going? Here's a somewhat different approach. I'm assuming that your goal
is to remove beginning and ending double quotes from a variable if it has both beginning and ending
quotes and to leave either a beginning double quote or an ending double quote(?)

----------------begin screen capture WinNT 4.0 SP6a-------------------------------------------
c:\cmd>Set var=""Two Quotes And Spaces Fails""

c:\cmd>checkquotes %var%
[""Two Quotes And Spaces Fails""] begins and ends with double quotes
["Two Quotes And Spaces Fails"] begins and ends with double quotes
[Two Quotes And Spaces Fails] neither begins or ends with a double quote

c:\cmd>set Height=5'6"

c:\cmd>checkquotes %Height%
[5'6"] ends with a double quote

c:\cmd>set Symbols="!@#

c:\cmd>checkquotes %Symbols%
["!@#] begins with a double quote

c:\cmd>checkquotes now is the time
[now is the time] neither begins or ends with a double quote

c:\cmd>checkquotes "now is the time"
["now is the time"] begins and ends with double quotes
[now is the time] neither begins or ends with a double quote

c:\cmd>qblist test\checkquotes.cmd
===== begin file c:\CMD\TEST\checkquotes.cmd =====
01. @echo off
02. setlocal
03. set cd=
04. set myinput=%*
05. if not defined cd set myinput=%myinput:~1%
06. :retry
07. call :length %myinput%
08. set S2=%myinput%
09. set S2=%S2:"=$Q%
10. call set lastchar=%%S2:~%length%,2%%
11. if "%lastchar:~1,1%" NEQ "$" goto :noadjust
12. set /a length+=1
13. call set lastchar=%%S2:~%length%,2%%
14. :noadjust
15. if "%S2:~0,2%" EQU "$Q" if "%lastchar%" EQU "$Q" (
16. echo [%myinput%] begins and ends with double quotes
17. set descriptor=BE
18. )
19. if "%S2:~0,2%" NEQ "$Q" if "%lastchar%" EQU "$Q" (
20. echo [%myinput%] ends with a double quote
21. set descriptor=E
22. )
23. if "%S2:~0,2%" EQU "$Q" if "%lastchar%" NEQ "$Q" (
24. echo [%myinput%] begins with a double quote
25. set descriptor=B
26. )
27. if "%S2:~0,2%" NEQ "$Q" if "%lastchar%" NEQ "$Q" (
28. echo [%myinput%] neither begins or ends with a double quote
29. set descriptor=N
30. )
31. if %descriptor% NEQ BE goto :EOF
32. set myinput=###%myinput%###
33. set myinput=%myinput:"###=%
34. set myinput=%myinput:###"=%
35. goto :retry
36.
37. :length
38. set length=-1
39. set S1=%myinput%
40. :lloop
41. if not defined S1 goto :EOF
42. set /a length+=1
43. set S1=%S1:~1%
44. goto :lloop
===== end file c:\CMD\TEST\checkquotes.cmd =====
----------------------end screen capture WinNT 4.0 SP6a-----------------------


--

u n z i p m y a d d r e s s t o s e n d e - m a i l


Frank

unread,
Sep 24, 2001, 6:10:06 AM9/24/01
to
Richard Fink <3a6f7570.01092...@posting.google.com>...

^ set var="sometext"
^ for /f "tokens=*" %%a in (%var%) do set var=%%a

That procedure fails with three of the examples I provided.

Frank

Frank

unread,
Sep 24, 2001, 6:25:50 AM9/24/01
to
Phil Robyn <3BAEC887...@uclink.berkzipeley.edu>...

That procedure doesn't set the original variable, which was my goal.

Frank

Simon Sheppard

unread,
Sep 24, 2001, 2:25:33 PM9/24/01
to
On Sun, 23 Sep 2001 12:40:17 GMT, "Frank"
<lxyuk...@jcnwhmultsiknfam.com> wrote:

>I just wrote a routine that reliably remove quotes from a variable's
>contents for a need that I have here. The routine, with a demonstration
>script, is included below. This routine will test for and ignore the
>variable if its contents do not begin with and end with a double quote.

This is very neat Frank - thanks for posting it
-
Simon Sheppard
Web: http://www.ss64.com
email: Simon@ "

Frank

unread,
Sep 24, 2001, 9:41:57 PM9/24/01
to
Frank <01c1442c$e36ee530$0125250a@jcnwhmultsiknfam>...

Here's a cleaned up version of the function:

::BEGIN FUNCTION::::::::::::::::::::::::::::::::::::::::::::::::::::::
:DeQuote VariableName %VariableName%
:: Removes the outer set of double quotes from a variable.
:: Written by Frank P. Westlake, 2001.09.22, 2001.09.24


::
:: Usage as a function within a script:

:: CALL :DeQuote VariableName %VariableName%


::
:: Usage as a script from the command line:
:: DeQuote.cmd VariableName %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="!@#
::

:: This function fails when a variable is quoted twice and has delimiters:
:: Set var=""Two Quotes;And,Delimiters=Fails""
::
Set DeQuote.Variable=%1&Set DeQuote.Contents=%2
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::::::::::::::::::::::::::::::::::::::::::::::::::::::::

0 new messages