Problem with output from this FOR command

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

Answer

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.

Attribution
Source : Link , Question Author : barlop , Answer Author : Vlastimil Ovčáčík

Leave a Comment