How to Save a Folder’s Contents to a Text File

Quick Tips

I just finished a Flash movie that loads PNGs from a particular site folder.  In this case, the list of desired files is determined by an Array instance.  There are fifty-three files in this folder, and I didn’t relish typing out the names of each file by hand.  Old DOS trick to the rescue! 

If you’re in Windows, open a Command prompt and type:

dir folder to list > file to print to

… then hit Enter.  What you get is a text file of the typical results of running dir in a DOS window; in other words, a listing of all the files.  If your folder names have spaces or are long, wrap your paths in quotes.

For example, this lengthy string …

dir "c:\\Documents and Settings\\<user name>\\My Sites\\sample\\images\\" >
 "c:\\Documents and Settings\\<user name>\\My Sites\\sample\\output.txt"

… puts a text file named output.txt into the root of the sample site.  That text file lists all the files in the sample\images\ folder.  You can copy paths from the Address field of Windows Explorer and past them into your DOS window by right-clicking on the window’s title bar and selecting  Edit > Paste.

Note, the resultant text file isn’t especially edit-ready.  You’ll get something like the following:

09/07/2006  04:15 PM   <DIR>    ..
09/07/2006  04:11 PM     85,143 1111bs-rstand.png
09/07/2006  04:11 PM    125,863 1515rvnt.png
09/07/2006  04:11 PM     95,544 1libsite.png
09/07/2006  04:11 PM    133,780 1libvent.png

So if you want file names only, use something like MS Word to replace the series of spaces between the date/time stamp “column” and the file size/name “column” with a tab character, then convert the text to a table, split on tabs.  Delete the first column, convert back to text, then convert back to a table, split on a space character.  Delete the first column again, then convert back.

Sounds tedious, I admit, but it sure beats typing out all those file names by hand.  If you’re into regular expressions, it can be a trivial matter to select only the file names.

In my case, I used regex to “rip” my file names and convert them into a series of Array.push() lines …

images.push(imagePath + "1111bs-rstand.png");
images.push(imagePath + "1515rvnt.png");
images.push(imagePath + "1libsite.png");
images.push(imagePath + "1libvent.png");

… all in one swoop.

Update!

Some of my blog posts are also published on the Community MX blog, and one of our subscribers commented there with a fantastic tip.  The dir command can be instructed to spit out file names only with a simple switch, /b.

dir /b c:\\path\\ > c:\\output.txt

Now there’s no need for the MS Word/regex massaging. Thanks, Craig!

Leave a Reply