[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VBS or batch:  Monitoring a folder for new additions

Posted on 2006-06-08
7
Medium Priority
?
2,099 Views
Last Modified: 2007-12-19
Hi all

I'd like to run a routine every 30 minutes to see if any files or folders have been added within a specific directory in the last 20 minutes.

If not, the routine should call a batch file I already have that sends an e-mail.  If there ARE files younger than 20 minutes, the routine should exit until it's called again.  Obviously if the routine can be done in batch, it can just goto :alert and I'll stick the e-mail routine there.

The folder is on a network, and files will be added within subdirectories so the routine needs to check down through all folders to check for new files.

It would also be handy if the routine could report the date and time of the youngest file it found and set a variable (I can use that in the batch e-mail) - so the process is:

- check \\server\share\{all subdirectories} for files younger than 20 minutes old
- found at least one file younger than 20 minutes?  OK - exit
- youngest file older than 20 minutes?  Get date and time of youngest file and set %oldest%=date+time - then call batch file (or goto :alert)

Could anyone help with that one ?

Thanks !


Simon
0
Comment
Question by:SimonUK
  • 4
  • 3
7 Comments
 
LVL 35

Expert Comment

by:mvidas
ID: 16870368
Hi Simon,

Can you change your batch file to accept the date/time as an argument instead of setting the variable? Setting dos variables via vbscript can be a little difficult, I'm not even sure of a way to do it.  But I did write a vbs for you to use if you can change it, set it up in windows scheduler to run every 30 minutes:

 Dim vNew()
 ReDim vNew(1)
 vNew(1) = 0
 GetNewestFile "\\server\share", vNew
 If vNew(1) > Now - TimeValue("00:20:00") Then
  WScript.Quit
 Else
  WScript.Shell "yourbatch.bat """ & vNew(1) & """"
 End If

Function GetNewestFile(ByVal vPath, ByRef vNew())
 Dim fso, f, fld, fl
 Set fso = CreateObject("scripting.filesystemobject")
 Set fld = fso.GetFolder(vPath)
 For Each f In fld.Files
  If f.DateCreated > vNew(1) Then
   vNew(0) = f.Path
   vNew(1) = f.DateCreated
  End If
 Next
 For Each fl In fld.SubFolders
  GetNewestFile fl.Path, vNew
 Next
 Set fso = Nothing
 Set f = Nothing
 Set fld = Nothing
 Set fl = Nothing
End Function

Matt
0
 
LVL 1

Author Comment

by:SimonUK
ID: 16872270
Hi Matt

Yes, I can accept the date/time as an argument instead of setting the variable - that's actually a neater way of doing it - thanks !

I'll try the script and let you know how it goes...


Simon
0
 
LVL 35

Expert Comment

by:mvidas
ID: 16872359
Though I'm sure you're aware, use %1 in the batch file to refer to the first (only) argument
If you wanted, we could try to integrate the emailing process into the vbs file, so you won't even need to call the batch
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:SimonUK
ID: 16872454
Hi again

Not quite there - I set the 00:20:00 to 00:02:00 to test (because files exist in the path that are younger than 20 minutes - that's how it's supposed to be!).

At first I thought nothing had happened... but a bit later I got two errors (obviously it was busy checking the folders).

Line 5 char 2, Type mismatch: 'TimeValue' code 800A000D

then

Line 8 char 3, Object doesn't support this property or method: 'WScript.Shell" code 800A01B6

One thing I forgot to mention is that the path has a space in it, and an IP address.  I'm working on changing it but it's a configuration issue in the software that generates the files !

The path looks like \\servername\share\folder\xxx.xxx.xxx.xxx\name spaced\DD-MM-YY\filename.ext

Hope that helps.


Simon
0
 
LVL 35

Accepted Solution

by:
mvidas earned 2000 total points
ID: 16872810
The space in the path shouldn't make a difference for the file search, but it will if your .bat file has a space in the name/path.

I wasn't fully thinking with the wscript.shell portion, I'll give you the right code for that in the update here.
As for the type mismatch in TimeValue, you said you changed it to 2 minutes, I'll change the updated code to 2 minutes as well to make sure you typed it in correctly (I often will use ' instead of " on accident, perhaps you did the same).

Change the initial code (not within the function) to:

 Dim vNew(), WShell
 ReDim vNew(1)
 Set WShell = CreateObject("wscript.shell")
 vNew(1) = 0
 GetNewestFile "\\server\share", vNew
 MsgBox vnew(0) & vbCrLf & vnew(1)
 WScript.Quit
 If vNew(1) > Now - TimeValue("00:02:00") Then
  WScript.Quit
 Else
  WShell.Run "yourbatch.bat """ & vNew(1) & """"
 End If

I have it giving you a message box telling you the newest file name and date/time, so you can see what it is returning, then quitting afterwards.  Assuming that is returning correct date (note that it returns the file Creation date rather than Modified date -- if you want that, just change .DateCreated in the function to .DateLastModified (or .DateLastAccessed if that fits your needs)), you can remove the first WScript.Quit there to let it call the batch file.  Let me know how it goes!
Matt
0
 
LVL 1

Author Comment

by:SimonUK
ID: 16872901
Perfect - thank you !


Simon
0
 
LVL 35

Expert Comment

by:mvidas
ID: 16872925
Great! Glad to help. Let me know if you need anything else, or want to try to integrate the email function into this.
Matt
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses
Course of the Month20 days, 3 hours left to enroll

872 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