Is it possible to pipe an output to a for loop?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Is it possible to pipe an output to a for loop?

#1 Post by orange_batch » 01 Aug 2010 17:40

Hi guys, new here. 8)

For example, I have this here:

dir >dir.txt
for /f "tokens=5" %%a in (dir.txt) do @echo %%a


...but I'd like to do this if I can:

dir | for /f "tokens=5" %%a in (output of dir) do @echo %%a


Thanks for your help.

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

Re: Is it possible to pipe an output to a for loop?

#2 Post by aGerman » 01 Aug 2010 17:54

You can process a command into a for loop. Use apostrophes into the brackets.

Code: Select all

for /f "delims=" %%a in ('dir /b') do echo %%a

Option /b means that only the filenames are outputed, no header.

Regards
aGerman

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Is it possible to pipe an output to a for loop?

#3 Post by orange_batch » 02 Aug 2010 09:52

Ahh, thanks again. I'm very rusty. :roll:

luca
Posts: 2
Joined: 05 Aug 2010 04:44

Re: Is it possible to pipe an output to a for loop?

#4 Post by luca » 07 Aug 2010 12:21

echo off
set a1=1
set b1=2
set c1=1
for %%x in (%a1%,%b1%,%c1%) do echo %%x > pippo.txt| set /a x=%%x+1 :)
Last edited by luca on 09 Aug 2010 12:29, edited 1 time in total.

luca
Posts: 2
Joined: 05 Aug 2010 04:44

Re: Is it possible to pipe an output to a for loop?

#5 Post by luca » 09 Aug 2010 12:22

rem example pipe
echo off
:10
set /a a1=%random:~1,1%
set /a b1=%random:~1,1%
set /a c1=%random:~1,1%
set /a d1=%random:~1,1%
for %%x in (%a1%,%b1%,%c1%,%d1%) do for %%y in (%a1%,%b1%,%c1%,%d1%) do echo %%z > pippo.txt| set /a z=%%x*%%y
goto 10

Post Reply