pipe into a dos for loop

April 8, 2009 at 10:08:43
Specs: DOS
I'm trying to retrieve a pattern from an output which I get to console: so I use a for loop to take in input this output from the console. The issue is I cannot use the result of 2 command pipelined as "list" for the for loop, because I get an error on "| not allowed". How can I solve this? I tried all.. :(
Here the code:

for %%a in ('pkunzip -c c:\test\%file%|ssed -n "s/.*\(....*\)..*\(%var%\).*/\2/p"') do echo %%a

See More: pipe into a dos for loop

Report •

April 8, 2009 at 13:13:20
Prefix the pipe using the caret char (^) so ^| and maybe you want ed to post For /F "delims=" not just For.

Report •

April 9, 2009 at 08:45:18
Thanks for reply IVO !!!

sorry but again does not work... if I add the ^ :

"for %%a in ('pkunzip -c c:\test\%file%^|ssed -n "s/.*\(....*\)..*\(%var%\).*/\2/p"') do echo %%a" I get:

-n" as error.

And if I add /F "delims=" to my for command :

"for /F "delims=" %%a in ('pkunzip -c c:\test\%file%^|ssed -n "s/.*\(....*\)..*\(%var%\).*/\2/p"') do echo %%a" I get no output (not a blank, just no output,strange, isn't it?...)

However, I get it works in this manner:

set var=pkunzip -c c:\test\%file%^|ssed -n "s/.*\(....*\)..*\(%var%\).*/\2/p"'

... and it works... showing me on console my pattern...but again a problem: this batch is called by another batch using a "for loop" passing the filename(zipped) retrieved from a folder to the second batch: now, the first filename is processed fine, then the second one... loop!!! 50% of my dual core cpu used and cmd hanged-up!!! :((

ok, I found ssed hangs where no pattern is found... i will work on this :(

Report •

April 9, 2009 at 09:44:36
Try also adding a caret to all the brackets in your command ^(^), except the one before do and after in.

Report •

Related Solutions

April 9, 2009 at 09:55:54
Your post is not clear to me as your solution too. Pay attention in using environment variables in For loops as often the pittfall is the "delayed expansion" not enabled by default that is required to get the values to dynamically change while running. Maybe that is not your problem, anyway a clear post of parameters tramsmission from first to second batch is needed to diagnose the hillness.

Report •

April 10, 2009 at 02:53:48
Hi, IVO, and again thanks for your time.
Maybe my english is too bed for you.. or written a bit confused...sorry!!
For all interested I have some update:
this is in first batch:
set /p param1=
set /p param2=
for /f "tokens=1" %%a in ('dir /b /t:w c:\dir\%param1%*.*') do (
call c:\test\batch2.bat %%a %param2%
Then in batch2 I have:
set file=%1
for /F "tokens=*" %%t in ('pkunzip -c c:\dir\%file%^|sed -n "s/.*\(%param2%\).*/\1/p"') do (
if %%t NEQ '' (
dir c:\test\%file%|sed -n "/%file%/p"
set var=pkunzip -c c:\test\%file%^|sed -n "s/.*\(...\)..*\(%param2%\).*/ \2 \1/p"

So, in this situation, all works except something strange: I get for the 2 value retrieved when I execute %var% for every occurrence the value in the previous file processed.
And if I execute the procedure a second time consecutively, it appen the first two value for the first file found with the pattern I search for, are exactly the last two value of my last batch execution. It seems a variable problem... Now this is the only difficoulty for the perfect functionality....

Can the "delayed expansion" help to solve?

sorry for long post...

Report •

April 10, 2009 at 03:12:56
Just solved with "delayed expansion" activated: everything works well!
Many thanks Ivo and Judago for help, I'm going to edit title ...

- mmm... - How can I edit the title of this post to write down a [SOLVED] ?? Can I did it?

Report •

April 10, 2009 at 06:38:20
You can edit your posts in the thread NOT the thread's title.

You are very good in enabling the delayed expansion and the replacing the variables' markers with ! where required. Rarely people success in that without srict guidance.

A last tip: avoid too complex clause in For statement as the one you posted (pkunzip ...) as they may lead to tricky code.

Report •

Ask Question