Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How do I pipe to the for command. So instead of

    C:\asd>ipconfig | findstr "Address" >a

   C:\asd>for /f "tokens=15" %f in (a) do @echo %f
   192.168.1.3

I would like to be able to do it on one line.

C:\asd>ipconfig | findstr "Address" | for /f "tokens=15" %f do @echo %f
do was unexpected at this time.
share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

Looking at the documentation for for (run for /? to see it), I don't think for /f will read from a pipe. The only valid sources are file-set, "string", and 'command'.

FOR /F ["options"] %variable IN (file-set) DO command [command-parameters]
FOR /F ["options"] %variable IN ("string") DO command [command-parameters]
FOR /F ["options"] %variable IN ('command') DO command [command-parameters]

You could put the command inside the for like this:

for /f "tokens=15" %f in ('ipconfig ^| findstr "Address"') do @echo %f

192.168.x.x
192.168.y.y

(thanks to neurolysis for pointing out it has to be ^| and not just |)

Or you could try putting ipconfig | findstr "Address" in a separate batch script and calling:

for /f "tokens=14" %f in ('ipaddresses') do @echo %f

(assuming you called the script ipaddresses).

Or you could use for to do the work of findstr too, like this:

for /f "tokens=2,14" %i in ('ipconfig') do @if %i==Address. @echo %j

192.168.x.x
192.168.y.y

it's not quite the same because it won't match IPv6 addresses, but something along those lines could work.

Also note that I changed tokens=15 to tokens=14. I think the last field on the line is token 14. Works for me anyway.

share|improve this answer
    
If you look at my question, you'll see I know about using a temp file, that was my example, I just called the file 'a'. With what you say at the start though, you have a point, the closest to doing the pipe.. is putting the command in the "in". And I suppose that since one can do that, that might explain why one can't use a pipe to for. It looks like one can't use a pipe to it. Interestin also you mention the term pipeline.. I hadn't heard the pipe called that.. but a google shows it. –  barlop Jan 16 '11 at 23:59
2  
For the record, the reason it "doesn't seem to support pipelines" is simply because you haven't escaped it, it is piping for instead of ipconfig. Just do for /f "tokens=15" %f in ('ipconfig ^| findstr "Address"') do @echo %f, when ^ is the escape character. –  Matthieu Cartier Jan 17 '11 at 0:06
    
Thanks neurolysis, will update my answer to include that. –  Mikel Jan 17 '11 at 0:09
    
Well I meant piping to For, like before it. But i'm convinced one can't. But can in the "in" as in Mikel's suggestion. But trying Mikel's line, I did run into a problem with Mikel's line.. and you(neurolysis) fixed that by escaping the pipe. –  barlop Jan 17 '11 at 0:10
    
@barlop, yes, I will remove the tempfile suggestion. "pipeline" is a UNIX term meaning a series of commands with pipes in the middle. –  Mikel Jan 17 '11 at 0:15
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.