Question About DeQuote Script

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Been thanked: 2 times
Contact:

Question About DeQuote Script

Post by MigrationUser »

09 Dec 2007 03:10
Nicholi


I got a few questions about the DeQuote script I saw on the site.

What is the colon doing in the following line after FINDSTR and before the GOTO command?

Code: Select all

Echo.%DeQuote.Contents%|FindStr/brv ""^">NUL:&&Goto :EOF
                                            ^
I understand this part of the script is to act as a filter for vars without double quotes at beginning and end, but I don't understand the syntactical purpose of that colon. And never read about anything like it before. Otherwise I clearly see that the GOTO command is only meant to run if the FINDSTR command was successful, thus &&.

Also since regular expressions are being used, what is the ""^" actually searching for? Wouldn't the correct regexp be '^"' and '"$' to find double quotes at the beginning and end of variables? That way you would not need to use the /B and /E parameters either.

Last edited by Nicholi (09 Dec 2007 03:12)

----------------------------

#2 09 Dec 2007 14:59
Simon Sheppard


The DeQuote script is the result of a couple of newsgroup threads from back in 2001/2002. The original script (by frank) is discussed here The modified version is here

The colon you notice is a standard delimiter, you should (in theory at least) be able to use space/tab/colon/equals/semicolon interchangeably as command delimiters, I think a colon was chosen here to avoid line wraps in the newsgroup.

The reason the script is so complex is that it's designed to catch all possible cases of null values, quotes and spaces in the string, so that you can throw just about any string into the function and still get the desired output.

Code: Select all

:: 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""     ----This is the one case that fails
It should be noted this was written when NT4 was still very common - if you are running XP and 2003 you can probably just use %~1 to get a parameter without spaces. see https://ss64.com/nt/syntax-args.html I need to do some testing to see how the two methods compare with all the cases above.

----------------------------

#3 09 Dec 2007 21:36
Nicholi


Ahh hmm, no wonder. It was written in days of y'ore.

Well here is my own modification for modern days then, which as far as I see, should work in _EVERY_ instance. I typically prefer being able to pass multiple variables to small functions/scripts like this. Which is why it starts with a FOR loop.

Code: Select all

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

:DeQuote
FOR %%G IN (%*) DO (
SET DeQuote.Variable=%%G
CALL SET DeQuote.Contents=%%!DeQuote.Variable!%%

IF [!DeQuote.Contents:~0^,1!]==[^"] (
IF [!DeQuote.Contents:~-1!]==[^"] (
SET DeQuote.Contents=!DeQuote.Contents:~1,-1!
) ELSE (GOTO :EOF no end quote)
) ELSE (GOTO :EOF no beginning quote)

SET !DeQuote.Variable!=!DeQuote.Contents!
SET DeQuote.Variable=
SET DeQuote.Contents=
)
GOTO :EOF

So it can be used as thus.
SET VAR1="I am some text"
SET VAR2=I am also some text"

CALL :DeQuote VAR1,VAR2
And only VAR1 will be dequoted. I still don't understand how the regexp search for ""^" is working in the original FINDSTR. Especially when it's searching for the double quote at the end.

----------------------------

#4 09 Dec 2007 22:55
Simon Sheppard
Nicholi wrote:

I still don't understand how the regexp search for ""^" is working in the original FINDSTR. Especially when it's searching for the double quote at the end.
Searching for a quote with findstr is tricky because it already uses quote symbols to hold the search string

From memory FindStr/brv ""^" is combining the expression "" (which evaluates to a single quote) with the expression ^", which evaluates to nothing but simply stops findstr from complaining that the search string is missing a closing quote.

You might expect this should just be something like FindStr/brv """ or FindStr/erv """ but life is never that easy!!

The new version you've posted looks really promising, thanks for posting it, I'll have to have a play with it over the next day or two.

----------------------------

#5 11 Dec 2007 20:42

Simon Sheppard


OK I've put a modified version up here https://ss64.com/nt/syntax-dequote.html

Code: Select all

@ECHO OFF
 
 :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
 :: Written by Frank P. Westlake, 2001.09.22, 2001.09.24
 :: Modified by Simon Sheppard 2002.06.09
I left out the FOR and changed the variable names just to make it a bit easier for beginners to follow.

Actually it just occurs to me it would be simple to add a SHIFT clause and have it dequote multiple variables passed as %1, %2 etc

----------------------------

#6 12 Dec 2007 11:24
Nicholi

Code: Select all

Simon Sheppard wrote:

    Actually it just occurs to me it would be simple to add a SHIFT clause and have it dequote multiple variables passed as %1, %2 etc
Ahh yes, that would work equally well for passing multiple variables. I'm just a sucker for FOR loops smile. You should probably also add a note to that page that the function will require ENABLEDELAYEDEXPANSION.

Thanks for the lowdown on how that regexp is breaking down in the shell as well.

Last edited by Nicholi (12 Dec 2007 11:28)

----------------------------

#7 29 Dec 2007 06:08
mmojica


Hi,

I need to remove single quotes from my variables. Any help would be appreciated.

--Moises

________________________
example output below:

'variable1'
'another variable
variable2'

________________________
batch file below:

@ECHO OFF

FOR /F "tokens=3 delims= " %%A IN ('smbios2 /G ^| find /I "serial number"') DO SET VAR1=%%A
FOR /F "tokens=3,4 delims= " %%A IN ('smbios2 /G ^| find /I "product name"') DO SET VAR2=%%A %%B
FOR /F "tokens=2 delims= " %%A IN ('smbios2 /G ^| find /I "product:"') DO SET VAR3=%%A

ECHO %VAR1%
ECHO %VAR2%
ECHO %VAR3%

----------------------------

#8 29 Dec 2007 13:04
bluesxman


You could probably get away with doing this:

Code: Select all

@ECHO OFF

FOR /F "tokens=3 delims= " %%A IN ('smbios2 /G ^| find /I "serial number"') DO SET VAR1=%%A
FOR /F "tokens=3,4 delims= " %%A IN ('smbios2 /G ^| find /I "product name"') DO SET VAR2=%%A %%B
FOR /F "tokens=2 delims= " %%A IN ('smbios2 /G ^| find /I "product:"') DO SET VAR3=%%A

ECHO %VAR1%
ECHO %VAR2%
ECHO %VAR3%
set VAR1=%VAR1:'=%
set VAR2=%VAR2:'=%
set VAR3=%VAR3:'=%
ECHO %VAR1%
ECHO %VAR2%
ECHO %VAR3%
cmd | *sh | ruby | chef

----------------------------

#9 29 Dec 2007 21:26
mmojica


Thank you,

I actually figured it out about an hour after I posted my question and came up with the same results. This option works well because I can do character manipulation as well rather than just deleting the ones that are not needed.

----------------------------

#10 26 Jan 2008 15:58
mrQQ


hello,

i've tried dequote script, but it doesnt seem to work..

this is what i put into file:

Code: Select all

SETLOCAL EnableDelayedExpansion

set _MyVariable="C:\Program Files\ss64\"
CALL :dequote _MyVariable
echo %_MyVariable%

goto :eof

:DeQuote

SET _DeQuoteVar=%1
CALL SET _DeQuoteString=%%!_DeQuoteVar!%%
IF [!_DeQuoteString:~0^,1!]==[^"] (
IF [!_DeQuoteString:~-1!]==[^"] (SET _DeQuoteString=!_DeQuoteString:~1,-1!
) ELSE (GOTO :EOF)
) ELSE (GOTO :EOF)SET !_DeQuoteVar!=!_DeQuoteString!
SET _DeQuoteVar=
SET _DeQuoteString=
GOTO :EOF
and this is what i get:

Code: Select all

D:>test.bat

D:\>SETLOCAL EnableDelayedExpansion

D:\>SETLOCAL EnableExtensions

D:\>set _MyVariable="C:\Program Files\ss64\"

D:\>echo "C:\Program Files\ss64\"
"C:\Program Files\ss64\"

D:\>CALL :dequote _MyVariable

D:\>SET _DeQuoteVar=_MyVariable

D:\>CALL SET _DeQuoteString=%!_DeQuoteVar!%
SET was unexpected at this time.

D:\>) ELSE (GOTO :EOF)SET !_DeQuoteVar!=!_DeQuoteString!
why? :(

----------------------------

#11 29 Jan 2008 13:07
bluesxman


This line...

Code: Select all

) ELSE (GOTO :EOF)SET !_DeQuoteVar!=!_DeQuoteString!
...is the problem. The SET command should be on a new line.

cmd | *sh | ruby | chef

----------------------------

#12 28 Feb 2008 23:10
simplyjaded
bluesxman wrote:

This line...

Code: Select all

    ) ELSE (GOTO :EOF)SET !_DeQuoteVar!=!_DeQuoteString!
...is the problem. The SET command should be on a new line.

I tried your suggestion above but its still not working... sad
Here's the code:

Code: Select all

::@echo off
SetLocal EnableDelayedExpansion
set _MyVariable = "C:\Program Files\ss64\"
CALL :dequote _MyVariable
echo MyVariable %_MyVariable%



:DeQuote

SET _DeQuoteVar=%1
CALL SET _DeQuoteString=%%!_DeQuoteVar!%%
IF [!_DeQuoteString:~0^,1!]==[^"] (
IF [!_DeQuoteString:~-1!]==[^"] (SET _DeQuoteString=!_DeQuoteString:~1,-1!
) ELSE (GOTO :EOF)
) ELSE (GOTO :EOF)
SET !_DeQuoteVar!=!_DeQuoteString!
SET _DeQuoteVar=
SET _DeQuoteString=
GOTO :EOF
And the output is:

Code: Select all

C:\Batchfiles>Quote

C:\Batchfiles>SetLocal EnableDelayedExpansion

C:\Batchfiles>set _MyVariable = "C:\Program Files\ss64\"

