joehua
joehua

Reputation: 735

put the find command in a batch file

The following command works fine in a CMD window. It lists directories that contains the word "NOTE" but not "NOTES".

dir c:\myfiles\mydirectories /s /b /ad |find "NOTE" | find "NOTES" /v

When I put the command in a batch file, CMD complains invalid switch -v.

dir c:\myfiles\mydirectories /s /b /ad ^|find "NOTE" ^|find "NOTES" /v

What did I do wrong? Thanks.

This is on Windows 7.

Upvotes: 1

Views: 14937

Answers (2)

Parag Doke
Parag Doke

Reputation: 863

Are you:

  1. Escaping pipes when you put this in a batch file as a command?
    If so, don't escape the pipes.

    dir c:\myfiles\mydirectories /s /b /ad |find "NOTE" |find "NOTES" /v
    
  2. Having the search invokes within a sub-shell?
    If so, retain escape characters on the pipes. I mean something like:

    for /f "delims=" %%a in ('dir c:\myfiles\mydirectories /s /b /ad ^|find "NOTE" ^|find "NOTES" /v') do echo Something
    
  3. Having your command saved in a batch file which is invoked in a sub-shell?
    If so, discard the escape characters. I mean:

    for /f "delims=" %%a in ('call <path to batch file containing your command>') do echo Something
    
  4. If none of the above apply, is your error showing up when this line is part of a larger batch file? If so, can you please share it entirely?

Upvotes: 5

Endoro
Endoro

Reputation: 37569

with findstr you need only one pipe:

dir c:\myfiles\mydirectories /s /b /ad|findstr "\<NOTE\>"

or with a for loop:

for /f "delims=" %%a in ('dir "c:\myfiles\mydirectories" /s /b /ad^|findstr "\<NOTE\>"') do echo %%~a

\< is the Regex for the start of a word
\> is the Regex for the end of a word
see help findstr for more help.

Upvotes: 4

Related Questions