Create a text file of the contents of a directory in Windows Explorer

Tested on 2 October 2007 using Windows XP Professional and on 3 August 2012 using Windows 7
Tip: The following guide will show how to output the contents of a directory to a text file for saving, printing or editing.

Method 1 - Using a batch file to create the text file:

  1. Create a new file in a text editor (i.e. open Notepad)

    New notepad file

  2. Insert the following text into the file:
    • dir /a /b /-p /o:gen >C:\WINDOWS\Temp\file_list.txt
    • start notepad C:\WINDOWS\Temp\file_list.txt

    Notepad file with inserted text

  3. Save the file as File List Generator.bat
  4. To show the contents of a directory (folder) in a text file, simply copy or move the File List Generator.bat file into the folder where you want the contents printed and double-click the file.

This will save a text file with the contents of the folder and then open it in Notepad.

From here you can edit, format and print the contents.

Modifying the output of the text file:

In the above example the text file simply created a list of the files and folders within the directory. It is also possible to output other information about the directory such as date and time stamps, file sizes and drive information.

  1. Create a new file in a text editor (i.e. open Notepad)
  2. Insert the following text into the file:
    • dir /a /b /-p /o:gen >C:\WINDOWS\Temp\file_list_full.txt
    • start notepad C:\WINDOWS\Temp\file_list_full.txt
  3. Save the file as File List Generator.bat
Note: If you have any problems it is usually caused by this part of the code:
C:\WINDOWS\Temp\

Make sure this directory exists and that you have permissions to read/write or, adjust the path to point to another folder. For instance:

  • dir /a /b /-p /o:gen >C:\file_list.txt
  • start notepad C:\file_list.txt

The above code would save the file to the top level of the C drive.

Method 2 - Using the command line to create the text file:

  1. Click Start, then click Run...
  2. Type cmd into the entry box and click OK
  3. From the windows command line, navigate to the directory/folder that you want to save to the text file.
    Tip: For example:
    • Type d: to move to another drive (d)
    • Type cd \ to go to the root of the drive
    • Type cd \some\directory to go to a specific directory
  4. To output the contents to a text file, type one of the following:
    1. Files and folders from the current directory only:
      dir /b >some-file.txt
    2. Files and folders from the current directory and all sub-directories:
      dir /s /b >some-file.txt
    3. Just the folders from the current directory only:
      dir /b *. >some-file.txt
    4. Just the folders from the current directory and all sub-directories:
      dir /s /b *. >some-file.txt
  5. Type exit to close the command line.

A text file (some-file.txt) will be saved in the same directory from where the command was made.

Go back to the How-to Guides main page.

Copyright © 2014 Fourwest Media. All rights reserved

Valid XHTML 1.0 Strict Valid CSS!