Avatar of sunhux
 asked on

Windows batch or VB script : list out files older than 2 mins & move out of spool folder

I have a requirement on our Win 2003 & 2008 servers:
if print spool files in a folder is older than 2 minutes, I
need to move the files out to another folder.

say the folder is c:\windows\xxx\spool\ ,
I wanted to move out files older than 2 minutes (up to
the second if possible) from the folder & then move
files less than 2 minutes old to yet another folder,
restart spooler service & move back files (of less
than 2 minutes old that were moved out earlier)
back to the spool directory
Windows BatchVB ScriptScripting Languages

Avatar of undefined
Last Comment
Steve Knight

8/22/2022 - Mon

Peter Kwan

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

Thanks very much.

Pardon my lack of knowledge of VB script (which is almost zero):
I intend to use Windows scheduler to run this script every minute
so this script should not loop continuous.

Also, are you able to modify this script such that files in the spool
folder that are of creation date older than 60 seconds (& let me
know which value is that so that I can tweak this 60 secs value to
say any other value like 50 secs to suit my requirement)
Steve Knight

Was about to post an example an hour ago but just refreshed and saw the above.  Was going to be similar lines, but also pointing you to a way of doing it from batch if you prefer, though getting VBScript to do the maths makes it easier, i.e. like this script of mine here to find 20 minute old files in Batch:


You've got a solution there by the looks so not going to duplicate.

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes

>> If DateDiff("n", oFile.DateLastModified, Now()) > 2/1440
Can see that you used 2/1440 for 2 minutes;  if it's files older
than 50secs, how is this coded?

'DateLastModified' is used.  If we wanted to base on date/time
of creation, what's the variable to use ?

after moving files (those older than 2 mins & lesser than 2 mins
out), can you insert something into your script to issue Windows
commands to restart the spooler, ie
   net stop spooler
   net start spooler

Lastly, someone gave me a script before (which could email to
notify if spooler has a problem).  Are you able to incorporate
the portion on emailing out to notify our operators if files
older than (say 1 minute) is found?  Below is the script (which
doesn't quite meet what I needed but it does work in sending
email) :

strComputer = "."
strService = "Print Spooler"

Set objEmail = CreateObject("CDO.Message")
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Service where DisplayName = '" & Service & "'")
For Each objItem in colItems
If objItem.status <>"OK" or objItem.state <>"Running" then
objEmail.From = "Email Address"
objEmail.To = "Email Address"
objEmail.Subject = "Print Spooler Service"
objEmail.Textbody = objItem.DisplayName & " on: " & strComputer _
'" State: " & objItem.State & VbCrLf
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
End If

Hi Steve,

Your url http://scripts.dragon-it.co.uk/links/batch-files-older-than
is inaccessible.  I certainly would like to have a look at Windows
batch script as well (as it's something I can try modifying/tweaking
but I certainly have zero comfort level modifying a VB script).

If you can't bring up scripts.dragon-it.co.uk , perhaps paste the
codes here

Managed to browse the url Steve gave now:
just one question, if I wanted to go for 30 secs,
would it work if I amend it to:
if timediff >=0.5

The batch script still needs quite a bit of modifications
before it could address my issue

Pasting the scripts below in case the website became inaccessible again:

@echo off

REM Error log kept here
set error="errorfile.txt"
del %error% 2>NUL

REM Change filespec here to what to look for
set filespec=*.txt

REM dirlist.txt is list of each dir, one per line:
for /f "tokens=*" %%y in ('type dirlist.txt') do call :checkdir "%%y"

REM Check file is above certain size (may well be a few bytes per dir due to blank lines from output of commands even if no errors.
call :checkfilesize %error%

exit /b

for /f "tokens=*" %%a in ('dir /b /a-d /od "%~1\%filespec%"') do >>%error% cscript //nologo checkfiletime.vbs "%%~fa" & exit /b
exit /b

if %~z1 LSS 20 echo There was no error & exit /b
type %error%
echo Now need to send this by email or whatever

REM Save this as Checkfiletime.vbs
DIM filename, filesys, file1, timediff

If Wscript.Arguments.Count = 0 Then
Wscript.echo "9999 Error - no file specified"
Set filesys = CreateObject("Scripting.FileSystemObject")
Set file1 = filesys.GetFile(wscript.arguments(0))
timediff = int((now - file1.DateCreated) * 60 * 24)

'Send age in minutes back to console:
if timediff >=20 then wscript.echo "ERROR: " & file1.name & " is " & timediff & " minutes old."
End if
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.