How to Save A Directory Tree Listing To A File In Windows


winlogo.jpgAfter my post on How To Print A Directory Tree From Windows Explorer, many readers asked me how to save a directory tree listing to a file. In additional to that question, readers also wanted to know how to display other types of information about the directory and files.

Saving a directory tree to a file can be very useful such as using it with batch files, keeping track of folders, or emailing to technical support when there are problems.

No matter what the reason, here are some tips on how to do it.

First, open a command prompt by clicking on Start \ Run… and entering cmd in the run box.

Once the command prompt opens, the current directory will be your user directory. To display the contents of the directory, we'll use the dir (directory) command.

Volume in drive C is WTN
Volume Serial Number is E40F-D10A

Directory of C:\Users\wtn

09/28/2008  11:10 AM    <DIR>          .
09/28/2008  11:10 AM    <DIR>          ..
06/19/2008  07:47 PM    <DIR>          Contacts
09/28/2008  08:09 AM    <DIR>          Desktop
09/28/2008  10:49 AM    <DIR>          Documents
06/28/2008  11:27 PM    <DIR>          Downloads
06/19/2008  07:47 PM    <DIR>          Favorites
07/20/2008  04:02 PM    <DIR>          Links
06/19/2008  07:47 PM    <DIR>          Music
08/20/2008  01:36 PM    <DIR>          Pictures
06/19/2008  07:47 PM    <DIR>          Saved Games
06/19/2008  09:16 PM    <DIR>          Searches
09/28/2008  11:10 AM                 0 test.txt
09/28/2008  11:10 AM                 0 test1.txt
06/19/2008  07:47 PM    <DIR>          Videos
2 File(s)              0 bytes
13 Dir(s)  29,575,204,864 bytes free

Now, to save the results to a file, we'll need to redirect the output to a file instead of the screen. For this we'll use the the redirect symbol > by pressing SHIFT+. (period) with the dir command.

dir > mydirectory.txt

When the command is executed, the file mydirectory.txt will be created (in the current directory you are at) with a directory tree listing.

If you want the file created somewhere else other than the current directory, just add the absolute path with the file name.

dir > c:\mydirectory.txt (file will be created off the root of your C drive)


dir > c:\myfolder\mydirectory.txt (file will be created in the folder myfolder)

You can also list any directory tree located anywhere on your computer:

dir c:\windows\system32 > c:\myfolder\mydirectory.txt (saves directory tree listing of the system32 folder to the file mydirectory.txt in the myfolder folder)

You can also append more than one directory tree listing to the same file, with out over writing the exisitng contents in the file, using the following command:

dir >> mydirectory.txt

Now that you know how to redirect a directory tree listing to a file, you may want to add more information or sort the information. Here are some tips:

  • Say you want to list hidden files. To do this, you need to use the /A attribute switch which also has other options that you can use:

/A          Displays files with specified 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

So to display hidden files, use the following command:

dir /AH

  • You can also combine attributes together:

dir /AHS  (display hidden and system files)

  • You can also sort files by using the /O switch:

/O          List by files in sorted order.
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

To display contents alphabetically, use:

dir /ON

  • If you want to display content of folders and sub-folders of a directory, use the /S switch:

dir /S

  • If you rather display the conents in a wide format, use the /W switch:

C:\Users\wtn>dir /w
Volume in drive C is WTN
Volume Serial Number is E40F-D10A

Directory of C:\Users\wtn

[.]               [..]              [Contacts]        [Desktop]         [Documents]
[Downloads]       [Favorites]       [Links]           [Music]           mydirectory.txt
[Pictures]        [Saved Games]     [Searches]        test.txt          test1.txt

As you can see there are many ways to control the listing of a directory by combining the switches together. If you want to get the complete list of all switches that can be used with the dir command, just enter the following at the command prompt:

dir /?

Filed under Windows Tips by  #

Comments on How to Save A Directory Tree Listing To A File In Windows Leave a Comment

February 9, 2013

Michael Charley @ 3:40 pm #

In creating the file with the details of a folder that contains my music videos, the length column (video time lenth) does not get included in the .txt file I create. All other columns in the folder's display of contents are saved in the file except the 'length'. How do I solve this.

April 21, 2016

Pancha Lingan @ 10:51 am #

Hi thank you so much this help.. I was able to save the listing as a notepad file. However I am failing trying to add /T:AC time written or saved is default. It does not display any other date fields. Please help me.

Leave a Comment

Fields marked by an asterisk (*) are required.

Subscribe without commenting