Free Tool & Demo From Fred

Hi Fred. I've been a subscriber to the Langa-List since its inception, and a long-time Plus member. I used to enjoy the articles you wrote for Windows Magazine all those years ago!

I've got a problem I'm hoping you can help me out with. I've been unable to find a resolution to my problem using Google searches, which is unusual in its own right as I'm usually able to find things where others fail!

I'm in charge of image management for our company, and we recently rolled out the first 700 notebooks with Windows XP SP2. What we're finding is that the harddrives seem to be getting polluted with empty temp directories as clients use the machines, all with a similar name - eg: "1a3f2.tmp", "3g6w6.tmp"

Please note that these are directories (folders) NOT files, all ending with ".tmp"
I'm trying to work out some way of deleting all of these .tmp directories that are being created in the root of the C: drive and root of the D: drive. I've been unable to find out what process is creating these tmp directories. If I knew that I'd be able to figure out a solution based on the application itself.

The standard DOS commands don't work because the RD and RMDIR commands don't support wildcards.

I've already got a scheduled task that cleans up temporary files created by our VPN client, and I'm hoping to find some way to update this batch file to remove these ".tmp" directories as well. This way I can push it down to all of the existing XP Systems using our Patchlink utility. Any help you could offer to find a solution to this would be greatly appreciated. --Don

We'll come back to a possible cause in a moment, but first let's look at the problem at hand: Automatic deleting of folders. It's a little tricky--- deliberately so, because it's a potentially dangerous thing. (Delete a folder, and the folder goes away, along with its contents, including the contents of any/all subfolders inside the deleted folder--- one command could wipe out an entire hard drive!)

But there's a workaround that's not too hard to cobble together, especially if you know where the files are, and what the format of their names is. (As is the case here.)

The first step is to gather the names of all the folders you want to target for deletion. This simple command will output a list of all the *.tmp folders and files in the current directory to a single, plain text file called "delme.txt:"

dir /b *.tmp >delme.txt

The "/b" switch tells the dir command to do a basic listing--- just the matching names, with no header, date, filesize or other info. In other words, delme.txt will contain just the names of folders (and files) with the tmp extension--- and that's just what we need.

As Don says, the command to remove a directory is RD. To tell RD to include all subdirectories, and to do so in quiet mode (without stopping to ask for confirmation), you add the /s (for subdirectories) and /q (for quiet) switches. In Don's specific case, he knows the folders are empty, so the /s switch isn't really needed. But adding it ensures that the tmp folders will get whacked, even if they do contain something.

So (bear with me, now) in manual mode, the full command to delete a folder called (say)  "3g6w6.tmp" would be:

rd /s /q "3g6w6.tmp"

The quotation marks also aren't essential in this example, but they're good to include because they allow for names that might have spaces in them that otherwise might mess up the command.

So, now that we know the format of the commands we need, all we need is a simple script or program that will take the names of the tmp folders listed in delme.txt and insert them into a series of properly-formatted RD commands.

In other words, we need a tool that will prepend

rd /s /q "

before each name, and append a single close-quote (") after each name. It'd be nice if the tool could then either perform the newly-constructed commands directly, or output them in a new list, perhaps in batchfile form for automatic execution by the operating system.

Here's a very quick and dirty demo, again using Don's examples. It has three parts:

First, there's a batch file I've called "DeleteTempFolder.bat" that contains these core lines:

c:
cd \
dir /b *.tmp >delme.txt
deltmp.exe
delme.bat

The first two lines switch to the C: drive, and to the root (\) directory, where Don's tmp folders live. You could edit the batch file to work on *any* folder where tmp files live, however.

The next line we've seen before, above: It simply outputs a basic listing of all the *.tmp files in the current directory. the listing is called "DELME.TXT" and is a generic text file; nothing special about it at all.

The next line runs a crude little purpose-built demo tool I ginned up called DELTEMP.EXE. It's an unadorned program that runs with no interface at all--- it's hardwired to look for a file called DELME.TXT in the C:\ directory, and if it finds it, to take each name in DELME.TXT, wrap it with the RD commands as shown above, and output the processed names in a new batch file called DELME.BAT.

The next line in DeleteTempFolder.bat then calls the newly-created DELME.BAT, which then performs the actual folder deletions.

The only two files you need to make this work are DeleteTempFolder.bat and Deltemp.exe; the other two files--- delme.txt and delme.bat--- are automatically generated each time.

To give you a starting place, DeleteTempFolder.bat and Deltemp.exe are available here, for free:

http://langa.com/extras/deltemp/deltemp.zip

(If clicking doesn't work, right click and "save as.")

Download the Zip file and uncompress the two files inside it to C:\ . Run DeleteTempFolder.bat, either manually or via Task Scheduler: It will gather the names of the all tmp folders in C:\ and delete them as above. (If your C:| doesn't have any tmp folders in it, you can create some fake ones first to see the demo in action.)

Again, this is a quick and dirty approach--- inelegant, but effective, and intended AS AN EXAMPLE of what you can do. As such, it's a "use at your own risk" thing and is NOT intended as a finished, production-quality tool--- it's a long, long way from that! <g> Your tools and preferences may differ; with more time and effort, and better tools, you can come up with a far slicker final product.

But the above should give you a running start, and (hopefully) spark further ideas you can use to make whatever tool you need for your exact circumstances.

Help people find this article on the Web (explain):

Get the latest on Windows.

Enter your e-mail address to receive the free Windows Secrets Newsletter weekly.


For instance: jan@example.com


All subscribers are covered by our Ironclad Privacy Guarantee:

1. We will never sell, rent, or give away your address to any outside party, ever;
2. We will never send you any unrequested e-mail, besides newsletter updates; and
3. All unsubscribe requests are honored immediately, period.  Privacy policy