How can I search for quotes in a for loop with findstr?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Andi
Posts: 5
Joined: 23 Jul 2010 07:46

How can I search for quotes in a for loop with findstr?

#1 Post by Andi » 11 Aug 2010 09:13

Hello,
in general I could use findstr to search for quotes:

Code: Select all

findstr /C:""" "someFilecontainingQuotes"

Within a script this would work:

Code: Select all

echo !amPar_Servername!|findstr /C:"""

And this would work because of the even number of quotes in the for-loop:

Code: Select all

for /f "delims== tokens=1,*" %%i in ('findstr /C:"a" "!ParamTestLog!"') do (
   echo PI=%%i
   )

But this does not work because the for-Loop does not allow for an uneven number of quotes which findstr does allow:

Code: Select all

for /f "delims== tokens=1,*" %%i in ('findstr /C:""" "!ParamTestLog!"') do (
   echo PI=%%i
   )
So it seems I cannot use findstr in a for-loop.
Would anyone have a workaround?
I need to make sure that a certain file - which contains Parameters set by users of my script - does NOT contain any Quotes.
This logic should be part of my script.

Thank you
Andi

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How can I search for quotes in a for loop with findstr?

#2 Post by aGerman » 11 Aug 2010 14:06

Seems you use EnableDelayedExpansion. This is an advantage in your case. Save the quote to a variable and use it into the FOR loop.

Code: Select all

set quote="
for /f "delims== tokens=1,*" %%i in ('findstr /C:"!quote!" "!ParamTestLog!"') do (
   echo PI=%%i
   )


Why this works? Well, I don't know it exactly. Seems that normal variables will expand before the line is processed by cmd.exe. If you use EnableDelayedExpansion the variable is expandet at the same time the value is needed. IMO this protects you from the syntax error.

Regards
aGerman

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: How can I search for quotes in a for loop with findstr?

#3 Post by jeb » 11 Aug 2010 17:00

aGerman wrote:Why this works? Well, I don't know it exactly. Seems that normal variables will expand before the line is processed by cmd.exe


Yes, the expansion of ! is nearly the latest interpreter phase for a line.

I have made some experiments ...

The phasese are:
    Expanding of %var%
    Parse the first two tokens of a line (you can see it with rem)
    Block begin "("
    Command and second token control REM, call and so on
    Expanding/inflating of ^ multiline and escape characters (so you can not escape a %)
    Interpreting "&" to multiple lines
    Interpreting "&&", "||"
    Interpreting "|" Pipe
    Expanding of for-loop-vars like %%a
    echo command Phase(if "echo on" is enabled), so you will see !var! but the content of %var%
The next is only true if EnableDelayedExpansion is active
    Expanding of !var!
    If there is a ! in the line (in any step till here) do the expansion of ^ a second time, now also in ""

Example

Code: Select all

set "var=!time!"
setlocal EnableDelayedExpansion
echo %var%

Results in the current time, because first the %var% is expanded and later the !time! is also expanded

call: expands first all %%var%% and then doubles all ^ in "" and nothing else, but ...

call/goto label: the next label with the name is searched in the file, without expanding/interpreting anything

Code: Select all

set var=hello
:%var%
So the name of the label is ":%var%" not ":hello"
But

Code: Select all

call :%var%

will search for a label named ":hello"

hope it helps
jeb

Andi
Posts: 5
Joined: 23 Jul 2010 07:46

Re: How can I search for quotes in a for loop with findstr?

#4 Post by Andi » 12 Aug 2010 06:51

Thank you.
Regards

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How can I search for quotes in a for loop with findstr?

#5 Post by aGerman » 12 Aug 2010 10:30

jeb wrote:The phasese are:

Expanding of %var%
Parse the first two tokens of a line (you can see it with rem)
Block begin "("
Command and second token control REM, call and so on
Expanding/inflating of ^ multiline and escape characters (so you can not escape a %)
Interpreting "&" to multiple lines
Interpreting "&&", "||"
Interpreting "|" Pipe
Expanding of for-loop-vars like %%a
echo command Phase(if "echo on" is enabled), so you will see !var! but the content of %var%

The next is only true if EnableDelayedExpansion is active

Expanding of !var!
If there is a ! in the line (in any step till here) do the expansion of ^ a second time, now also in ""


Thanks jeb. I saved this to a text file. This is one of the most important things to understand how cmd works.

Regards
aGerman

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: How can I search for quotes in a for loop with findstr?

#6 Post by jeb » 13 Aug 2010 02:45

I forgot one thing.

Expansion in a for loop

Code: Select all

setlocal DisableDelayedExpansion
set a=!b!
set b=%%c%%
set c=!d!
set d=Ende
setlocal EnableDelayedExpansion
FOR /F "tokens=* delims=" %%M IN ('echo %a%') do (
  echo output=%%M
)

RESULT:
output=Ende


or

Code: Select all

set "var=hello ^& echo there"
echo var is = %var%
FOR /F "tokens=* delims=" %%M IN ('echo %var%') do (
  echo output=%%M
)

RESULT:
var is = hello & echo there
output=hello
output=there


The command block is completly interpreted two times with all phases

Post Reply