Solved

Windows batch or VB script : check if oldest file in folder is more than 50 secs

Posted on 2013-01-13
4
623 Views
Last Modified: 2013-01-15
Print jobs in our Win 2003 occasionally got stuck.  Generally, a print
job spooled will be printed within half a minute & at any one time,
we've always seen one print job that's causing the problem (not
seen any multiple print job causing problem at any one time yet).

A print job will create one .spl & one .shd file to the folllowing folder:
   C:\Windows\System32\Spool\Printers

For this example, say, we have 3 print jobs:
abc.spl & abc.shd
bcd.spl & bcd.shd
cde.spl & cde.shd

I'll need a script (that I'll run every minute using Windows scheduler)
to check for the oldest (modified date/time) .spl to see if it is more
than 50 secs old.

If the oldest .spl file (take for example it's abc.spl in this case) is
more than 50 secs old, the script will

a) move abc.spl & abc.shd to c:\temp\stuckjob\  folder   &
b) move the rest of the files (in this case, it's bcd.* & cde.*)
    to c:\temp\goodjob\  folder
c) then restart the print spooler (ie issue 'sc stop spooler'
    followed by 'sc start spooler'
d) move back c:\temp\goodjob\*.* to
    C:\Windows\System32\Spool\Printers   folder
0
Comment
Question by:sunhux
  • 2
4 Comments
 
LVL 95

Assisted Solution

by:Lee W, MVP
Lee W, MVP earned 20 total points
ID: 38772185
Not going to do this with a batch file since a batch file cannot check a file time to the second (without some other utility to look at file properties).

I wouldn't be looking to do 50 seconds ... why not every minute - something (relatively) easy?

Also, why do this at all.  Print jobs should not regularly get stuck.  If you're having problems, I'd fix the problems, not improvise a long term work-around to the problem.
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 460 total points
ID: 38772466
Correct, a cmd batch would not allow for that granularity. VBS and PowerShell do, however. Nevertheless, I have to agree to what Lee said above - better to resolve the issue than use a flaky workaround.

PowerShell:
set-location C:\Windows\System32\Spool\Printers
get-childitem -filter '*.spl'|
  where {  ((get-date) - $_.LastWriteTime).TotalSeconds -gt 50 } |
  foreach-object {
    move-item $_, $_.Replace('.spl', '.shd') -Destination c:\temp\stuckjob\
    move-item * -Destination c:\temp\goodjob\
    restart-service Spooler -force
    move-item c:\temp\goodjob\* .
  }
}

Open in new window

0
 

Author Comment

by:sunhux
ID: 38773456
Not quite regular affair but about once every 2-3 months, we have
one incident of a job being stuck & held up all other jobs in the spooler.
No specific error code in Event viewer too.

Is PowerShell available in Win 2003 or I need to install a PowerShell?
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 38773644
You'll need to download it. Windows Update should provide it as optional update.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now