List of all subdirectories with the specified name

I am trying to get a list of paths of all subdirectories (recursively) that have a specific name, eg. "bin"

... The problem is that if the current directory contains a subdirectory of that name, the DIR command will only be executed 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

lists all subdirectories with bin

in their name. And this exit is fine. Ditto with dir bin* /ad /s /b

, which lists all subdirectories that start with bin

. But it dir bin /ad /s /b

only outputs the contents of the first child of the current directory named bin

. Desired output:

C:\DEVELOPMENT\RESEARCH\bin
C:\DEVELOPMENT\RESEARCH\Apache\bin
C:\DEVELOPMENT\RESEARCH\C#\ConsoleApps\MiscTests\bin

      

How can I achieve this?

NOTE. If the current directory does not contain a child bin

, the output will be as expected. (I removed bin

child to show this)

+3


source to share


2 answers


If your current directory contains a subdirectory bin

then using standard DOS commands is tricky. I think you have three main options:

# Option 1: FOR and check directory existance (modified from MBu 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

      



+5


source


This should work:



for /R /D %A in (*bin*) do echo %A

      

+3


source







All Articles