Avoiding quotation in findstr search bar.

How can I correctly avoid quotes in the search bar when using findstr.exe?

Example:

findstr /misc:"namespace=\"" *.cs > ns.txt 

This is displayed on the console, not on the file I specified.

I do this directly on the command line, not in a batch file, although this information may also be useful.

+8
escaping quotes batch-file findstr
source share
4 answers

Please correct me if I am wrong, but I think I understood:

 findstr.exe /misc:^"namespace=\^"^" *.cs > ns.txt 

This seems to give the correct output, even if there are spaces in the search bar. This allows you to redirect files, pipes, and additional literals in the same findstr.exe call to work correctly.

The original command in my question does not work, because both cmd.exe and findstr.exe have special processing for the character. " I ended up with an unsurpassed set of quotes in the cmd.exe processing.

The new command in my answer works because ^" allows you to pass a quote from cmd.exe to findstr.exe, and \" tells findstr.exe to ignore this quote for command processing purposes and treat it as a character literal.

Edit

Well, my decision was right, but the reason that it was right was completely wrong. I wrote a small program for testing.

I found out that cmd.exe passes this input to the program when I pass a bad command line:

 test.exe /misc:namespace=" *.cs > ns.txt 

If the characters are reset correctly, cmd.exe passes this input to the program (and redirects the output to a file):

 test.exe /misc:namespace=" *.cs 
+6
source share

Found Re: FINDSTR searches for a double quote and redirects / outputs the result

 Try this: findstr > x.txt /S /I /M /C:"\.\"" * 

I have no idea why this works.

Does not work for the output pipeline. See the linked findstr trace output link.

+3
source share

It would not be enough:

 findstr /misc:namespace=^" *.cs > ns.txt 

?

EDIT

If you were looking for a way to pass the character " inside the specified parameter, then it could be (using your example)

 findstr /misc:"namespace=""" *.cs > ns.txt 

(the character " repeated twice inside the quoted string).

+1
source share

According to my criteria, the correct return character is the backslash:

 c:\Temp>findstr /isc:"session id=\"59620\"" C:\Temp\logs\some*.xml C:\Temp\logs\some_2016_11_03.xml: <session id="59620" remoteAddress="192.168.195.3:49885"/> 
+1
source share

All Articles