Link to home
Start Free TrialLog in
Avatar of sunhux
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
Avatar of sunhux
sunhux

ASKER

SOLUTION
Avatar of Peter Kwan
Peter Kwan
Flag of Hong Kong image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sunhux

ASKER

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)
Avatar of 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:

http://scripts.dragon-it.co.uk/links/batch-files-older-than

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

Steve
Avatar of sunhux

ASKER

Q1:
>> 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?

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

Q3:
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

Q4:
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") = _
"smtp"
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Update
objEmail.Send
objItem.StartService()
End If
Next
Avatar of sunhux

ASKER

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
Avatar of sunhux

ASKER

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

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

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

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

If Wscript.Arguments.Count = 0 Then
Wscript.echo "9999 Error - no file specified"
ELSE
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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial