Thursday, April 23, 2015

Findstr regexp to find subfolders of shares 3rd level

Findstr regexp quick note:
As you may know regexp or regular expressions are there to ease our work
Using awk,sed is very common. But it can also be used to quickly search some patterns in windows native command findstr.
If you are not familiar with regexp it is like telling your computer you want to search some patterns instead of a literal search.
Literal search would be: dir file.txt : file.txt is the filename
Regexp search would be: dir *.txt : the asterisk is part of the regexp search, it is a very silly simple example but you understand the idea.

So for example you have a list of directories or shares.
And what you need is to find only the subfolders, at the 3rd level.
You can you use findstr in this way:
type shares-with-quotes.txt | findstr /r /c:"\\\\.*\\.*\\.*"

From input:
\\server1\share1
\\server2\share2
\\server1\share1\subfolder1
\\server2\share2\subfolder1

type shares-with-quotes.txt | findstr /r /c:"\\\\.*\\.*\\.*"
Only this will be filtered:
\\server1\share1\subfolder1
\\server2\share2\subfolder1

And if you want the opposite do somethink like:
type shares-with-quotes.txt | findstr /r /v /c:"\\\\.*\\.*\\.*"
\\server1\share1
\\server2\share2

Note: "\" has to be doubled because of the regexp notation so a literal "\" needs to be stated as "\\", you will have to escape some special characters like this or dot, asteriks, etc

0 comments:

Post a Comment