Thursday, August 6, 2015

How to get a list of folders only to be used in a batch file

I have been asked a few times "How do I get a list of folder names only to be used in a windows batch file?"

There a likely several ways to accomplish this depending on your intended use but for a simple list of folders found in a particular directory you can run the command below

dir /B /A:D-H-S

This command will display a list of folder names only without any time stamps or file sizes. It will not show Hidden or System folders.
This can be used if you wanted to loop through these folders in a batch file and perform another action on each folder.

The /B switch is for "bare format"
The /A:D-H-S shows only Directories (D) and does not show Hidden(-H) or System(-S) files

You can also pipe this to a text file if you wanted as well by using

dir /B /A:D-H-S > directory-listing.txt
To read more about the dir command options you can also type the following from a windows command prompt.

dir /?


Hope this helps.

No comments: