Wednesday, March 11, 2009

Generate a File Listing from a Windows Explorer Context Menu

Recommended batch code:

dir /a /s /b /-p /o:gen >filelisting.txt

fileListing.bat

How many times have you been browsing through directories in Windows Explorer and wished you could generate a text file or printout listing the files and folders? It seems like such a simple request that it's amazing the option isn't available. You don't believe me? Right click on a folder and see for yourself if there is an option to list or print the structure. There isn't, but there is a workaround that doesn't require any third party software. Here's how to create a context menu item that when clicked will generate an editable text file listing of the selected directory.

Step 1 - Create A Batch File

To create the entry in the context menu it's necessary to first create a .bat file. The format for the .bat file is:

dir /a /-p /o:gen >filelisting.txt

The name of the .txt file can be whatever you'd like it called. In the example above it's filelisting, but it could just as easily be filelist, listoffiles, namedfiles, or even wally if you have a sense of the bizarre. Once you've decided on the name, create the file in Notepad and save the file in your Windows folder as shown below. If you want, just copy and paste the example up above if you don't find wally intriguing.


Fig. 01

Step 2 - Modify The Context Menu

Now that we have the .bat file created the next step is to make it functional and easily accessible by integrating it into the context menu that opens when a right click is executed. To do this:

Open Windows Explorer, click Tools, then click Folder Options.
Click the File Types tab, and then click Folder.
Click the Advanced button and then click New to pen the New Action box shown below.


Fig. 02

In the New Action [Fig. 02] box, type the name that you want to appear in the context menu. Once again, you have a wide latitude in choices but something akin to Create File Listing will probably be more useful than naming it Martha Stewart. As you can see in the example above, I ditched Martha in favor of Create File Listing. Sorry Martha. Browse to the location where the .bat file you created is located, select it and let it be the Application Used to Perform Actions. Click OK and do the standard Windows dance of Apply and OK again to close all the open windows.

That's it ! Congratulations. You've created a new item on the context menu that's ready to go to work. So now that's it there, what can you do with it? Open up Windows Explorer as I did in the example [Fig. 03] below.


Fig. 03

Navigate to whatever folder you want to use as the basis for the file list and right click to open the context menu. Click on the Create File Listing item and the list will be generated and displayed at the bottom of the open window as filelisting.txt. The example below was created from the Sample Music folder shown above. Since it is a text file it can be fully edited, copied, pasted, printed, etc for any purpose.


Fig. 04

Note: If for any reason you want to remove the Create File Listing entry from the context menu it will be necessary to edit the registry. This can be accomplished by navigating to HKEY_CLASSES_ROOT\Folder\shell\Create_File_Listing using regedit and deleting the Create_File_Listing key in the left pane. Close regedit and reboot to complete removal.


Additional File Modifications Submitted by TEG Readers


The following tip was sent in containing a very useful modification to the batch file;

Adding the "/b" switch to the proposed batch file will create a "bare format (no heading information or summary)" text which makes the output much easier to copy and manipulate. It eliminates all the other garbage in the output file.
Best regards,

Geert Pauwels

This is an excellent idea and very easy to implement. If you are creating the batch file for the first time just modify the file listed in the article so it reads;

dir /a /b /-p /o:gen >filelisting.txt

and continue with the instructions for completing the modification. If the batch file already exists in C:\Windows, navigate to the file, right click it and select Edit from the context menu and modify the file so it contains the /b switch. Save the changes and close the file. The change will modify the output so it appears as shown below.


Fig. 05

While it's easy enough to go into the C:\Windows directory and edit the batch file to generate the type of output preferred, depending on how often you change between the two output formats you may find it more convenient to create separate entries for each format that can be selected from the context menu. The procedure is simple.

  • Create one batch file using the /b switch and name it filelisting.bat.

  • Create a second batch file without the /b switch and name it filelisting1.bat.

  • Save both files in C:\Windows.

  • Modify the context menu as shown in Step 2 and Fig. 02 above, but this time go through the procedure twice and give each entry a different name in the Action line, assigning each entry a different batch file. I used the names "Create File Listing" and "Create File Listing Verbose" for this example, assigning the batch file with the /b switch to "Create File Listing" and the batch file without the /b switch to the "Create File Listing Verbose" entry.

  • Both entries are now on the context menu and available for use as shown in Fig. 06.


Fig. 06


The following modification was submitted by Antony

Hi,

Thanks for the tip on generating a directory listing from windows explorer - this has bugged me for ages.

Just wanted to add that when I run the command as listed, the file gets created in the directory that I want a listing for, but is never displayed automatically (I have to open it manually). This was annoying so I fixed it by doing the following:

  1. Specifying that the directory listing file always gets saved in the same location.

  2. Using the Start command to open the file with Notepad.

Here is the modified batch file.

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


The following modification was submitted by Max

Hello, I enjoyed learning how to create file listing with the method provided on http://www.theeldergeek.com/file_list_generator.htm
I have however created a modification that people should enjoy.
By adding the line cd %1 to the top of the bat file, you can right click on a folder select Create a File List, and then the txt file will be saved inside that folder with a listing of the contents of that folder.
The next modification prevents the generated textfile from being listed within its self. The two lines are added under the dir command.

copy "C:\Temporary Location\textfile.txt" %1

del "C:\Temporary Location\textfile.txt"
Thus, my entire bat file looks like this:
cd %1
dir /a /b /-p /o:GEN >"C:\Documents and Settings\\Local Settings\Temp\filelist.txt"
copy "C:\Documents and Settings\\Local Settings\Temp\filelist.txt" %1
del "C:\Documents and Settings\\Local Settings\Temp\filelist.txt"
Please remember to use the quotations...otherwise, dos won't be able to deal with the spaces.
Enjoy,

Max


The following modification was submitted by Kevin

I've been trying to figure out how to capture a listing of files from within a folder -- but I don't want to print the list, just save it to a disc. (I'm sending a buddy a listing of all of the CD's that I've burned.)

I found your article entitled, "
Generate a File Listing from a Windows Explorer Context Menu" -- and although none of the solutions worked perfectly for me, I did some minor editing to give me what I needed.

I needed a batch file which would create a listing, allow me to cut, copy or print the contents, then delete the file itself (I have no need for a permanent file listing cluttering up my hard drive).

Here it is:

dir /a /-p /o:gen >filelisting.txt
filelisting.txt
del filelisting.txt

No comments:

Post a Comment

[Please do not advertise, or post irrelevant links. Thank you for your cooperation.]