Computing.Net > Forums > Programming > pipe into a dos for loop

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

pipe into a dos for loop

Reply to Message Icon

Name: camgian
Date: April 8, 2009 at 10:08:43 Pacific
OS: DOS
Subcategory: Batch
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: IVO
Date: April 8, 2009 at 13:13:20 Pacific
Reply:

Prefix the pipe using the caret char (^) so ^| and maybe you want ed to post For /F "delims=" not just For.


0

Response Number 2
Name: camgian
Date: April 9, 2009 at 08:45:18 Pacific
Reply:

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:

"'pkunzip
-c
c:\test\myfile|ssed
-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"'
%var%

... 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 :(



0

Response Number 3
Name: Judago
Date: April 9, 2009 at 09:44:36 Pacific
Reply:

Try also adding a caret to all the brackets in your command ^(^), except the one before do and after in.


0

Response Number 4
Name: IVO
Date: April 9, 2009 at 09:55:54 Pacific
Reply:

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.


0

Response Number 5
Name: camgian
Date: April 10, 2009 at 02:53:48 Pacific
Reply:

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"
%var%))

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...


0

Related Posts

See More



Response Number 6
Name: camgian
Date: April 10, 2009 at 03:12:56 Pacific
Reply:

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?


0

Response Number 7
Name: IVO
Date: April 10, 2009 at 06:38:20 Pacific
Reply:

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.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Rename Files /Add Reg Fil... boot up



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: pipe into a dos for loop

DOS for loop www.computing.net/answers/programming/dos-for-loop/17251.html

output to file in a DOS for loop www.computing.net/answers/programming/output-to-file-in-a-dos-for-loop/19938.html

VBA Referencing a cell for a do until loop www.computing.net/answers/programming/vba-referencing-a-cell-for-a-do-until-loop/19866.html