列出具有指定名称的所有子目录

Boj*_*zec 4 cmd batch-file command-prompt windows-7

我试图得到一个所有子目录(递归)的路径列表,其中有一些指定的名称,例如"bin".问题是如果当前目录包含该名称的子目录,则DIR命令将仅在该子目录中执行,忽略其他子目录.

例:

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>
Run Code Online (Sandbox Code Playgroud)

dir *bin* /ad /s /b输出bin名称中包含的所有子目录.这个输出没问题.与dir bin* /ad /s /b输出名称开头的所有子目录相同bin.但dir bin /ad /s /b只输出当前目录中具有名称的第一个子节点的内容bin.期望的输出是:

C:\DEVELOPMENT\RESEARCH\bin
C:\DEVELOPMENT\RESEARCH\Apache\bin
C:\DEVELOPMENT\RESEARCH\C#\ConsoleApps\MiscTests\bin
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

注意:如果当前目录不包含子目录bin,则输出符合预期.(我删除了bin孩子以显示这个)

Rya*_*yan 6

如果当前目录包含bin子目录,则使用标准DOS命令很困难.我认为你有三个基本选择:

# 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
Run Code Online (Sandbox Code Playgroud)


Aac*_*ini 5

这应该工作:

for /R /D %A in (*bin*) do echo %A
Run Code Online (Sandbox Code Playgroud)