Why does this 'findstr' command not function as expected?

2

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?

CubicleSoft

Posted 2012-10-24T15:17:53.787

Reputation: 253

what OS are you using? – EBGreen – 2012-10-24T15:20:23.643

Windows 7. Plain ol' Command Prompt (i.e. no PowerShell). – CubicleSoft – 2012-10-24T15:23:06.403

I'll check it out, but why not use PS? – EBGreen – 2012-10-24T15:24:32.437

The tools of the basic Command Prompt are generally sufficient for my needs. – CubicleSoft – 2012-10-24T15:28:16.723

Answers

6

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:"\"^>" *

dsolimano

Posted 2012-10-24T15:17:53.787

Reputation: 2 778

This works fine. – Karan – 2012-10-24T23:02:22.730

+1 for the link to the extensive MSDN blog post. Marking as the answer because this works. – CubicleSoft – 2012-10-25T04:09:14.807

4

The character > is used for redirection and generally needs to be escaped with a caret if part of a literal string. Encapsulating the string in quotes will prevent metacharacters from being parsed and eliminates the need for escaping. That's why both echo ">" and echo ^> will print an actual greater than-sign instead of trying to redirect the output.

The second occurence of double quotes always marks the end of a literal string. It cannot be escaped, because the caret would not be parsed as a metacharacter; it is in between quotes. As a result, both echo "">" and echo "^">" will try to redirect the output, in this case to an invalid file.

In your first line, findstr /sic:"\">" *, the second double quotes are interpreted as the end of a literal string. The next character indicates a redirected output. The succeeding filename is indeed invalid; the error thrown is hardly cryptic. The remainder of the intended argument isn't even visible to the findstr-command, which only gets to execute /sic:"\". If you want to stick to double quotes, the following syntax would behave as intended: findstr /sic:""^>" *.

Marcks Thomas

Posted 2012-10-24T15:17:53.787

Reputation: 5 749

findstr /sic:""^>" * does not work in Win7! Did you try it yourself? – Karan – 2012-10-24T23:01:50.127

How does the command behave on your system? I indeed tried it myself, on a Windows 7 machine and can confirm it does work. – Marcks Thomas – 2012-10-24T23:13:28.010

1Win7 x64, created a bunch of text files, a few had "> in them while the rest didn't. Ran your command and just got a blinking cursor at the next line, as if it was waiting for input. I could type whatever I wanted, press Enter multiple times, but the only way back to the cmd prompt was via Ctrl+C/Break. No output at all. – Karan – 2012-10-24T23:17:52.907