Finding two backslashes

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Finding two backslashes

#1 Post by Squashman » 30 Mar 2015 13:13

Not sure why I can't figure this out. Just trying to find all files that use UNC paths in them. So I figured I would just search for a double back slash. But every time I try it outputs all of my files. There should only be a handful. But don't feel like manually searching through dozens of files.

I have tried
findstr /M "\\" *.*
findstr /M "\\\\" *.*
findstr /M /R "\\\\" *.*

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Finding two backslashes

#2 Post by ShadowThief » 30 Mar 2015 13:31

What about the /L flag? Windows can interpret C:\path\\to\\file\\name as valid syntax, so something might be getting lost in there (I don't have any network drives so I can't test this).

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Finding two backslashes

#3 Post by Squashman » 30 Mar 2015 13:37

Tried the literal switch as well. Trying to read up on Dave's extensive documentation. I must not be understanding something.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Finding two backslashes

#4 Post by Squashman » 30 Mar 2015 13:43

Assuming I ALWAYS put quotes around my file paths as a best practice, this seems to work but would rather just look for \\.
findstr /M /C:"\"\\\\" *.*

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Finding two backslashes

#5 Post by Squashman » 30 Mar 2015 13:48

I literally need to use Dave's examples as he provided them. Don't put quotes around your search string!!!!!!
C:\BatchFiles\backslashes>findstr \\\\ *.*
TwoSlashes.txt:TwoSlashes\\TwoSlashes
TwoSlashes.txt: \\TwoSlashes
TwoSlashes.txt: "\\TwoSlashes"

C:\BatchFiles\backslashes>findstr \\\ *.*
TwoSlashes.txt:TwoSlashes\\TwoSlashes
TwoSlashes.txt: \\TwoSlashes
TwoSlashes.txt: "\\TwoSlashes"

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

Re: Finding two backslashes

#6 Post by dbenham » 30 Mar 2015 15:18

The backslash and quote escape rules are really wonky.

I can usually get it to work with enough patience and experimentation, but I don't think I've figured out the exact rules yet. I'm also pretty sure it changes between versions (especially Vista).

I find the simplest reliable method that seems to work in all situations is to use the regular expression [\\] to represent a single backslash. It works both quoted and unquoted.

Code: Select all

findstr "[\\][\\]" *.*
findstr [\\][\\] *.*


Dave Benham

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Finding two backslashes

#7 Post by penpen » 30 Mar 2015 19:55

I think (if i remember right.... tested some long long while ago using XP) this happens:
A doublequote character toggles an additional expansion phase (:= "doublequote expansion phase") between "on" and "off",
seperating the search string in "doublequoted" and "not doublequoted" parts.
Initially the state of this phase is "off".

Categorizing the doublequotes to:
- opening doublequote := a doublequote that toggles this state to "on".
- closing doublequote := a doublequote that toggles this state to "off".
- literal doublequote := a doublequote that does not toggle this state.

A doublequote following a "closing" doublequote or a '\' is a literal doublequote:

Code: Select all

"doublequoted"not_doublequoted"doublequoted"not_doublequoted
"doublequoted"""doublequoted"not_doublequoted
"doublequoted\"doublequoted"not_doublequoted
Note: A space in the 'not doublequoted' part ends the search string.
I haven't tested, if there are other characters that terminates the search string.

The opening and closing doublequotes are removed.
If a '\' follows a first '\', then the first '\' is removed (if it is not within a class block []), else it stays as is.

Code: Select all

"\"" --> \"
"\"  --> \
"\\"  --> \
"a\"  --> a\
"a\b" --> a\b
Note: You may also 'escape' the doublequotes by using """.

Search strings using this notation are defaulted to regular expressions (separated by space characters):

Code: Select all

findstr "three regular expressions" file.txt

Search strings using this notation are defaultet to literally expressions (separated by space characters):

Code: Select all

findstr /C:"one literal expression"


Examples for this "doublequote expansion":

Code: Select all

search string  --> matching pattern (interpreted "literally", or as a regular expresssion)
"\a"           --> \a
"\""           --> \"
"\\"           --> \
"\\\\"         --> \\
"\\\\\\"       --> \\\
"\\\\\\\\"     --> \\\\
"[\\][\\]"     --> [\\][\\]
"[\\\\][\\\\]" --> [\\\\][\\\\]
               ...
"\."           --> \.
"\*"           --> \*
"\^"           --> \^
"\$"           --> \$
"\["           --> \[
"\]"           --> \]
"\<"           --> \<
"\>"           --> \>


penpen

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

Re: Finding two backslashes

#8 Post by dbenham » 30 Mar 2015 21:11

Wouldn't it be nice if FINDSTR were that logical, but alas, no. :evil:

For example "\\\\" will match a single \ :!: (I haven't figured this one out). But remove the quotes, and it works as expected:

Code: Select all

C:\>echo \|findstr "\\\\"
\

C:\>echo \|findstr \\\\

C:\test>


Dave Benham

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Finding two backslashes

#9 Post by Squashman » 31 Mar 2015 07:28

dbenham wrote:Wouldn't it be nice if FINDSTR were that logical, but alas, no. :evil:
We probably can't blame this one on Bill's peyote smoking either. He was probably well removed from that development by the time the findstr command came around.

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

Re: Finding two backslashes

#10 Post by dbenham » 31 Mar 2015 07:43

Here is the story behind FINDSTR, as told by Raymond Chen - a long time developer at MicroSoft
http://blogs.msdn.com/b/oldnewthing/arc ... 72436.aspx


Dave Benham

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Finding two backslashes

#11 Post by Squashman » 31 Mar 2015 08:25

Strange that they first called it QGREP. I guess you could say it is like GREP but IMHO, GREP is a lot more powerful then FINDSTR. But, luckily we have JREPL now. :wink:

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Finding two backslashes

#12 Post by penpen » 31 Mar 2015 14:30

dbenham wrote:Wouldn't it be nice if FINDSTR were that logical, but alas, no. :evil:

For example "\\\\" will match a single \ :!: (I haven't figured this one out). But remove the quotes, and it works as expected:

Code: Select all

C:\>echo \|findstr "\\\\"
\

C:\>echo \|findstr \\\\

C:\test>


Dave Benham
Didn't i say this?
penpen wrote:

Code: Select all

search string  --> matching pattern (interpreted "literally", or as a regular expresssion)
: ...
"\\\\"         --> \\


With this notation i wanted to say:

Code: Select all

:: this command
findstr /L /C:"\\\\" test.txt
:: equals that command:
findstr /L /C:\\ test.txt

::same for all other in that list
findstr /L /C:"\\\\\\\\" test.txt
findstr /L /C:\\\\ test.txt

:: ...


penpen

Post Reply