Page 1 of 1

Print Spooler: Auto delete after 1 month?

Posted: 14 Feb 2012, 15:03
by Anakha56
Morning All,

Quick and simple question with a simple answer (hopefully).

On Windows Server 2003 is it possible to set up our printers to automatically delete spooled jobs after 30 days? If so how?

Thank you :).

Re: Print Spooler: Auto delete after 1 month?

Posted: 14 Feb 2012, 17:08
by hamin_aus
http://support.microsoft.com/kb/946737

Method D

But instead of just running the .CMD file once-off, create a scheduled job to run it every 30 days on your print servers

Re: Print Spooler: Auto delete after 1 month?

Posted: 14 Feb 2012, 17:40
by Ron2K
jamin, that's close to what he wants, but not exactly -- deleting all spooled jobs every 30 days isn't the same as deleting all spooled jobs that have been sitting there munching popcorn for 30 days. The batch file as a scheduled job is just as likely to nuke something munching popcorn for 30 seconds than something munching popcorn for 30 days. ;)

That being said, someone with coding knowledge could hack up a Windows Service that looks at %systemroot%\system32\spool\printers\*.shd and %systemroot%\system32\spool\printers\*.spl, examines file created/modified/whatever-you-want-to-use times, and does the steps detailed in the article if the age is above a user-defined threshold.

Re: Print Spooler: Auto delete after 1 month?

Posted: 14 Feb 2012, 20:06
by SykomantiS
This is rather interesting. If someone does write it, I'd like to look at the source code :)
I'll ask some folks at work about it.

Re: Print Spooler: Auto delete after 1 month?

Posted: 14 Feb 2012, 20:10
by Anakha56
For some reason I always thought that there was a setting one could set in Windows when you ask the printer to keep jobs, I could not find it so that's why I am asking here :). Will investigate jamins cmd prompt and try to make it a scheduled task :).

Re: Print Spooler: Auto delete after 1 month?

Posted: 14 Feb 2012, 21:16
by rustypup
SykomantiS wrote:If someone does write it, I'd like to look at the source code
:?

Code: Select all

<pseudo code>
file[] grabFiles(int days){
   select files from dir where datediff[dy,timestamp]>=days 
   filter file[] for regex(\.(?i:)(?:shd|spl)$) 
   return file[]
}
void flushOldQueue( 
   stopQueue()
   for(file f:grabFiles()) 
      if(!f is null&&!f is directory) 
         f.delete()
   startQueue()
)
</pseudo code>
this isn't rocket science... it's a kludge to do something the OS should be able to manage...

i'd still spend time validating file headers just to be sure, but how is it the spool queue doesn't support something so simple?

<edit>
actually, the API would be the second port of call, only resorting to kludgery when successive calls to EndDocPrinter fails to clear the job...

first prize would be a managed service provided by mickeysoft themselves...
</edit>

Re: Print Spooler: Auto delete after 1 month?

Posted: 14 Feb 2012, 21:36
by Bladerunner
Compile this C# code and let me know if it works. You can plug this app into your task scheduler I guess?

Code: Select all

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;

namespace S66SpoolClear
{
    class Program
    {
        static ServiceController service = new ServiceController("Spooler");
        static DirectoryInfo SpoolDir = new DirectoryInfo(@"C:\Windows\system32\spool\printers\");
        static void Main(string[] args)
        {
            StopSpool();
            ClearSpool();
            RestartSpool();
            Console.WriteLine("Done!");
            Console.ReadKey();
        }

        static void ClearSpool()
        {
            Console.WriteLine("Deleting files");
            foreach (FileInfo SpoolFile in SpoolDir.GetFiles())
            {
                if (SpoolFile.Extension == "shd" || SpoolFile.Extension == "spl")
                {
                    if ((DateTime.Now - SpoolFile.LastWriteTime).Days >= 30)
                        SpoolFile.Delete();
                }
            }
        }

        static void StopSpool()
        {
            Console.WriteLine("Stopping spool");
            service.Stop();
        }

        static void RestartSpool()
        {
            Console.WriteLine("Restarting spool");
            service.Start();
        }
    }
}

Re: Print Spooler: Auto delete after 1 month?

Posted: 15 Feb 2012, 03:56
by hamin_aus
Bladerunner wrote:Compile this C# code
:lol: Yeah because he has Visual Studio laying around :P

Why don't you compile it with any required DLL's zip it and upload it somewhere, Annie is not a developer and AFAIK his company doesn't employ any.

A lot of people are being very classy with their code, but no-one seems to be actually spoon-feeding little orphan Annie. So here goes :P

Code: Select all

net stop spooler
forfiles.exe /p %systemroot%\system32\spool\printers\ /s /m *.shd /d -30 /c "cmd /c del @file"
forfiles.exe /p %systemroot%\system32\spool\printers\ /s /m *.spl /d -30 /c "cmd /c del @file"
net start spooler
Caveats:
- make sure you run the batch as a user with permissions to stop and start the print spooler
- make sure you have forfiles.exe, win2k3 and later should have it, but if not get it here.

Re: Print Spooler: Auto delete after 1 month?

Posted: 15 Feb 2012, 07:13
by Bladerunner
I wouldn't just run any exe on my company's server. I'd rather read through the code myself and then compile it.

Re: Print Spooler: Auto delete after 1 month?

Posted: 15 Feb 2012, 07:17
by Anakha56
@jamin Thanks for the script. Will play around with it today :D.

@Bladerunner Dont worry about it. I may not be familiar with VB but my boss is so I will let him review the code and make the decision :).

@rusty They are making some improvements :). In server 2008 you can now arrange your spool jobs by time & date which makes life a heck of a lot easier its just 2003 that you want to burn... :?

Re: Print Spooler: Auto delete after 1 month?

Posted: 15 Feb 2012, 07:36
by rustypup
using the scripted appraoch also requries that you fiddle with sc query in order to verify if the service actually restarted, (unless you enjoy being flooded with tickets )....

<obligatory> CUPS knocks the M$ spool service into a cocked hat while blind drunk and being savaged by a rodent.. </obligatory>

Re: Print Spooler: Auto delete after 1 month?

Posted: 15 Feb 2012, 07:38
by Anakha56
My personal preference is the bat file. I can run that manually when needed.