Ahmad Ismail
Ahmad Ismail

Reputation: 13862

CMD - check folder path for a regular expression

I am searching for all the folders path that has a certain pattern.

Ex. 
D:\Testfolder>tree
Folder PATH listing for volume Working
D:.

├───fifth
│   ├───first
│   ├───second
│   └───third
│       └───_dn
│           ├───first
│           └───second
├───first
│   ├───first
│   └───second
│       └───_dn
│           ├───first
│           └───second
├───fourth
│   └───first
│       └───_dn
│           ├───first
│           └───second
├───second
│   ├───first
│   ├───second
│   └───third
│       └───_dn
│           └───first
└───third
    ├───first
    └───second
        └───_dn
            ├───first
            └───second

If the path has the word "_dn" then I need to save it in a file.

Expected Output:

D:\Testfolder\fifth\third\_dn
D:\Testfolder\fifth\third\_dn\first
D:\Testfolder\fifth\third\_dn\second
D:\Testfolder\first\second\_dn
D:\Testfolder\first\second\_dn\first
D:\Testfolder\first\second\_dn\second
D:\Testfolder\fourth\first\_dn
D:\Testfolder\fourth\first\_dn\first
D:\Testfolder\fourth\first\_dn\second
D:\Testfolder\second\third\_dn
D:\Testfolder\second\third\_dn\first
D:\Testfolder\third\second\_dn
D:\Testfolder\third\second\_dn\first
D:\Testfolder\third\second\_dn\second

Update 1

Someone posted a almost correct answer and then deleted it. I could not track his name to refer him.

The code is

(For /R "D:\Testfolder" /D %A In (*_dn*) Do @Echo %A)>"savefile.txt"

The return value is

D:\Testfolder\fifth\third\_dn
D:\Testfolder\first\second\_dn
D:\Testfolder\fourth\first\_dn
D:\Testfolder\second\third\_dn
D:\Testfolder\third\second\_dn

However this return value is missing all the paths that has something after _dn\,

Ex.
D:\Testfolder\fifth\third\_dn\first
D:\Testfolder\fifth\third\_dn\second
D:\Testfolder\first\second\_dn\first
D:\Testfolder\first\second\_dn\second
D:\Testfolder\fourth\first\_dn\first
D:\Testfolder\fourth\first\_dn\second
D:\Testfolder\second\third\_dn\first
D:\Testfolder\third\second\_dn\first
D:\Testfolder\third\second\_dn\second

Upvotes: 1

Views: 1436

Answers (3)

lit
lit

Reputation: 16236

I see that you already have an answer. If you wanted to take a step into PowerShell, you could do something like this. Someday, this will probably be considered more maintainable.

Get-ChildItem -Directory -Recurse -Filter '_dn' -ErrorAction SilentlyContinue |
    ForEach-Object { $_.FullName } >savefile.txt

Or, using aliases and minimal parameters it could be cryptic and unmaintainable.

gci -di -rec -filt '_dn' -ea si | % { $_.FullName } >savefile.txt

Upvotes: 0

Hackoo
Hackoo

Reputation: 18827

Try like this :

for /f "delims=" %a in ('Dir /ad /s /b D:\Testfolder\ ^| find /I "_dn"') do @echo %a

Upvotes: 1

Compo
Compo

Reputation: 38623

You could perhaps try using Dir with Find like this:

(Dir /B/S/AD "D:\Testfolder"|Find /I "\_dn\")>"savefile.txt"

Upvotes: 1

Related Questions