Keep Log & Temp Files Under Control With This Windows Script
I’ve spent a lot of time helping out friends and family with their computer issues, and I have to say that above all other problems, the one issue that I see come up again and again are temp files and log files eating up shrinking disk space and eventually bogging down the system.
The temporary Internet files folder is a common culprit, but that’s one that has an easily solution, because all you have to do is set up the files to get deleted inside of Internet Options in the control panel. However, what about that pesky Windows temp folder, or all of those application log files that keep building up with random junk that never gets deleted? At first, those don’t cause much of a problem, but over time that accumulated junk turns into a massive pile of old files that serve no useful purpose.
Well written applications will delete log or temp files that are no longer needed, but too many programs out there don’t properly clean up after themselves – leaving you, after years, with a very messy computer. However, if you know of any particular log folders – whether it’s the Windows temp folder or application log folders, like an antivirus notification log folder or something like that, you can use the following Windows script to regularly clean up those log files that are older than a few days.
Clean Temp Files With Windows Script
If you’re new to Windows Scripting, take a quick look at the WSH tutorial I wrote a while back. There are lots of cool things you can do with Windows Script, such as changing network settings or automatically scheduling your antivirus or malware scanning software .
Obviously, if you want to schedule a cleanup routine to keep those application log files or temporary file folders under control, Windows Script is definitely the solution.
Writing a Cleanup Windows Script
This Windows script is going to focus on one particular directory, and go through that entire directory looking for files that have a modification date that’s older than a few days. It then deletes those files. Then, the script will go through any and all subdirectories and perform the same check and cleanup. Sound complicated? It’s not. The first part of the script looks like this:
Option Explicit On Error Resume Next Dim oFSO, oFolder, sDirectoryPath Dim oFileCollection, oFile, sDir Dim iDaysOld iDaysOld = 3
This section declares the file system variables that you’re going to use to access the directory and the files that you want to clean up. Setting up the “iDaysOld” variable tells the script the age of the files that you want to keep. In this case, I’m keeping any files that are newer than 3 days old.
Next comes the ultra-simple cleanup section.
' ***** CLEAR OUT OLD FILES IN LOG FOLDER ***** sDirectoryPath = "C:\Users\Owner\AppData\Local\Temp\" Set oFSO = CreateObject("Scripting.FileSystemObject") Set oFolder = oFSO.GetFolder(sDirectoryPath) Set oFileCollection = oFolder.Files For each oFile in oFileCollection If oFile.DateLastModified < (Date() - iDaysOld) Then oFile.Delete(True) End If Next
The section above connects with the Windows File System, and then connects to the directory that you’ve defined with the “sDirectoryPath” variable. This first loop goes through each individual file in the directory, checks the modified date and compares it to the age of the file that you defined. If it’s older than 3 days, it performs a deletion operation on that file.
This works great on files, but what about all of the subdirectories in folders like the Windows temp directory? This next section of the script will next file through all of the subdirectories, and perform the same file operations on the files in there as well.
For Each oSubFolder In oSubFolders sDirectoryPath = "C:\Users\Owner\AppData\Local\Temp\" & oSubFolder Set oFSO = CreateObject("Scripting.FileSystemObject") Set oFolder = oFSO.GetFolder(sDirectoryPath) Set oFileCollection = oFolder.Files For each oFile in oFileCollection If oFile.DateLastModified < (Date() - iDaysOld) Then oFile.Delete(True) End If Next If oSubFolder.Size = 0 Then oSubFolder.Delete(True) Set oFSO = Nothing Set oFolder = Nothing Set oFileCollection = Nothing Set oFile = Nothing Next
Finally, don’t forget to clear out the objects in the case where there weren’t any subdirectories to go through.
Set oFSO = Nothing Set oFolder = Nothing Set oFileCollection = Nothing Set oFile = Nothing WScript.Quit
It’s as simple as that. The script above will clean up any folder at all that you may want to clean. Write an individual script for each directory that you want to keep cleaned up on a regular schedule, set the “sDirectoryPath” to the directory you want to keep clean, and then store it in a directory like “C:\temp\” or “c:\vbscripts\”. Once you have those scripts set up, you’re ready to schedule those scripts.
Scheduling Your Cleanup Scripts
To schedule your cleanup script, in Windows 7, go to Administrative Tools and open up the Task Scheduler.
Create a Basic Task from the Action menu item.
Then, set up the recurring schedule to run whenever you’d like to clean up that directory. In my case, I run my cleanup scripts at noon on Sunday when I’m typically always logged in and working on my computer. The scripts just run in the background.
You’ll need to set up a scheduled task for each windows script you’ve written to clean up the individual log or temp directories.
To test out your script after you’ve created in in the Task Schedule, just click on “Action” and then “Run”.
You should see all of the files in that log or temp folder that are older than a few days (or however you set up your script) get deleted automatically. In my case, I clean up my Windows Temp folder on a weekly basis. After running this script, I saw about 45 files in the folder get chopped down to just about 20 or so of the latest files – including all of the files in the subdirectories.
It can be a real pain to maintain computers – and that job can get even harder when you have the system and all sorts of applications constantly writing to log files or building up junk temp files in the Windows temp folder. This script is especially useful for IT techs that might regularly run batch jobs or scripts on a server that all create new log files every time they’re run. By running a Windows Script that regularly cleans up the oldest log files, you can write WSF files like above that will keep those log directories nice and clean – you can keep a history of log files that you want, but clean up the really old ones that you don’t.
Can you think of some creative uses for such a script? Do you clean temp folders and log files manually – and might a script like this save you work? Give it a try and share your thoughts and feedback in the comments section below!
Image Credit: Crane at Junk Yard via Shutterstock
Affiliate Disclosure: By buying the products we recommend, you help keep the site alive. Read more.