I would like all the csv files in a directory which filename does not contain word "summary". Inside the command prompt I can type the following command
dir /b my_dir\*.csv | find /V "summary"
When I try to transfer the above command into a batch file I run into a problem in that the pipe command is not supported in the for loop. That is I cannot do the following
FOR /f %%A in ('dir /b my_dir\*.csv | find /V "summary"') do ( rem want to do something here )
Can somebody shed some light to me on how to solve the problem above?
Thanks in advance!
|| (not used above) executes this command only if previous command's errorlevel is NOT 0 > output to a file >> append output to a file < input from a file | output of one command into the input of another command ^ escapes any of the above, including itself, if needed to be passed to a program " parameters with spaces ...
When used in a command line, script, or batch file, %1 is used to represent a variable or matched string. For example, in a Microsoft batch file, %1 can print what is entered after the batch file name.
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 )
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