Keep Log & Temp Files Under Control With This Windows Script

junkpile   Keep Log & Temp Files Under Control With This Windows ScriptI’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.

tempinternet3   Keep Log & Temp Files Under Control With This Windows Script

 Create a Basic Task from the Action menu item.

tempinternet4   Keep Log & Temp Files Under Control With This Windows Script

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.

tempinternet5   Keep Log & Temp Files Under Control With This Windows Script

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.

tempinternet6   Keep Log & Temp Files Under Control With This Windows Script

To test out your script after you’ve created in in the Task Schedule, just click on “Action” and then “Run”.

tempinternet7   Keep Log & Temp Files Under Control With This Windows Script

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.

tempinternet20b   Keep Log & Temp Files Under Control With This Windows Script

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 

The comments were closed because the article is more than 180 days old.

If you have any questions related to what's mentioned in the article or need help with any computer issue, ask it on MakeUseOf Answers—We and our community will be more than happy to help.

15 Comments -

0 votes

Jon Smith

is this what programs like ccleaner do?

0 votes

Ryan Dube

In a way – yes. The approach here is much easier to customize and lets you clear out log files for just about any program, service, or application you may have running by simply adjusting the location of the log/temp path to clean in the script. For most users though, ccleaner will do a great job if you don’t feel comfortable with scripting.

0 votes

Márcio Guerra

This sounds a bit higher than my level of coding… Ehehehe! Ok, you give us the code, but, if we put a comma out of place wont we build some weapon of mass destruction? Eheheh! Hope not! Jokes apart, this seems a nice post, and helpful. What do you advise on a mac?

Cheers!

0 votes

Ryan Dube

There may be similar scripts you can write for Mac, but I wouldn’t know – I’ve never used one. :-)

0 votes

JimAV

On your MAC use Automator its in your applications folder. You can create all kinds of useful scrips Automator calls “Workflows”. You can also search for scripts created by others to do anything. Like update a twitter feed every time you clean up your computer.

0 votes

Márcio Guerra

That is awesome. I’ve noticed already automator, but I assumed it was some sort of batch processing for, I don’t know, burning CD, but not too complicated… Need to take a serious look to that! Very good!

Thank you JimAV and also thank you Ryan Dube. I believe that Ryan also got to know something from this post, eheheh! ;)

Cheers!

0 votes

Ryan Dube

Definitely – it’s the role of every writer to try and learn from his/her readers. :-)

0 votes

Ryan Dube

This is excellent – thank you Jim!

0 votes

Winston Leong

Thanks for sharing..

0 votes

Lisa Santika Onggrid

I schedule CCleaner to run everyday in my personal machine. Every once in a while I explore C drive to find temp files undiscovered by CCleaner.

0 votes

Nikhil Chandak

yea
I also run CCcleaner everyday

0 votes

Nancy B

I never knew I could set up that task in Windows 7….I was doing it manually about every other week.
Thanks for letting me know!

0 votes

Efi Dreyshner

Great way to handle the system!
But I am using Linux :P

0 votes

Artem

Yep, its quite funny to see windows users reinventing syslogd :D

0 votes

T D

Nice! I plan to use this regularly. Note that the following two lines…

For Each oSubFolder In oSubFolders
sDirectoryPath = “C:\Users\Owner\AppData\Local\Temp\” & oSubFolder

…need to be amended as follows:

Set oSubfolders = oFolder.SubFolders

For Each oSubFolder In oSubFolders
sDirectoryPath = oSubFolder

Also, in the code, change the & to & and the < to <

Thanks for this useful script!