Page 1 of 1

findstr /C:""" file.txt&&set found=1 FAILS

Posted: 12 Feb 2019 02:59
by miskox
Hello!

I have .txt file: file.txt

Code: Select all

Some text
Some more text with " quote
This works:

Code: Select all

findstr /C:""" file.txt
This fails:

Code: Select all

findstr /C:""" file.txt&&set found=1
returns:

Code: Select all

c:\findstr /C:""" file.txt&&set found=1
FINDSTR: Cannot open file.txt&&set
FINDSTR: Cannot open found=1
If I want to redirect output to a file:

Code: Select all

c:\findstr /C:""" file.txt>temptemp.temp
FINDSTR: Cannot open file.txt>temptemp.temp
This does not work either:

Code: Select all

c:\findstr /C:"""" file.txt
Any ideas? Regular expressions maybe?

Thanks.
Saso

Re: findstr /C:""" file.txt&&set found=1 FAILS

Posted: 12 Feb 2019 04:14
by jfl
You have an odd number of quotes in your request. So everything that follows the third one is passed to findstr as a single argument.
To fix that, you have to ^escape one of the quotes, so that cmd.exe parses it as a normal character:

Code: Select all

C:\JFL\Temp>C:\JFL\Temp>findstr /C:^""" file.txt
Some more text with " quote

findstr /C:^""" file.txt && echo FOUND
Some more text with " quote
FOUND

C:\JFL\Temp>

Re: findstr /C:""" file.txt&&set found=1 FAILS

Posted: 12 Feb 2019 06:29
by miskox
Great! Thanks. I added extra quote (total of 4) but that didn't work either.

I had problems with

Code: Select all

findstr /C:"^|" file.txt
returns no matches, while

Code: Select all

findstr /C:"|" file.txt
returns a match (or more). This does not work:

Code: Select all

findstr /C:"^"" file.txt

But this (your version - first quote escaped) works:

Code: Select all

findstr /C:^""" file.txt
Thanks.
Saso

Re: findstr /C:""" file.txt&&set found=1 FAILS

Posted: 12 Feb 2019 06:58
by aGerman
Dave's explanations on SO might be quite interesting in those cases
https://stackoverflow.com/questions/884 ... str-comman

Steffen

Re: findstr /C:""" file.txt&&set found=1 FAILS

Posted: 12 Feb 2019 07:13
by dbenham
I'm not sure what you are attempting with your first two examples. But the last two give expected behavior - you cannot escape a quote once quoting has begun.
So you can escape the first quote or the last quote, but not the middle one.

Regarding my SO post that aGerman linked - the section on escaping quotes is not correct, but does provide useful tips that are generally useful. The actual rules for how FINDSTR treats quotes are horrifically complicated, and are an artifact of how the Microsoft C/C++ library parses command line arguments. Follow the link if you dare :!: :lol:

I can follow that article enough to see that there is predictable behavior, but I can't quite wrap my head around it.


Dave Benham

Re: findstr /C:""" file.txt&&set found=1 FAILS

Posted: 12 Feb 2019 07:17
by jfl
The ^ escaping works outside of quoted strings. Inside quoted strings, ^ is a normal character.
^""" passes """ to the findstr.exe program
"^"" passes "^"" to the findstr.exe program
Same thing for the ^| character in your first example. It's inside a quoted string, so
1) The ^ is useless, as | being inside a quoted string, it needs no escaping,
and 2) the ^ is passed along with the | to findstr, which does not find ^| together in the file.

Re: findstr /C:""" file.txt&&set found=1 FAILS

Posted: 13 Feb 2019 06:27
by miskox
Thanks.

Saso