pass the output of a batch file into a for loop - for-loop

I am trying to pass the output of a process into a for loop by using pipes
type %1% | findstr /R /V "Test" | for /F "tokens=*" %%i IN ('more') DO #echo %%i
but I do not know what to put in place of ('more') so that it reads the output from the findstr command. Is this even possible? Or do I have to save the output to a file and then read in the file in an entirely different batch program? Please help.

for /f "delims=" %%a in ('findstr /rv "Test" "%1%" ^| more') do echo %%a

for loops cannot read from STDIN, so you need to put the command whose output you want to process into the parantheses:
for /F "tokens=*" %%i IN ('type %1% ^| findstr /R /V "Test"') DO #echo %%i
Note that pipes must be escaped in the subshell (^|).

Related

How to use parameters passed to a batch file in a for loop?

Please consider the following...
This works:
for /f %%A in ('Dir /b *ipc10*.JPG') do copy %%A "%cd%\!sd!"
The goal from the cli> file.bat *ipc9* or *ipc10* or *ipc6* or etc.
... to get passed to the for command, instead of hard-coding each argument\parameter within the batch file.
I'm able to use echo %1, echo %2, etc. However %1 nor %%1, will work as expected within that parenthesized command.
i.e.
for /f %%A in ('Dir /b %1 OR %2 .JPG') do copy %%A "%cd%\!sd!"
for /f %%A in ('Dir /b %%1 OR %%2 .JPG') do copy %%A "%cd%\!sd!"
... will not expand to the intended contents
Please advise.

Batch script for loop

I have a problem with for loop in batch script.
When I try:
for /f "delims=;" %g in ('dir') do echo %g%
i see this
'dir' is not recognized as an internal or external command,
operable program or batch file.
Did I miss somethig? Why windows command doesn't work?
Don't do that. You get all kinds of problems iterating over dir output with for /f. Instead just use
for %g in (*) do #echo %g
In your question you stated echo %g% which is wrong (the trailing % will be returned literally).
When the command is directly typed into command prompt, use:
for /f "delims=" %g in ('dir /B') do echo %g
When you are using for within a batch file you need double-% for its variable:
for /f "delims=" %%g in ('dir /B') do echo %%g
The option delims=; makes no sense as dir does not give a semicolon-separated list, so I deactivated delims.
The /B switch changes the dir output to bare format (no headers ad footers, only files and dir.s).
It looks like the %comspec% variable has been nulled or changed and it can't find cmd.exe
Test this to see the error, and remove line 2 for it to work fine, on a working machine.
#echo off
set comspec=c:\aaa
for /f "delims=;" %%g in ('dir') do echo %%g
pause
Something that is very interesting is that the path to cmd.exe is cached.
The two scripts below are the same with only the order of the commands changed
This batch file will fail with both for commands
#echo off
set comspec=c:\aaa
for /f "delims=;" %%g in ('dir') do echo %%g
pause
set "comspec=%windir%\System32\cmd.exe"
for /f "delims=;" %%g in ('dir') do echo %%g
pause
and this batch file will work with both for commands.
#echo off
set "comspec=%windir%\System32\cmd.exe"
for /f "delims=;" %%g in ('dir') do echo %%g
pause
set comspec=c:\aaa
for /f "delims=;" %%g in ('dir') do echo %%g
pause

Find text in file and set it as a variable. Batch file

I try to find one line in whole text file. Next I need to set this line as a variable.
When I try do this:
set MY_VARIABLE=findstr /I "MY_TEXT" MY.FILE
echo %MY_VARIABLE%
The result of echo is findstr /I "MY_TEXT" MY.FILE, but I want to see result of this command line instead.
When I try do this – first enter in cmd:
for /F "delims=" %%a in ('findstr /I "MY_TEXT" MY.FILE') do set "batToolDir=%%a"
then enter in cmd:
echo "%batToolDir%"
I see an error:
the %%a variable is unsuspected
When I make a file SCRIPT.bat:
#echo off
for /F "delims=" %%a in ('set MY_VARIABLE=findstr /I "MY_TEXT" MY.FILE') do set "batToolDir=%%a"
echo "%batToolDir%"
I get this:
""
What is wrong? How to make this?
Almost done
For command line
for /F "delims=" %a in ('findstr /I "MY_TEXT" MY.FILE') do set "batToolDir=%a"
For batch file double the percent signs
for /F "delims=" %%a in ('findstr /I "MY_TEXT" MY.FILE') do set "batToolDir=%%a"

The ECHO command echo only show the last line of my file instead of shows each line

I want to show out of the console each line from a file. I try this:
findstr /v /b /c:" " <%1>toto
for /f "tokens=*" %%a in (toto) do set co=%%a
echo. %co%
Also this one:
findstr /v /b /c:" " <%1>toto
for /f "tokens=*" %%a in (toto) do (set co=%%a
echo. %co%
)
But the first way shows me only the last line of file and the second one doesn't show me anything.
Someone can explain me what's wrong in these latter and how to get in the 'co' variable each line printed on the console?
Thanks
you override co. to append to it try
set co=%co% %%a
Try this:
for /f "tokens=1*delims=:" %%a in ('findstr /n "^" "toto"') do echo %%b

Output of FindStr to a variable

I want to keep the output of the following command to a variable.
corflags ICEConnectDT.dll | findstr "PE"
When I tried the following statement, it shows the error "| was unexpected at this time."
for /F "delims=" %%a in ('corflags ICEConnectDT.dll | findstr PE') do echo %%a
How can I solve the issue?
Escape the pipe
for /F "delims=" %%a in ('corflags ICEConnectDT.dll ^| findstr PE') do echo %%a
Or enclose the entire command string in double quotes (inside the single quotes)
for /F "delims=" %%a in ('"corflags ICEConnectDT.dll | findstr PE"') do echo %%a

Resources