 |
| VBScript For questions and discussions related to VBScript. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the VBScript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

August 5th, 2009, 05:14 PM
|
|
Registered User
|
|
Join Date: Aug 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Search for files modified within the last five minutes
I'm completely new to programming and scripting. I need a script that searches a folder for a file named *_single.xml and a file named *_full.xml. It must find at least one of each that has been modified within the last five minutes. If either one of these files does not exist with a last modified date of within five minutes, the script will fire a windows event. The script I have right now checks a specific file to see if it has been checked within five minutes, but now I need the script to search the folder for those two files and check the date modified on all results. Here's my current script. Thanks in advance for any help!
--------------------------------------------------------------------------
Option Explicit
'* DEFINE CONSTANTS
Const EVENT_ERROR = 1
Const EVENT_INFORMATIONAL = 4
'* DECLARE VARIABLES
Dim objShell, objExecObject, strText
Dim FSO,File
Dim Date1,Date2,Hour1,Hour2
'* Set environment and open output file
Set objShell = WScript.CreateObject("WScript.Shell")
set FSO=CreateObject("Scripting.FileSystemObject")
Set File=FSO.GetFile("c:\test\test.txt")
Date1=Now()
Date2=File.DateLastModified
'wscript.echo DateDiff("s",Date2,Date1),"Seconds"
If DateDiff("s",Date2,Date1) > 300 then
objShell.LogEvent EVENT_ERROR, "file has not been written to within the last 5 minutes"
End if
|

August 6th, 2009, 02:47 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
Do you mean the *exact* names of the files are as you gave them or only that the file names will *end* like that? That is, will the file be "_full.xml" or could it be "whatever_full.xml"??
If the file names are exact, it's a trivial change to what you have there:
Code:
Option Explicit
'* DEFINE CONSTANTS
Const EVENT_ERROR = 1
Const EVENT_INFORMATIONAL = 4
'* DECLARE VARIABLES
Dim objShell
Dim FSO, File1, File2
'* Set environment and open output file
' why do you create this until you need it?
Set objShell = WScript.CreateObject("WScript.Shell")
set FSO = CreateObject("Scripting.FileSystemObject")
Dim match : match = False
If FSO.FileExists("c:\test\_single.xml") AND FSO.FileExists("c:\test\_full.xml") Then
' the two files at least exist...when were the modified?
Set File2 = FSO.GetFile("c:\test\_single.xml")
Set File2 = FSO.GetFile("c:\test\_full.xml")
If DateDiff("s",File1.DateLastModified,Now()) <= 300 _
AND DateDiff("s",File2.DateLastModified,Now()) <= 300 _
Then
match = True
End If
End If
If Not match Then
objShell.LogEvent EVENT_ERROR, "One or both files have not been written to within the last 5 minutes"
End if
'
|

August 6th, 2009, 03:05 PM
|
|
Registered User
|
|
Join Date: Aug 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks for the help! Unfortunately it would be "whatever_full.xml." That's what was meant by the asterisk. There are multiple files ending with _full.xml or _single.xml, all I need to do is make sure that at least one of each has been modified within the last 5 minutes.
|

August 6th, 2009, 05:17 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
I was really afraid that was what you meant, but had to ask.
This is 100% UNTESTED, but if it's wrong it should be wrong in the details, not in the concepts.
Code:
Option Explicit
'* DEFINE CONSTANTS
Const EVENT_ERROR = 1
Const EVENT_INFORMATIONAL = 4
Const FULL = "_full.xml"
Const SINGLE = "_single.xml"
'* DECLARE VARIABLES
Dim objShell
Dim FSO, fldr, File
Dim fulllen, singlelen
fulllen = Len(FULL)
singlen = Len(SINGLE)
'* Set environment and open output file
' why do you create this until you need it?
Set objShell = WScript.CreateObject("WScript.Shell")
Set FSO = CreateObject("Scripting.FileSystemObject")
Set fldr = FSO.GetFolder("c:\test")
Dim matchfull, matchsingle
matchfull = False
matchsingle = False
For Each file In fldr.Files
If Not matchfull Then
If Right(LCase(file.Name),fullen) = FULL Then
matchfull = ( DateDiff("s",File.DateLastModified,Now()) <= 300 )
End If
End If
If Not matchsingle Then
If Right(LCase(file.Name),singlelen) = SINGLE Then
matchsingle = ( DateDiff("s",File.DateLastModified,Now()) <= 300 )
End If
End If
If matchfull AND matchsingle Then Exit For ' no point in continuing
Next
If ( Not matchfull ) AND ( Not matchsingle ) Then
objShell.LogEvent EVENT_ERROR, "One or both files have not been written to within the last 5 minutes"
End if
'
'
|
|
 |