Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I just realized I'm executing a bat file at the start at this registry key, that had echo running abc and some other irrelevant things

HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun

So here I've modified that bat file.. it is just one line nothing irrelevant in it.

you see the contents of the bat file.

Here is some output from the cmd prompt showing the situation now which is the same fundamental problem but you should be able to reproduce the problem

The question now is,

  • Why does it say echo sss and not echo sss ttt?
  • How can I suppress the running of cmd or that initialization bat file with the FOR? The FOR statement should really just display "echo g", not "echo sss" or "echo sss ttt".

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
sss ttt
C:\Documents and Settings\Administrator>cd\

C:\>type \blah\startfile.bat <ENTER>
@echo sss ttt   

C:\>  

C:\>for /f %f in ('echo g ^| findstr "g"') do echo %f <ENTER>

C:\>echo sss
sss

C:\>echo g
g

C:\>
share|improve this question
2  
Can't reproduce. What version of windows is that? –  Mat Jul 31 '11 at 18:00
    
I got "The syntax of the command is incorrect." –  Simon Sheehan Jul 31 '11 at 18:25
1  
It works as expected for me (Win 7) - no mystery "echo running" command. –  Ƭᴇcʜιᴇ007 Jul 31 '11 at 18:56
    
Out of curiosity, what is the purpose of the caret in your echo line? –  jaquer Jul 31 '11 at 19:15
    
@techie007 xp but just tried it on another machine and it's only on one computer –  barlop Jul 31 '11 at 20:46

3 Answers 3

Why does it say echo sss and not echo sss ttt?

Because the line sss ttt contains a space character. The for command tokenize it and stores first half sss in %f and second half ttt in %g variable.

How can I suppress the running of cmd or that initialization bat file with the FOR? The FOR statement should really just display "echo g", not "echo sss" or "echo sss ttt".

Seems like in order to evaluate echo g ^| findstr "g" a new command interpreter is initialized and your \blah\startfile.bat is automatically executed. Make your batch not to output anything.

share|improve this answer

If you're going to use %f in a batch file, you need to escape it with another %:

for /f %%f in ('echo g ^| findstr "g"') do echo %%f

Put that in your batch file and you should get the results you're looking for.

share|improve this answer
    
The FOR was not in a batch file. Try reading the question more carefully. Look at the dump from the command prompt. –  barlop Nov 3 '11 at 19:06
    
Whoops. Yep, I see that now. I'll update my answer... –  treehead Nov 8 '11 at 17:58

Just to expand a bit on Vlastimil's answer...

When the FOR command executes the "echo g" and pipes the output to findstr, FOR has to first start a new command processor which causes \blah\startfile.bat to be executed.

\blah\startfile.bat outputs a line containing sss ttt. That line is processed by FOR and is NOT passed to findstr.

According the the help for FOR:

Processing consists of ..., breaking it up into individual lines of text and then parsing each line into zero or more tokens. The body of the for loop is then called with the variable value(s) set to the found token string(s). By default, /F passes the first blank separated token from each line...

The default delimiters for FOR are: " ,;" (space comma and semi-colon).

The first line that FOR processes contains the string: sss ttt. That string contains a " " (space), which is a default delimiter for FOR, which parses the line and assigns the first token string: "sss" to "%f". So the "echo %f" outputs a line with only: "sss". That is the first line you see printed. The "ttt" is simply discarded because the "FOR ... echo %f" command did not specify to include anything more than the first token.

If the FOR command was written differently, you would have seen the full line "sss ttt" echoed.

Here are 3 ways the FOR command could have been written to do that.

The first way, using the "delims=" option, sets the delimiter for FOR to "empty", so FOR returns the entire line as 1 token. The tokens are still parsed, and all delimiters (" ,;") are removed, then all the tokens are combined again with a space between each token. This combined value is returned in the variable "%f"

C:\>for /f "delims=" %f in ('echo sss ttt')  do echo %f

C:\>echo sss ttt
sss ttt

C:\>for /f "delims=" %f in ('echo mm;nn,oo, ;ss ,, tt') do echo %f

C:\>echo mm nn oo ss tt
mm nn oo ss tt

This next way, using the "tokens=*" option, tells FOR to put all of the tokens found into the value of the specified variable ("%f"). The tokens are still parsed, and all delimiters are removed, then all the tokens are combined again with a space between each token.

C:\>for /f "tokens=*" %f in ('echo sss ttt')  do echo %f

C:\>echo sss ttt
sss ttt

C:\>for /f "tokens=*" %f in ('echo mm;nn,oo, ;ss ,, tt') do echo %f

C:\Users\1-kevin>echo mm nn oo ss tt
mm nn oo ss tt

This third way, using the "tokens=1,*" option, tells FOR to put the first (1) token found into the value of the specified variable ("%f"). And, the remaining tokens are parsed, and all delimiters are removed, then all those remaining tokens are combined again with a space between each token, and this combined value is returned in the variable named with the letter that follows the specified variable name (so in this case, variable "%g").

C:\>for /f "tokens=1,*" %f in ('echo sss ttt')  do echo %f %g

C:\>echo sss ttt
sss ttt

C:\>for /f "tokens=1,*" %f in ('echo mm;nn,oo, ;ss ,, tt') do echo "%f" "%g"

C:\>echo "mm" "nn oo ss tt"
"mm" "nn oo ss tt"

Note that the following will not work because there are no "tokens=" defined for variables beyond the specified variable of "%f":

C:\>for /f %f in ('echo sss ttt')  do echo %f %g

C:\>echo sss %g
sss %g


So, in your example, if you added the "delims=" option, you would see this:

C:\>for /f "delims=" %f in ('echo g ^| findstr "g"') do echo %f

C:\>echo sss ttt
sss ttt

C:\>echo g
g

C:\>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.