How to grab the names of all subfolders in a batch script?
I just want to know how can I get all the folder names in the current directory. For example, in my current directory, I have three folders:
stackoverflow reddit codinghorror
Then, when I execute my batch script, all three folders will print to the screen.
How can I achieve this?
Using batch files:
for /d %%d in (*.*) do echo %%d
If you want to check that on the command line use only one% character in both cases.
On Windows, you can use:
dir /ad /b
/ad
will only provide you with directories will /b
present it in "naked" format
EDIT (answer to comment):
If you want to iterate over these directories and do something with them, use the command for
:
for /F "delims=" %%a in ('dir /ad /b') do (
echo %%a
)
- note the double
%
is for package use, if you use for command line use one%
. - added resetting default space demits in response to @ Helen's comment
With PowerShell:
gci | ? { $_.PSIsContainer }
Old answer:
With PowerShell:
gci | ? {$_.Length -eq $null } | % { $_.Name }
You can use the result as an array in your script and then loop through it or whatever you do ...
To get all subfolders of a specific directory and display it in the CMD:
@echo off
dir C:\input /s /b /o:n /a:d
Pause&Exit
To get all subfolders of a specific directory and save it to a text file:
dir C:\your_directory /s /b /o:n /a:d > output.txt