HOME
TOPICS
ABOUT ME
MAIL

 
Windows won't let you delete files that are currently used by programs.
  technofile
Al Fasoldt's reviews and commentaries, continuously available online since 1983

Write your own temp-file cleanup program



   
Sept. 3, 2000

By Al Fasoldt
Copyright ©2000, Al Fasoldt
Copyright ©2000, The Syracuse Newspapers

   We all need a place to put stuff temporarily. I use my dresser drawer, much to the dismay of my wife. I have to clean it out now and then just to be able to store more junk in the same place.
   Windows has its own dresser drawer. It's called the Temp folder. Unlike me, Windows is totally lazy. It never cleans out its storage area on its own.
   So you have to do it yourself. Otherwise the Temp folder will fill up with hundreds or even thousands of files. This wastes disk space and slows down Windows.
   In previous articles (How to get rid of the junk Windows leaves behind, A batch file that deletes the Windows temp folder and recreates an empty one and Batch file that spares current files and folders while deleting contents of the TEMP folder) I've described both plain and fancy ways to clean up the Temp folder yourself. Those articles are on my Web site along with the actual code I wrote to do the job. If you're used to working behind the scenes in DOS and Windows, you might think this would be simple -- you just select everything in the Temp folder and delete it. But that doesn't work reliably.
   Here's why. Windows won't let you delete files that are currently used by programs. In other words, it won't let you get rid of files that are in use. This is a double problem. First, files that are in use can't be deleted, of course. Second, in the directory list Windows shows you, all files BELOW the one that is in use can't be deleted, either. (Windows can't figure out how to skip one file and delete the rest; it just stops dead when it hits the first problem. Microsoft has had a monopoly on Windows operating system software, so there's been no market pressure to fix this senseless bug.)
   Another problem -- and it can be a big one -- comes from the fact that programs use the Temp folder to store vital files. You could lose important stuff if you tamper with the Temp folder while Windows is running.
   The best way around this takes advantage of the old-fashioned way Windows starts up. When you turn on your computer, Windows relies on a method used since the early 1980s to run the operating system. It looks for two files that contain instructions. If it finds them, it follows each command before starting up.
   Those two files are AUTOEXEC.BAT and CONFIG.SYS. The one we're going to use is AUTOEXEC.BAT. This method will work if your PC uses Windows 95 or Windows 98. (I haven't tried it under later versions of Windows.)
   Here's how we do it. Before Windows starts up, our changes to AUTOEXEC.BAT tell the DOS operating system to clean out the Temp folder. This works MUCH better than trying to clean out the Temp folder while Windows is running. Then Windows starts up with a clean, empty Temp folder. Your PC regains its former speed and gets back a lot of storage space.
   But I'm giving you fair warning that this method is not foolproof. If you are installing a program and you see a message that tells you to restart Windows or reboot the computer, that program could be storing something very important in the Temp folder to use when Windows reboots. (This is stupid and should never be done, but even Microsoft's own software does it.)
   So you can't ever assume that it's OK to clean out the Temp folder during a reboot. If you're installing a new program, obviously that's a bad time. If Windows is doing some of its own installations, that's also a bad time. Use my cleanup method ONLY at the first bootup of the day. Don't use it at any other time.
   Edit AUTOEXEC.BAT by using the RUN command in the Start Menu and typing SYSEDIT and pressing Enter. Simply put these lines at the bottom -- they MUST be at the bottom -- of the AUTOEXEC.BAT file, then close the SYSEDIT window and save the file. (SYSEDIT will ask if you want to save it.)
   
   echo "This will eliminate everything from the Temp folder."
   echo "If you have just installed a program and rebooted,"
   echo "press Ctrl-C to cancel this step."
   pause
   c:
   cd windows
   deltree /y temp
   md temp

   
   There is a single space after "deltree" but no space between "/" and "y" in the third line.
   That's all there is to it. Save AUTOEXEC.BAT and reboot to test the code.
   The code shows a message on the screen using "echo," then establishes the C: drive as the location for commands. It changes directories to Windows and deletes the entire "tree" (set of folders) in the temp folder. It then makes a directory called temp to give Windows a fresh, blank Temp folder. (Windows doesn't care whether names start with capital letters.)
   These instructions won't work if the Windows folder on your computer is called something else. (I suppose you might have one called "WIN95" or something like that. If you do, use the name of your Windows folder instead of the word "windows" in the instructions.)
   They also won't work if your PC's Temp folder is not called "Temp." (It's always called "Temp" unless you or someone else changed it.) Change the instructions to match the actual name if you have to.