Tuesday, January 1, 2008

Code Grinder 1: File Enumeration

Code Grinders: File Enumeration

So, a buddy of mine was talking to me the other day, and he was needing to get all the names of the files in a certain directory. I told him, easy, just use the 'Code Grinder' File Enumeration that's in the main QM Toolbar under 'Files, Web'. A 'Code Grinder' is a term I came up with to describe dialogs that help you come up with pre-formatted code with just a little bit of info from you. In the same way that you drop some meat, garlic, pepper, salt, sage into the hopper, turn it on and shove it all down the grinder's throat. It's the same here in QM. The grinder is usually a dialog of some kind and the meat and spices are locations or variable names.

Let's take a look at the File Enumeration grinder and see what we can come up with (or is that, ...'see with what we can come up'....no, that's not right either... ummmm....'see what can come up from ourselves'...yeah, that's totally not the right direction....man, I hate that whole, "you can't end a sentence with a preposition" thing...is it any better going on and on like this at the end of a sentence rather than a preposition?). Anyway, so start up that dialog and lets take a look. Click the File Enumeration icon in the 'Files, Web' menu and here's what we've got.

As you can see, it's a pretty straight forward dialog but there are some real useful features. First, you can browse to the folder you are looking for or you can choose 'Special Folders' that will be the same on all computers. It looks like this.

I've circled some of the things that are a real pain when moving from computer to computer, but with 'Special Folders' you can make it quite easy to get to those directories no matter who is logged in (doh, preposition again!). You can set date/time qualifiers as well as 'this folder/subfolders' options as well. Now where this grinder really gets cooking is in the 'Variables' section. If you need just filenames, just put in your variable name in the field. If you need the path, just put in your variable name in that field. If you need the size etc, etc, etc.

Here's the code I get from asking it to list the filenames on the desktop.

Dir d
foreach(d "$Desktop$\*" FE_Dir)
str sPath=d.FileName()
out sPath

Go ahead and dump that into your QM and see what happens. It's that easy. Now 'sPath' has the filename of each file as it loops through the directory. So, now you have a way to get some or all that info automatically (or at least so close to automatically that it makes little difference).

No comments: