[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4833
  • Last Modified:

DOS command to list both directory AND subdirectories


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?

1 Solution
Christopher McKayCommented:
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!


Christopher McKayCommented:
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!


Emmanuel AdebayoCommented:
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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Steve KnightIT ConsultancyCommented:
To add slightly to that...

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

Will show all files in the tree too.

Joe Winograd, EE MVE 2015&2016DeveloperCommented:
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
WeThotUWasAToadAuthor Commented:
Thanks for the detailed answer.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now