Solved

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

Posted on 2012-12-27
8
2,319 Views
Last Modified: 2013-01-08
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
0
Comment
Question by:sunhux
  • 5
  • 2
8 Comments
 

Author Comment

by:sunhux
ID: 38723476
The scripts here are up to day but not to the minute / seconds :

http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_25780555.html

http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_23306423.html

Appreciate if someone can give a script that meets my needs fully
0
 
LVL 16

Assisted Solution

by:Peter Kwan
Peter Kwan earned 340 total points
ID: 38723685
Please see if the following works for you.  Please make sure that the destination folder is an empty folder, otherwise it will delete all contents in that folder.

Dim modDate
Dim fso, oFile, sSrcFolder, oSrcFolder, sDestFolder, oDestFolder
Dim oWMIService, oServiceList, oService

Set fso = CreateObject("Scripting.FileSystemObject")
sSrcFolder = "C:\Windows\xxx\Spool\"
Set oSrcFolder = fso.GetFolder(sSrcFolder)
sDestFolder = "D:\Documents\XXX"

If fso.FolderExists(sDestFolder) Then
   fso.DeleteFolder(sDestFolder)
End If

Set oDestFolder = fso.CreateFolder(sDestFolder)

For Each oFile in oSrcFolder.Files
If DateDiff("n", oFile.DateLastModified, Now()) > 2/1440 Then
  fso.MoveFile oFile.Path, sDestFolder & "\"
End If
Next

Set oWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set oServiceList= oWMIService.ExecQuery("Select * from Win32_Service Where Name ='spooler'")

For Each oService In oServiceList
	oService.StopService()
Next

Wscript.Sleep 5000

For Each oService In oServiceList
	oService.StartService()
Next

For Each oFile in oDestFolder.Files
' Move
fso.MoveFile oFile, sSrcFolder & "\"
Next

Set oWMIService = Nothing
Set oServiceList = Nothing
Set oService = Nothing
Set oFile = Nothing
Set oSrcFolder = Nothing
Set oDestFolder = Nothing
Set fso = Nothing

Open in new window

0
 

Author Comment

by:sunhux
ID: 38723813
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)
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 38723814
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
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:sunhux
ID: 38723853
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
0
 

Author Comment

by:sunhux
ID: 38723862
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
0
 

Author Comment

by:sunhux
ID: 38723908
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
0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 140 total points
ID: 38724065
Sorry about that.  It is hosted on my Domino box at an ISP but I have had a few dodgy connections this morning too so looks like a general issue there, my server says it is fine but losing connections to outside world a bit.

Yes you could use >=0.5, what it does is take the difference between now and (in that case) the created date of the file and multiplies it by 60 minutes and 24 hours, the result starts out as fractions of a day, i. 12 hours = 0.5 so... multiply it by 60 * 24 (which is where the 1440 the other guy used came in) and it is the no. of minutes, multiply by 60*60*24 and it is the number of seconds.

Sorry I don't have time to put the bits of scripts together for you at the moment but will pickup when I can if no-one else already has.

If you want the emailing / service changes and file changes you might aswell do it all in VBScript.  It is actually quite logical.  The other option is we do it from batch but have to call VBScript to do some of the bits anyway.

To your questions....

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?


See above.  If you use 50/(1440*60)or 50/86400 then that would be seconds (86400 secs in a day, 1440 mins in a day)

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


Just use the  property DateCreated instead as in my script above.

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


You could use the code from your other VBScript - I have quickly edited it below, though not tested it yet, or use wscript.shell (google should give you example) to run net stop / net start commands.

REM Bits to stop and start spooler service and send email if not started right

Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Service where DisplayName = 'Print Spooler'")

For Each objItem in colItems
   objItem.StopService()
   objItem.StartService()
   If objItem.status <>"OK" or objItem.state <>"Running" then
      Set objEmail = CreateObject("CDO.Message")
      objEmail.From = "Email Address"
      objEmail.To = "Email Address"
      objEmail.Subject = "Print Spooler Service"
      objEmail.Textbody = objItem.DisplayName & " 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

   end if
next

Open in new window


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


Sounds to me like you need to define a bit better what is wanted, depends what you are trying to do but this sort of things sounds logical to me:

Check if spooler stopped, if so make a note but Start spooler and wait 30 -60 seconds to see give spooled files time to start clearing
Check if any files over 5 minutes old.  If so Stop service, move them to another folder and make a note of them, start spooler.

If any files were found over 5 minutes old then Stop & Start spooler, check if has started ok, make a note if not.

If any issues found above then send an email.

Is your printing really causing you so many problems, things are SO much better in most places these days in later OS and newer printer drivers in my experience.

Good luck with it anyway, can't believe how much I just typed, was only passing!

Steve
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

746 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

11 Experts available now in Live!

Get 1:1 Help Now