32

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!

aschipfl
  • 33,626
  • 12
  • 54
  • 99
Gilbeg
  • 741
  • 2
  • 9
  • 19
  • 1
    there's no `for /f` in DOS. [cmd.exe and DOS are completely different things](https://superuser.com/q/451432/241386) – phuclv Dec 02 '17 at 09:55

4 Answers4

67

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.

foxidrive
  • 40,353
  • 10
  • 53
  • 68
Andriy M
  • 76,112
  • 17
  • 94
  • 154
  • 1
    Nope! I got `find: `/V': No such file or directory find: `summary': No such file or directory` – Gilbeg May 17 '11 at 06:39
  • @Gilbeg: Works for me. Are you sure you didn't miss anything? Does `my_dir\*.csv` contain spaces? If so, is it enclosed in double quotes? Although that would still hardly produce that error message. – Andriy M May 17 '11 at 12:13
7

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
)
foxidrive
  • 40,353
  • 10
  • 53
  • 68
kxs
  • 81
  • 1
  • 2
  • 2
    Welcome to StackOverflow. It's great that you would like to contribute. However, this is a general answer. It's much better if you tailor your answer to the question asked. Please update accordingly. – kkuilla Sep 22 '14 at 11:41
1

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"
)
David Rogers
  • 4,010
  • 3
  • 29
  • 28
-4

have a look at the Windows PowerShell. Not that I have ever used it myself, mind.

Philip Sheard
  • 5,789
  • 5
  • 27
  • 42
  • Unfortunately PowerShell is the path that the client wants to go. – Gilbeg May 17 '11 at 06:00
  • Powershell works well, but as a note I've previously experienced instabilities when calling from bat files (e.g. call powershell.exe -command "blah blah blah"). This was largely related to automated testing where the PS subshell would never return (despite an "Exit" command). We didn't have time to fully investigate, but I thought I'd mention it. Maybe it will save someone else the headache of debugging :) – TwoByteHero Mar 14 '13 at 17:11