Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Other Programming > VBScript
|
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
  #1 (permalink)  
Old August 5th, 2009, 05:14 PM
Registered User
 
Join Date: Aug 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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
Reply With Quote
  #2 (permalink)  
Old August 6th, 2009, 02:47 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

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

'
Reply With Quote
  #3 (permalink)  
Old August 6th, 2009, 03:05 PM
Registered User
 
Join Date: Aug 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
Reply With Quote
  #4 (permalink)  
Old August 6th, 2009, 05:17 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

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

'
'
Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
View files which were recently modified marcin2k Access VBA 25 February 4th, 2005 10:46 PM
Search Files mistry_bhavin General .NET 1 May 5th, 2004 09:23 AM
How to search whole disk for .doc files? bcmaverik VB.NET 2002/2003 Basics 2 February 18th, 2004 09:25 AM
Search files containing text salman Classic ASP Databases 1 October 22nd, 2003 05:43 AM
Search files by filename! wenzation Classic ASP Basics 3 August 31st, 2003 08:28 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.