Skip to main content

Save directory listing into a file on Windows

Save directory listing into a text file under Windows by simply using the right-click button.

Printing (or saving) a directory listing using the command line is an easy process. What we’re going to do is add an option to do this in the context menu of all folders.

The script file

Open an editor (like Notepad++), create a file named “listfile.bat” and save it where you want. To keep thing organized I’ll save it in the C:/Windows/ folder. This will be our batch file!

Now copy and paste the following lines:

cd %1
dir /a /b /p /o:gne > list.txt
exit

What does this code do?

%1 is an argument (the first and the only parameter of our script) which is, in this case, the path of the selected folder. So, in the first line, the batch file simply change the working directory (cd) to the one you clicked on.

The second line generate the list of files and directories that are inside our folder and redirect (>) the output to a file named “list.txt”.

Other arguments are used for:

/a  --> display all files
/b  --> hide extra information, like creation dates
/p  --> show output as pages
/o  --> sort results by folders (G), by name (N), and then by extension (E)

Add to context menu

Now let’s add our script to the context menu. Open the Registry Editor, click Start > Run > (or simply Win+R), type “regedit” and press OK.

Using the folder tree on the left, go to

Computer/HKEY_CLASSES_ROOT/Folder/shell

right click and select “New Key”. Set the name the key as “Create List of Files and Folders”. This will be the name of the option on the context menu and obviously it’s customizable.

After that, add another key named “command”, go on right panel, double click on Default REG_SZ and type here the path of your script, with an %1 at the end.

In my case, since my batch file is in C:/Windows/, this will be:

"C:/Windows/listfile.bat %1"

Save and close the regedit. That’s it, now simply right-click over a folder and click “Create List of Files and Folders” to generate the content list!