Batch Script to Generate File/Folder Listing

I was chatting with a friend who had a huge music collection and I wanted to know what all he had. This provoked me to make a batch script which would do the job. I did some research online and found out some basics of batch scripts. This is the final thing which I ended up with.

@echo Directory listing will be created at c:\dirList.txt
@dir %1 /S > c:\dirList.txt
@echo Opening c:\dirList.txt in Notepad
@notepad c:\dirList.txt

I think it’s self explanatory. But still, I’ll go ahead and explain.

1. Prints “Directory listing will be created at c:\dirList.txt”

2. dir is a command which will print the list of directories and files. %1 is a variable which will contain the location from where you run this script. /s is a switch for showing all sub-directories. > c:\dirList.txt will save the output of the operation dir %1 /s to a text file called dirList.txt in C Drive.

In short, the second line means this:
Run the directory listing command for the location %1 and store it’s output in c:\dirList.txt.

3. Prints “Opening c:\dirList.txt in Notepad”

4. Opens c:\dirList.txt in notepad.

5. Wait for the user to respond by pressing any key before terminating the program.

Now I’ll tell you how to make the whole thing work. Create a New Text Document on your desktop and rename it to Create DirList.bat. Right-click on it and press Edit. Copy the following code segment into it and save.

@echo Directory listing will be created at c:\dirList.txt
@dir %1 /S > c:\dirList.txt
@echo Opening c:\dirList.txt in Notepad
@notepad c:\dirList.txt

Now move it to C:\Documents and Settings\{username}\SendTo

Since you'd be giving this to your friends and some of them will have difficulty doing this, to make things simpler, I created another script to perform this move.

Create a New Text Document again and this time, call it install.bat.

Copy the following into it and save it.

@move "Create DirList.bat" "C:\Documents and Settings\%username%\SendTo\Create DirList.bat"
@echo Move Complete

Remember that both install.bat and Create DirList.bat must be located in the same place.

Now, run install.bat to move Create DirList.bat to the SendTo folder. The reason why we do this is simple. Whenever we right-click on a folder and Send To -> Create DirList.bat, it will send the location of the folder we right-clicked as %1.

Therefore, if you right-click on a folder called Songs in F Drive, %1 will contain the location F:\Songs. Therefore, the main command executed will be this:

dir F:\Songs /s > c:\dirList.txt

Now whenever you want the listing of a directory, just Right-click -> Send To -> Create DirList.txt.

Simple isn’t it?

Please keep all the suggestions pouring in.

Published by


I'm a student pursuing a masters degree in Theoretical Computer Science. Prior to this, I completed my bachelors in Computer Science and Engineering and worked for an year, until I realized that the slogging off at an office in front of the computer is not the job for me. I am easily fascinated and distracted by everything under the sun, which has kept me busy trying to figure out what I am going to. I love dreaming about making radical life decisions, bringing happiness on faces and about a world full of meadows and fluttery creatures and people who do not judge you for what you love doing.

4 thoughts on “Batch Script to Generate File/Folder Listing”

  1. I've encountered some java programs which did that... It might be possible with batch scripts, but it'll be lot of work. Why insist on batch when there are better programming languages?

  2. well, batch is nice because it's just one file that runs in your directory... i found a little program called dir2html on and it seems to be doing the trick... i just wish it would accept command line arguments to expedite the process.
    i run a test server on my computer called tinyweb by ritlabs... it doesn't give directory listings, so i was looking for a way that i could do it quick and hyperlinked...

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>