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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
miskox
Posts: 330
Joined: 28 Jun 2010 03:46

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

#1 Post by miskox » 12 Feb 2019 02:59

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

jfl
Posts: 99
Joined: 26 Oct 2012 06:40
Location: Saint Hilaire du Touvet, France
Contact:

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

#2 Post by jfl » 12 Feb 2019 04:14

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>

miskox
Posts: 330
Joined: 28 Jun 2010 03:46

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

#3 Post by miskox » 12 Feb 2019 06:29

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

aGerman
Expert
Posts: 3605
Joined: 22 Jan 2010 18:01
Location: Germany

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

#4 Post by aGerman » 12 Feb 2019 06:58

Dave's explanations on SO might be quite interesting in those cases
https://stackoverflow.com/questions/884 ... str-comman

Steffen

dbenham
Expert
Posts: 2218
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

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

#5 Post by dbenham » 12 Feb 2019 07:13

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

jfl
Posts: 99
Joined: 26 Oct 2012 06:40
Location: Saint Hilaire du Touvet, France
Contact:

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

#6 Post by jfl » 12 Feb 2019 07:17

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.

miskox
Posts: 330
Joined: 28 Jun 2010 03:46

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

#7 Post by miskox » 13 Feb 2019 06:27

Thanks.

Saso

Post Reply