Listing Folder Contents to a Text File
My brother in law Matt (take a bow buddy!) asked me a good one the other day. He had a directory (I still call them that, I know, they’re called Folders now) which contained a number of files that he wanted to be able to print a list of.
No, I don’t know why he wanted it either but it’s a good theoretical example that can show off how easy this kind of stuff can be from my homeland, the command line.
Once more, unto the DOS dear friends….
In order to get a list of files whenever we find ourselves on the rocky shores of the command line (you know, the place you go if you type cmd in the run box) you use the very handy and occasionally powerful dir command.
Dir does what it says on the tin, it gives you a directory list of the files in that directory. Where it gets powerful is when you combine it with the switches it can use. A switch is a special parameter you pass to the command to tell it how to behave. With most commands you can get a list of their switches and their uses by following the command with the /? switch (Eg dir /?).
Lets just go straight for the example I gave Matt. The commands will take you to a directory on the C drive of the PC you’re on (in this case we want the contents of the C:\Music directory) then list the contents of that directory into a text file for you.
C:
Cd\
Cd Music
Dir /b >C:\EdsExampleFile.txt
This is readable and doesn’t come with all the details of the files (making it a handy list for other purposes). It does however list any child directory names too. To get it even more useful, we can use more switches in the Dir command line to make it hide those folder names.
The /a switch is used to list files that have a particular attribute set (that it’s a directory, a hidden file, read-only, that sort of stuff). To list all directories you’d type dir /ad. Here we don’t want them so the command line should now be:
Dir /b /a-d >C:\EdsExampleFile.txt
Oh, and if we just wanted a list of the .mp3 files in that folder (see, what I did there? Using the right term) you’d go:
Dir /b *.mp3
Easy peasy!