Windows batch file - display all sub-folders

10,630

Solution 1

You can use the dir command with the modifier /a:d which will tell it to only search directories

FOR /f "tokens=*" %%i in ('DIR /a:d /b w*') DO (
    ECHO %%i
)

This will find all of the subfolders that start with w*

Solution 2

Here's modified version of Andrew's answer that can handle multiple prefixes:

dir /a:d /b w* we* cm* cr*
Share:
10,630
TyC
Author by

TyC

Unloyal glory hunter

Updated on June 17, 2022

Comments

  • TyC
    TyC almost 2 years

    I'm having difficulty returning JUST folders (ignore files) using a windows batch file.

    Here's what I have right now. Currently it's returing files and sub-sub-folders.

    for /r %%g in ("xx*") do echo %%g
    

    Also, say I want to only return the folders that start with a couple different prefixes.

    For example: I want to echo only the folders that start with w*, we*, cm*, cr*, etc under within in the folder "work". I do Is this possible using a batch file?

    Thanks.

  • TyC
    TyC over 12 years
    Thanks andrew, do you know if you can search for all of those prefixes at once by chance?
  • Andrew
    Andrew over 12 years
    I don't know of a way to search for multiple prefixes at once without a more complicated IF statement (something like list everything, then check everything to see if it matches the pattern).