After 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.
C:\Users\wtn>dir
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)
or
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 /?
Comments on How to Save A Directory Tree Listing To A File In Windows
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.
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.
Thomas Preiss @ 5:18 pm
Hello,
I have read through the information and am still at a loss as to how print up a file tree that includes the files inside each file. I am wanting to do this with my digitally recorded vinyl collection. For example, in my Neil Young file, there are 45 files that contain the mp3 files from each record I made a digital copy of.
I hope I have asked a clear, concise question regarding my problem. I appreciate you being out there, fielding these questions.
Sincerely,
Thomas Preiss
@Thomas Preiss
To list files in multiple folders under the parent folder, use the /S switch with the dir command.
For example to list all files under each folder in the Neil Young folder and print output to the neilyoung.txt file use:
dir /S > neilyoung.txt