DOS command to list both directory AND subdirectories

Posted on 2014-08-06
Last Modified: 2014-08-18

I have almost no knowledge of DOS but sometime ago, an helpful EE Expert provided me with a DOS command which creates a text file of a folder's directory. The command is as follows:

      Dir "C:\Users\Username\Folder name" > "C:\Users\Username\Folder name\List.txt"

I have found this command to be a great tool and have used it multiple times.

When using this command, I've noticed that when the directory contains a subfolder, the subfolder name is included in the resulting .txt file with the preceding label: <DIR>, but no subfolder contents are included.

Is there a way to modify the above DOS command so that it will display not only subfolder names but their directories as well?

Question by:WeThotUWasAToad
    LVL 22

    Accepted Solution

    that's controlled by the DIR command

    Displays a list of files and subdirectories in a directory.

    DIR [drive:][path][filename] [/A[[:]attributes]] [ /B ]  [/C] [/D] [/L] [/N]
      [/O[[:]sortorder]] [/P] [/Q] [/R] [/S] [/T[[:]timefield]] [/W] [/X] [/4]

                  Specifies drive, directory, and/or files to list.

      /A          Displays files with specified attributes.
      attributes   D  Directories                R  Read-only files
                   H  Hidden files               A  Files ready for archiving
                   S  System files               I  Not content indexed files
                   L  Reparse Points             -  Prefix meaning not
      /B          Uses bare format (no heading information or summary).
      /C          Display the thousand separator in file sizes.  This is the
                  default.  Use /-C to disable display of separator.
      /D          Same as wide but files are list sorted by column.
      /L          Uses lowercase.
      /N          New long list format where filenames are on the far right.
      /O          List by files in sorted order.
      sortorder    N  By name (alphabetic)       S  By size (smallest first)
                   E  By extension (alphabetic)  D  By date/time (oldest first)
                   G  Group directories first    -  Prefix to reverse order
     /P          Pauses after each screenful of information.
     /Q          Display the owner of the file.
     /R          Display alternate data streams of the file.
     /S          Displays files in specified directory and all subdirectories.
     /T          Controls which time field displayed or used for sorting
     timefield   C  Creation
                 A  Last Access
                 W  Last Written
     /W          Uses wide list format.
     /X          This displays the short names generated for non-8dot3 file
                 names.  The format is that of /N with the short name inserted
                 before the long name. If no short name is present, blanks are
                 displayed in its place.
     /4          Displays four-digit years

    witches may be preset in the DIRCMD environment variable.  Override
    reset switches by prefixing any switch with - (hyphen)--for example, /-W.

    So, to show All subdirectories you could use this:

    Dir "C:\Users\Username\Folder name" /s >"C:\Users\Username\Folder name\List.txt"

    Please note that this will show all files in the subdirectories as well.

    Hope this helps!


    LVL 22

    Expert Comment

    If you wanted to show only the directory names, you could try this:

    Dir "C:\Users\Username\Folder name" /s /AD > "C:\Users\Username\Folder name\List.txt"

    Hope this helps!


    LVL 16

    Expert Comment

    by:Emmanuel Adebayo
    Tree command should do what you are looking for

    At the command prompt enter the following

    Tree > c:\tree.txt

    When the execution is completed, check c:\tree.txt file

    LVL 43

    Expert Comment

    by:Steve Knight
    To add slightly to that...

    CD c:\startdir
    Tree /f > c:\somewhere\lust.txt

    Will show all files in the tree too.

    LVL 51

    Expert Comment

    by:Joe Winograd, EE MVE
    Hi We...Toad,

    > display not only subfolder names but their directories as well

    By "directories" above, I'm assuming you mean "files" (or "contents"). Just add the "/s" option. In other words:

    Dir "C:\Users\Username\Folder name" /s > "C:\Users\Username\Folder name\List.txt"

    Regards, Joe

    Author Closing Comment

    Thanks for the detailed answer.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
    One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    This video discusses moving either the default database or any database to a new volume.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now