Tuesday, October 07, 2008

TechTip: Getting a List of Files

As mentioned in my previous post, there are certain tasks that can be accomplished more effectively and efficiently using a command line interface versus a graphical user interface. Here's one scenario;

Let's suppose you work in a office and you're part of a large team working on a project where everyone stores and shares their files on a networked file share. Your manager has given you a task to compile a list of files into a spreadsheet in order to create an documented file index or checklist.

How would you do this?

If you were using a GUI, you couldn't just select all the files you need and use a hypothetical "get list" command (at least as far as I'm aware there isn't any standard feature like this that exists in Windows, Mac, X Windows, or any major GUI). Even copying the selected files and pasting into a text editor or spreadsheet just simply doesn't work.

Probably the most obvious answer for most GUI-trained people would be to simply type out each filename one at a time, or use the rename feature to copy the filename text and paste into a text document or spreadsheet. This is probably a fair solution if you only had to deal with a small number of files, but it is a bit error-prone... and what if you had a large number of files, like over 20 to possibly a few hundred or more? Unless you had several people at your disposal helping you to split up the work, this would be a pretty tedious task, and could take anywhere from a few minutes to a few hours or even the whole day to complete.

Solution:

Command Line to the rescue! You can accomplish this quickly and easily using the command line.

In Windows, fire up the DOS console by going to Start -- Programs -- Accessories -- Command Prompt, or Start -- Run, enter "cmd", and click OK.

Let's say the file share is mounted to drive letter S:, and all the files that you need to get a list of are under the path "\teams\myteam\projectx".

Use the "cd" (or "chdir") command to change into that directory:

cd S:
cd \teams\myteam\projectx


Doing a "dir" command will output a list of the contents of that folder. You could now theoretically use the Edit -- Mark & Copy commands available in the Window icon menu at the top left of the Command Prompt window (maybe even use the "dir /p" switch to pause at each screen)... this is certainly more efficient than typing or copying/pasting each filename, but there's actually an even better way yet...

Using the "dir /b" switch will give you a "bare" list of files without the columns for modification date, filesize, etc.

Combine this with the file redirection pipe ">", and this will instantly generate your list -- no fuss, no muss!

dir /b > filelist.txt

If you want to get fancier, you could use additional switches and wild cards with the dir command...

First you can construct and issue the dir command on it's own to preview the results on the screen.

dir /b /s 2008*.doc

The above command gets a bare listing, includes files in subfolders where the filenames begin with "2008", and have the ".doc" file extension.

If you are happy with the results, you can then add the portion of the command to pipe the output into a file instead of outputting to the screen.

dir /b /s 2008*.doc > filelist.txt

Once you this is done, you can close the Command Prompt window (or enter the "exit" command).

Going back to the S:\teams\myteam\projectx folder, you will now find a file named "filelist.txt" has been created. Open this file, and there you have it -- a plain text list of your files that you can just copy and paste into a spreadsheet or use in any way you like.

For Linux, Mac OS X or any other Unix-like Operating Systems you can use the equivalent "ls" commands.

ls -F1 > filelist.txt
ls -F1 -R 2008*.doc > filelist.txt

etc.

Now you can show off newly discovered superhuman productivity skills to impress your boss & coworkers. You're due for a raise! ;-)

No comments: