Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 10 down vote accepted

Although it doesn't appear to be documented, you can pipe to the command inside the FOR command. I first learned about it from this example:

SC query | FOR /F "tokens=2" %%i IN ('FIND /I "SERVICE_NAME"') DO @ECHO %%i

This modified version of your code works:

C:\>ipconfig | for /f "tokens=14" %f in ('findstr "Address"') do @echo %f
192.168.x.x
192.168.y.y

You can also use "more" inside the FOR command, as described in this answer, to parse input piped in from an arbitrary command:

C:\>ipconfig | findstr "Address" | for /f "tokens=14" %f in ('more') do @echo %f
192.168.x.x
192.168.y.y
share|improve this answer

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .