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 @pause
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 @pause
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 @pause
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.txtNow 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.


4 Comments on “Batch Script to Generate File/Folder Listing”
now… if the script could generate a hyperlinked directory listing… that would be something!
i’m using this one now:
http://www.geocities.com/~budallen/batfiles.html#How%20to%20print%20a%20directory%20listing%20from%20Explorer
but i would like a batch file that would spit out an html file, something like a sitemap… that “Art HTML Listing” or like “The Html Directory 1.0″ make
any ideas?
check the link for another solution… do you have any ideas about how to make this hyperlinked?
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?
well, batch is nice because it’s just one file that runs in your directory… i found a little program called dir2html on pc-tools.net 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…