Why does this ‘findstr’ command not function as expected

command linefindstr

I use the command-line program 'findstr' frequently as I write software. It helps me quickly locate all the files that contain a specific string in a set of subdirectories. It is a lot faster to use 'findstr' than anything else in a lot of cases. Today I ran into an issue where I wanted to find the string ">, so I ran this command (what I considered to be a fairly typical escaped string):

findstr /sic:"\">" *

And got back this cryptic error message: "The filename, directory name, or volume label syntax is incorrect."

Changing it to:

findstr /sic:'\">' *

Works properly. Why did I need to use single quotes instead of double quotes? I've run hundreds (maybe thousands?) of findstr commands before where I've escaped double quotes inside a double quote wrapper without any issue. What makes this particular search string so different?

Best Answer

There are two levels of processing going on here - first cmd.exe is parsing the command line to determine if redirection, etc needs to happen, and then after that, findstr gets the remaining command line and does its own parsing according to its own rules (in practice, usually the same for various programs).

This MSDN blog post goes into some detail on the subject.

It seems to me that, since > is a cmd.exe metacharacter, what you need is the cmd.exe escape character, which happens to be ^. For example, this command works for me:

findstr /sic:"\"^>" *
Related Question