Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8550187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:53:39+00:00 2026-06-11T13:53:39+00:00

I’m currently using the following command to list some directories: dir /b /s /AD

  • 0

I’m currently using the following command to list some directories:

dir /b /s /AD > c:\temp\dir_list.txt

This gives me almost the list that I need. But it is way too much data because some folders have lots of subfolders that I don’t want to see in my listing.

Is it possible to limit the recursion depth of the command to 3 (for example)?

c:\dir_1\dir_2\dir_3\dir_foo

So I don’t want to see the directory dir_foo if I execute the command in the above example in c:\>, but just the dir_n ones.

Maybe without a batch or VB script?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T13:53:41+00:00Added an answer on June 11, 2026 at 1:53 pm

    I’m sure it is possible to write a complex command that would list n levels of directories. But it would be hard to remember the syntax and error prone. It would also need to change each time you want to change the number of levels.

    Much better to use a simple script.

    EDIT 5 Years Later – Actually, there is a simple one liner that has been available since Vista. See my new ROBOCOPY solution.

    Here is a batch solution that performs a depth first listing. The DIR /S command performs a breadth first listing, but I prefer this depth first format.

    @echo off
    setlocal
    set currentLevel=0
    set maxLevel=%2
    if not defined maxLevel set maxLevel=1
    
    :procFolder
    pushd %1 2>nul || exit /b
    if %currentLevel% lss %maxLevel% (
      for /d %%F in (*) do (
        echo %%~fF
        set /a currentLevel+=1
        call :procFolder "%%F"
        set /a currentLevel-=1
      )
    )
    popd
    

    The breadth first version is nearly the same, except it requires an extra FOR loop.

    @echo off
    setlocal
    set currentLevel=0
    set maxLevel=%2
    if not defined maxLevel set maxLevel=1
    
    :procFolder
    pushd %1 2>nul || exit /b
    if %currentLevel% lss %maxLevel% (
      for /d %%F in (*) do echo %%~fF
      for /d %%F in (*) do (
        set /a currentLevel+=1
        call :procFolder "%%F"
        set /a currentLevel-=1
      )
    )
    popd
    

    Both scripts expect two arguments:

    arg1 = the path of the root directory to be listed

    arg2 = the number of levels to list.

    So to list 3 levels of the current directory, you could use

    listDirs.bat . 3
    

    To list 5 levels of a different directory, you could use

    listDirs.bat "d:\my folder\" 5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.