Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to grab the names of all sub-folders in a batch script?

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?

like image 365
domlao Avatar asked Nov 30 '22 19:11

domlao


2 Answers

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.

like image 94
Ben M Avatar answered Dec 06 '22 09:12

Ben M


On Windows, you can use:

dir /ad /b

/ad will get you the directories only
/b will present it in 'bare' format

EDIT (reply to comment):

If you want to iterate over these directories and do something with them, use a for command:

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
like image 37
akf Avatar answered Dec 06 '22 09:12

akf