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

时间:2012-04-03 11:53:22

标签: windows-7 batch-file command-prompt cmd

我正在尝试获取所有子目录(递归)的路径列表,这些路径具有一些指定的名称,例如: "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>

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

我怎样才能做到这一点?

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:3)

这应该有效:

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