windows 如何在批处理脚本中获取所有子文件夹的名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1487214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to grab the names of all sub-folders in a batch script?
提问by domlao
I just want to know how can I get all the names of the folders in a current directory. For example in my current directory I have three folders:
我只想知道如何获取当前目录中文件夹的所有名称。例如,在我当前的目录中,我有三个文件夹:
stackoverflow reddit codinghorror
Then when I execute my batch script all the three folders will print in the screen.
然后当我执行批处理脚本时,所有三个文件夹都将打印在屏幕上。
How can I achieve this?
我怎样才能做到这一点?
回答by Ben M
Using batch files:
使用批处理文件:
for /d %%d in (*.*) do echo %%d
If you want to test that on the command line, use only one % sign in both cases.
如果您想在命令行上进行测试,请在两种情况下仅使用一个 % 符号。
回答by akf
On Windows, you can use:
在 Windows 上,您可以使用:
dir /ad /b
/ad
will get you the directories only/b
will present it in 'bare' format
/ad
会让你的目录只会/b
以“裸”格式呈现
EDIT (reply to comment):
编辑(回复评论):
If you want to iterate over these directories and do something with them, use a for
command:
如果要遍历这些目录并对其进行处理,请使用以下for
命令:
for /F "delims=" %%a in ('dir /ad /b') do (
echo %%a
)
- note the double
%
- this is for use in a batch, if you use for on the command line, use a single%
. - added the resetting of default space delims in response to @Helen's comment
- 注意 double
%
- 这是用于批处理的,如果您在命令行上使用 for,请使用单个%
. - 添加了默认空格分隔的重置以响应@Helen 的评论
回答by Philippe
With PowerShell:
使用 PowerShell:
gci | ? { $_.PSIsContainer }
Old Answer:
旧答案:
With PowerShell:
使用 PowerShell:
gci | ? {$_.Length -eq $null } | % { $_.Name }
You can use the result as an array in a script, and then foreach trough it, or whatever you want to do...
您可以将结果用作脚本中的数组,然后通过它进行 foreach,或者您想做的任何事情...
回答by Arnaf Aziz
For getting all the subfolders of a specific directory and display it in CMD :
要获取特定目录的所有子文件夹并将其显示在 CMD 中:
@echo off
dir C:\input /s /b /o:n /a:d
Pause&Exit
For getting all the subfolders of a specific directory and save it in a text file :
要获取特定目录的所有子文件夹并将其保存在文本文件中:
dir C:\your_directory /s /b /o:n /a:d > output.txt