Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMD: Iterate stdin piped from another command

Tags:

windows

cmd

A recent question contains a problem that I many times used to think about but still do not know if it has an elegant solution:

Suppose, I have a command that forms a list of filenames or directories: dir /B /AD. Then, I would like to pipe the result of the command to an iterator that would do something with each filename.

How can I do it?

Instead of piping, one can certainly use backtick version of the for:

for /F "usebackq" %F in (`dir /B /AD`) do @echo %F

But I am interested here in getting the data from stdin, so that I would not hardcode the source of the filenames.

like image 362
newtover Avatar asked Oct 23 '25 15:10

newtover


1 Answers

I happen to have found a solution myself:

dir /B /AD | for /F "usebackq" %F in (`findstr "."`) do @echo %F

This is even better than I thought, because it allows filtering directly in backticks expression. For example to exclude some particular filenames (.svn and _lib) I can write:

dir /AD /B | for /F "usebackq" %F in (`findstr /V "\.svn _lib"`) do @echo %F
like image 132
newtover Avatar answered Oct 26 '25 05:10

newtover



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!