#1 09 Dec 2007 03:10

Nicholi
Member
Registered: 09 Dec 2007
Posts: 7

Question About DeQuote Script

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?

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)

Offline

#2 09 Dec 2007 14:59

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 777
Website

Re: Question About DeQuote Script

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.

:: 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 http://ss64.com/nt/syntax-args.html I need to do some testing to see how the two methods compare with all the cases above.

Offline

#3 09 Dec 2007 21:36

Nicholi
Member
Registered: 09 Dec 2007
Posts: 7

Re: Question About DeQuote Script

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.

@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.

Offline

#4 09 Dec 2007 22:55

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 777
Website

Re: Question About DeQuote Script

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.

Offline

#5 11 Dec 2007 20:42

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 777
Website

Re: Question About DeQuote Script

OK I've put a modified version up here http://ss64.com/nt/syntax-dequote.html
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

Offline

#6 12 Dec 2007 11:24

Nicholi
Member
Registered: 09 Dec 2007
Posts: 7

Re: Question About DeQuote Script

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)

Offline

#7 29 Dec 2007 06:08

mmojica
Member
Registered: 29 Dec 2007
Posts: 2

Re: Question About DeQuote Script

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%

Offline

#8 29 Dec 2007 13:04

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 939

Re: Question About DeQuote Script

You could probably get away with doing this:

@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  | KiX | AutoIT3 | PowerShell | Puppet

Offline

#9 29 Dec 2007 21:26

mmojica
Member
Registered: 29 Dec 2007
Posts: 2

Re: Question About DeQuote Script

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.

Offline

#10 26 Jan 2008 15:58

mrQQ
Member
Registered: 26 Jan 2008
Posts: 1

Re: Question About DeQuote Script

hello,

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

this is what i put into file:

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:

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? sad

Offline

#11 29 Jan 2008 13:07

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 939

Re: Question About DeQuote Script

This line...

) ELSE (GOTO :EOF)SET !_DeQuoteVar!=!_DeQuoteString!

...is the problem.  The SET command should be on a new line.


cmd | *sh  | KiX | AutoIT3 | PowerShell | Puppet

Offline

#12 01 Feb 2008 04:41

noa123450
Member
Registered: 01 Feb 2008
Posts: 2

Re: Question About DeQuote Script

How can I recognize the number of pc .
there is command that enable me to do that
thank ahead,
noa.

Offline

#13 01 Feb 2008 04:42

noa123450
Member
Registered: 01 Feb 2008
Posts: 2

Re: Question About DeQuote Script

How can I recognize the number of pc .
there is command that enable me to do that
thank ahead,
noa.

Offline

#14 01 Feb 2008 12:07

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 939

Re: Question About DeQuote Script

noa123450 wrote:

How can I recognize the number of pc .
there is command that enable me to do that
thank ahead,
noa.

Don't know what on earth you're banging on about ... please try to explain yourself a bit better!


cmd | *sh  | KiX | AutoIT3 | PowerShell | Puppet

Offline

#15 28 Feb 2008 23:10

simplyjaded
Member
Registered: 28 Feb 2008
Posts: 2

Re: Question About DeQuote Script

bluesxman wrote:

This line...

) 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:

::@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:

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??:(

Offline

#16 28 Feb 2008 23:34

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 777
Website

Re: Question About DeQuote Script

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 ]

Offline

#17 19 Mar 2008 10:57

klint
Member
Registered: 19 Mar 2008
Posts: 10

Re: Question About DeQuote Script

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:

@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)

Offline

#18 29 Mar 2008 13:05

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 777
Website

Re: Question About DeQuote Script

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

Offline

#19 02 Oct 2008 04:09

a1s2d3f4
Member
Registered: 02 Oct 2008
Posts: 2

Re: Question About DeQuote Script

Just tried the :DeQuote routine.:pc:

Here is what my bat file looks like:

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:

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.

Offline

#20 02 Oct 2008 04:33

a1s2d3f4
Member
Registered: 02 Oct 2008
Posts: 2

Re: Question About DeQuote Script

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.

Offline

Board footer

Powered by FluxBB