Reputation: 9536
I am trying to get a list of paths of all subdirectories (recursively) which have some specified name, e.g. "bin"
. The problem is that if current directory contains subdirectory of that name, DIR command will be executed only within that subdirectory, ignoring other subdirectories.
Example:
C:\DEVELOPMENT\RESEARCH>ver
Microsoft Windows [Version 6.1.7601]
C:\DEVELOPMENT\RESEARCH>dir *bin* /ad /s /b
C:\DEVELOPMENT\RESEARCH\bin
C:\DEVELOPMENT\RESEARCH\Apache\2bin
C:\DEVELOPMENT\RESEARCH\Apache\bin
C:\DEVELOPMENT\RESEARCH\Apache\bin1
C:\DEVELOPMENT\RESEARCH\C#\ConsoleApps\MiscTests\bin
C:\DEVELOPMENT\RESEARCH>dir bin* /ad /s /b
C:\DEVELOPMENT\RESEARCH\bin
C:\DEVELOPMENT\RESEARCH\Apache\bin
C:\DEVELOPMENT\RESEARCH\Apache\bin1
C:\DEVELOPMENT\RESEARCH\C#\ConsoleApps\MiscTests\bin
C:\DEVELOPMENT\RESEARCH>dir bin /ad /s /b
C:\DEVELOPMENT\RESEARCH\bin\test
C:\DEVELOPMENT\RESEARCH>rmdir bin /s /q
C:\DEVELOPMENT\RESEARCH>dir bin /ad /s /b
C:\DEVELOPMENT\RESEARCH\Apache\bin
C:\DEVELOPMENT\RESEARCH\C#\ConsoleApps\MiscTests\bin
C:\DEVELOPMENT\RESEARCH>
dir *bin* /ad /s /b
outputs all subdirectories that have bin
in their name. And this output is ok. Same with dir bin* /ad /s /b
which outputs all subdirectories which name begins with bin
. But dir bin /ad /s /b
outputs only the content of the first child of the current directory which has name bin
. Desired output is:
C:\DEVELOPMENT\RESEARCH\bin
C:\DEVELOPMENT\RESEARCH\Apache\bin
C:\DEVELOPMENT\RESEARCH\C#\ConsoleApps\MiscTests\bin
How can I achieve this?
NOTE: If current directory does not contain bin
child, output is as expected. (I deleted bin
child to show this)
Upvotes: 5
Views: 7378
Reputation: 8005
If your current directory contains a bin
subdirectory, then it is difficult using standard DOS commands. I think you have three basic options:
# Option 1: FOR and check directory existance (modified from MBu's answer - the
# original answer just appended 'bin' to all directories whether it existed or not)
# (replace the 'echo %A' with your desired action)
for /r /d %A in (bin) do if exist %A\NUL echo %A
# Option 2: PowerShell (second item is if you need to verify it is a directory)
Get-ChildItem -filter bin -recurse
Get-ChildItem -filter bin -recurse |? { $_.Attributes -match 'Directory' }
# Option 3: Use UNIX/Cygwin find.exe (not to be confused in DOS find)
# (you can locate on the net, such as GNU Utilities for Win32)
find.exe . -name bin
find.exe . -name bin -type d
Upvotes: 6