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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With