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?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Christopher McKayMicrosoft Network AdministratorCommented:
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!



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Christopher McKayMicrosoft Network AdministratorCommented:
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 AdebayoGlobal Windows Infrastructure Engineer - ConsultantCommented:
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

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

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, Fellow&MVEDeveloperCommented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.