C:\Batchfiles>CALL :dequote _MyVariable

C:\Batchfiles>SET _DeQuoteVar=_MyVariable

C:\Batchfiles>CALL SET _DeQuoteString=%!_DeQuoteVar!%

C:\Batchfiles>IF [!_DeQuoteString:~0,1!] == ["] (IF [!_DeQuoteString:~-1!] == ["
] (SET _DeQuoteString=!_DeQuoteString:~1,-1! )  ELSE (GOTO :EOF ) )  ELSE (GOTO
:EOF )

C:\Batchfiles>echo MyVariable
MyVariable

C:\Batchfiles>SET _DeQuoteVar=

C:\Batchfiles>CALL SET _DeQuoteString=%!_DeQuoteVar!%

C:\Batchfiles>IF [!_DeQuoteString:~0,1!] == ["] (IF [!_DeQuoteString:~-1!] == ["
] (SET _DeQuoteString=!_DeQuoteString:~1,-1! )  ELSE (GOTO :EOF ) )  ELSE (GOTO
:EOF )

C:\Batchfiles>
Whats wrong??:(

----------------------------

#13 28 Feb 2008 23:34
Simon Sheppard


Change this line
set _MyVariable = "C:\Program Files\ss64\"

to
set _MyVariable="C:\Program Files\ss64\"

This is because variable names can include spaces so in the first example you would be creating a variable with the not terribly useful name of [_MyVariable ]

----------------------------

#14 19 Mar 2008 10:57
klint


Hi, I'm "klint" and I'm new to this forum. I apologise in advance if this has been suggested already. I had a quick read and couldn't find it.

Has anyone thought of using the %%~a syntax to remove quotes? See FOR /? for more information. Here's a test batch file:

Code: Select all

@echo off
setlocal enabledelayedexpansion

set string1="this string was originally quoted"
set string2=this string is not quoted
set string3='this string has 'single quotes'

for /f "usebackq delims=" %%s in ('!string1!') do set string1=%%~s
for /f "usebackq delims=" %%s in ('!string2!') do set string2=%%~s
for /f "usebackq delims=" %%s in ('!string3!') do set string3=%%~s

rem Now show the values of the processed strings. We expect only the
rem quoted string to be un-quoted; the others should be the same.
set string
Last edited by klint (19 Mar 2008 10:58)

----------------------------

#15 29 Mar 2008 13:05
Simon Sheppard


Theres a few strings which may break the above such as Height=5'6" and Symbols="!@#

If you are sure those cases will never crop up, there's nothing wrong with a one line FOR command, but having got into the habit I now find it quicker to just type CALL :dequote SomeVariable

----------------------------

#16 02 Oct 2008 04:09
a1s2d3f4


Just tried the :DeQuote routine.:pc:

Here is what my bat file looks like:

Code: Select all

SetLocal EnableDelayedExpansion
set _MyVariable="C:\Program Files\ss64\"  
CALL :dequote _MyVariable 
echo %_MyVariable%
goto :EOF


:DeQuote 

SET _DeQuoteVar=%1
CALL SET _DeQuoteString=%%!_DeQuoteVar!%%
IF [!_DeQuoteString:~0^,1!]==[^"] (
IF [!_DeQuoteString:~-1!]==[^"] (SET _DeQuoteString=!_DeQuoteString:~1,-1!
) ELSE (GOTO :EOF)
) ELSE (GOTO :EOF)
SET !_DeQuoteVar!=!_DeQuoteString!
SET _DeQuoteVar=
SET _DeQuoteString=
GOTO :EOF
Here is the output:

Code: Select all

C:\PROGRA~1\SEPROG~1>SetLocal EnableDelayedExpansion

C:\PROGRA~1\SEPROG~1>set _MyVariable="C:\Program Files\ss64\"

C:\PROGRA~1\SEPROG~1>CALL :dequote _MyVariable

C:\PROGRA~1\SEPROG~1>SET _DeQuoteVar=_MyVariable

C:\PROGRA~1\SEPROG~1>CALL SET _DeQuoteString=%!_DeQuoteVar!%

C:\PROGRA~1\SEPROG~1>IF [!_DeQuoteString:~0,1!] == ["] (IF [!_DeQuoteString:~-1!
] == ["] (SET _DeQuoteString=!_DeQuoteString:~1,-1! )  ELSE (GOTO :EOF ) )  ELSE
 (GOTO :EOF )

C:\PROGRA~1\SEPROG~1>echo "C:\Program Files\ss64\"
"C:\Program Files\ss64\"

C:\PROGRA~1\SEPROG~1>goto :EOF
Looks like it is not removing the quotes...

Please help?

Thanks.

Win8.1 x64

----------------------------

#17 02 Oct 2008 04:33
a1s2d3f4


Ok, luckily I solved my problem.

What it was - and you can actually "see it" if you select my code above - I had 2 extra spaces after the last " in my set _MyVariable command. Once I removed them, everything worked fine.

Win8.1 x64
Post Reply