Batch script with for loop and pipe Batch script with for loop and pipe windows windows

Batch script with for loop and pipe


You need to escape the | character to prevent its being interpreted at the time of parsing the loop command. Use ^ to escape it:

FOR /f "delims=" %%A in ('dir /b "my_dir\*.csv" ^| find /V "summary"') do (rem do what you want with %%A here)

Once escaped, the | becomes part of the '-delimited string. It is only interpreted as a special symbol when that string is parsed separately from the loop, as a "sub-command", according to the syntax. And that is done after parsing the loop.


If you get the problem that Gilbeg got "find: /V': No such file or directory" then it's most likely you have cygwin, or similar, in your path and the batch file's not using the Windows find command. If you modify your script to use the absolute path of the Windows find then the error will go away:

FOR /f "delims=" %%A in ('dir /b "my_dir\*.csv" ^| %SYSTEMROOT%\system32\find.exe /V "summary"') do (rem want to do something here with %%A)


You can also just embed a double-quoted string inside the single-quotes string, as in:

FOR /f "delims=" %%A in ('"dir /b my_dir\*.csv | find /I /V "summary""') do @(    ECHO Do something with "%%A")