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 …
Hello everyone! I do not know how much change to highlight the name of my file and I do not know whether to change anything in the words of French style. Y he means to make amends and see one without that works, thank you for your response!
Jerome …
Worked like a charm!
I set it up as a scheduled job to delete very large files (50GBs) from a backup.
Thank you.
I’m getting a Permission denied error
Line: 10
Char: 13
Error: Permission denied
Code: 800A0046
Source: Microsoft VBScript runtime error
Any ideas?
Thanks
Changed it a bit to delete .bak files older than X min. Now, I need to calculate the size of all files to be deleted and redirect output of name of files deleted and their size into an output file. I need to run the script more than once per day via scheduler. The output file should not be overwritten every time I run the vb script. Can you please assist? Thank you!
Hi Neolim, Please tell me how you modifed this script for .bak. files.
i am also wants to do same thing please help me.
Here is nicer version of your script:
strFolder = “C:Folder”
Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Set objFolder = objFSO.GetFolder(strFolder)
Set colFiles = objFolder.Files
For Each objFile In colFiles
If DateDiff(“N”,objFile.DateLastModified,Now()) > 59 Then
objFSO.DeleteFile(objFile),True
End if
Next
tom yours works to but you use the wrong ” how the script works;)
strFolder = “C:Scripttest”
Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Set objFolder = objFSO.GetFolder(strFolder)
Set colFiles = objFolder.Files
For Each objFile In colFiles
If DateDiff(“N”,objFile.DateLastModified,Now()) > 59 Then
objFSO.DeleteFile(objFile),True
End if
Next
if you would like to use the script to delete files older then the x day change the N to D like so:
strFolder = “C:Scripttest”
Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Set objFolder = objFSO.GetFolder(strFolder)
Set colFiles = objFolder.Files
For Each objFile In colFiles
If DateDiff(“D”,objFile.DateLastModified,Now()) > 1 Then
objFSO.DeleteFile(objFile),True
End if
Next
where the 1 is now olderd then 1 day change this to 14 then it delets files older then 14 days
If DateDiff(“D”,objFile.DateLastModified,Now()) > 1 Then
If DateDiff(“D”,objFile.DateLastModified,Now()) > 14 Then
Awesome! Really great job, dude!
Nice script…can you guide me through if Date is in a certain date range then create a hyperlink to the file, otherwise no hyperlink. This would be great…!
Thanks!
Nice script…can you guide me through if Date is in a certain date range then create a hyperlink to the file, otherwise no hyperlink. This would be great…!
Thanks!