We help IT Professionals succeed at work.
Get Started

VBScript to Kill print jobs

2,713 Views
Last Modified: 2015-11-19

Hi,

I am trying to get a vbscript to run in the background and delete any print jobs spooled that are over 1hr old (on XP SP3 Machines)

I have this script that will successfully delete files older than 1hr

*******************
sFolder = "c:\windows\system32\spool\printers"
iMaxAge = 1
Set oFSO = CreateObject("Scripting.FileSystemObject")
If oFSO.FolderExists(sFolder) Then
for each oFile in oFSO.GetFolder(sFolder).Files
If DateDiff("h", oFile.DateLastModified, Now) > iMaxAge Then
oFile.Delete
End If
next
End If
**************************

and I have scripts that "should" stop and start the "print spooler" service


**************************

'stop spooler
strServiceName = "print spooler"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery("Select * from Win32_Service Where Name ='" & strServiceName & "'")
For Each objService in colListOfServices
    objService.StopService()

**************************
'start spooler
For Each objService in colListOfServices
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery ("Select * from Win32_Service Where Name ='" & strServiceName & "'")
    objService.StartService()
**************************


However I seem unable to stitch this togther so that the print spooler is only stopped if there are files over 1hour old, then the delete kicks in, then restart the service.

I do not want to stop the service unless there are files over 1hr old to delete.

Can anyone point me in the right direction why I cant seem to get this to work?

Thanks


Comment
Watch Question
Software Engineer
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 29 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE