Fred's Deep Blush (Deleting TMP Files) Pt1
Maybe I was lost in a "forest through the trees" problem. Maybe I just spaced out. But in any case, in "Free Tool & Demo From Fred" ( http://langa.com/newsletters/2006/2006-04-13.htm#1 ) we discussed a problem designed to (1) illustrate the general principles of automating some commands that normally resist automation; and (2) specifically, to clean up errant TMP files that were accumulating in a reader's C:\ folders.
I remembered from my batch-file programming days something called a "For...In...Do" loop, which allows for repeated execution of a command on a set of files. The skeletal form is:
FOR %thisvariable IN (thislist) DO thiscommand
In the template above, "thiscommand" (whatever it might be) will be performed once for every item in "thislist." The %thisvariable represents a special variable that the batch processor will substitute with one item at a time from (thislist) until the end of the list is reached. In typed-in commands, that special variable is indicated with a single % sign as shown. Inside batch files, it's written with double percent signs (%%), but it's otherwise exactly the same.
Here's how it works in real life: If (thislist) is a list of files in a directory, for example, the FOR part of the command would take the file names IN (thislist), one at a time, assign them to (thisvariable) and then DO whatever command you specify on each file, in turn. Thus, the specified command will be performed once on each file in the directory, in this example.
And because the command is executed once per item in (this list), it gets past the "no wildcard" limitations of powerful commands like RD--- Remove Directory: Using a FOR...IN...DO loop will issue one RD command per item in (thislist), with no wildcards needed.
So, when we got the reader letter asking about deleting a group of TMP files from the C:\ directory, I set about trying to make it work with a simple one-line batch command based on the FOR...IN...DO structure.
I still don't know why, but I couldn't make it work. I tried the copious help built into XP (go to Start/Run, type COMMAND on the Run line to open a command window, and type "FOR /?" (without the quotes) for several screens of help.) I tried web sites. ( http://www.google.com/search?hl=en&lr=&safe=off&q=for+in+do+batch ) I even blew the dust off a 1990 edition of "DOS Power Tools;" a massive 1300 page tome that was state of the art 16 years ago, and that covers batch programming in abundant detail. <g> The "For...In...Do" trio is covered on page 568, and my copy of the book naturally falls open to that page!
With all that, you'd think I could get the TMP deletion to work. But nothing I tried worked, and after an increasingly frustrating while, I assumed (!) I'd run into a special condition or prohibition in XP--- maybe related to working in the root folder. So, with the newsletter deadline approaching, I opted for the longer but more certain Plan B (using an external tool) which was what I published in http://langa.com/newsletters/2006/2006-04-13.htm#1 . That, at least, I could get to work, for sure; and the info there is correct, as published.
But the limitation was mine, not XP's, There indeed is a simpler easier way, via a For...In...Do command, as a number of readers gently informed me. (See next items.)
I still have no idea what my blind spot was in trying to build that command. I suppose the silver lining is that now you've seen an even wider range of approaches to solving this kind of problem, and a very real--- albeit personally embarrassing--- example of how a Plan B can still quite adequately get the job done when Plan A just won't work for whatever reason.
But I apologize for my blind spot.
Anyway, see the next items for several very slick examples of reader-crafted FOR...IN...DO loops.
