So, I am constantly looking for ways of automating tasks. Too many admins do not take advantage of scripting and scheduled tasks/cron. Just this last week, I was implementing a new print server. Besides just building up the new server, I wanted to actually offer the users something new and useful.
I’ve been wanting to setup a network pdf printer for quite some time. I have played around with setting up a network PDF printer using cups. However, we seem to be so MS centric these days that I decided to use PDFCreator‘s print server. It was really a piece of cake. Just install the server portion, setup the service, create a share and watch the PDF’s spool.
I quickly found that the folder where PDF’s were written to, was quickly filling with PDF’s as users were not removing them. So, the solution was to write a little vbscript to purge any files older than an hour. There were two things I wanted:
- I have a file named “!FILES ARE PURGED AFTER ONE HOUR!”. I did not want this file removed. It serves as a warning for uses.
- I did not want to purge the folder every hour. I wanted to remove any files that were one hour old or greater. That way, if a user creates a PDF at 2:59pm, the 3:00pm run won’t delete it. It will be deleted on or after 3:59pm.
Here is the script I came up with:
strFolder = "C:\Folder\" Set objShell = CreateObject("Shell.Application") Set objFolder = objShell.Namespace(strFolder) Set objFSO = CreateObject("Scripting.FileSystemObject") For Each strFileName in objFolder.Items If len(objFSO.GetExtensionName(strFileName)) > 0 Then Set objFile = objFSO.GetFile(strFolder & strFileName.Name) If DateDiff("N",objFile.DateLastModified,Now()) > 59 Then objFSO.DeleteFile(strFolder & strFileName.Name),True End If End If Next
The great thing about this is that you get a free network PDF printer that can be left alone. Your boss thinks you are a genius and there is no sweat on your brow.
Cheers!
Thanks! This is what i’m looking for and it works great! ^_^
I like your script, but how do I make it delete files older then 90 days not mins?
The trick is in the line:
If DateDiff(“N”,objFile.DateLastModified,Now()) > 59 Then
If you look-up the DateDiff function, it takes a few arguments. The first is the “N” argument which says, “get the difference in minutes.” All you need to do is change that to “D” for days. Then, change the 59 to 90. It will now only delete files older than 90 days.
hi there,
i tried the vbscript, but i get a error
0 Then
Set objFile = objFSO.GetFile(strFolder & strFileName.Name)
If DateDiff(“N”,objFile.DateLastModified,Now()) > 2 Then
objFSO.DeleteFile(strFolder & strFileName.Name),True
End If
End If
Next
%>
Output error:
Microsoft VBScript runtime (0x800A01A8)
Object required
/axima/download2.asp, line 10
what’s wroing? I only changed the folder
I like your script too! How about recursive delete through sub-folders?
Hello,
How do I add a line to tell script to delete the folder path only PDF files?
thank you for sharing this script …