View previous topic :: View next topic |
Author |
Message |
jsimotas
Joined: 02 Jul 2005 Posts: 5
|
Posted: Thu Nov 22, 2007 11:22 am Post subject: Age of newest file in folder |
|
|
I copied (and added to) code from another user on this forum but now i can't find or give credit to the other post!
Anyways, I use this vbscript to check the age (in minutes) of the newest file in a folder. In my case a process I monitor is supposed to export a daily file so I use:
testname "c:\myfolder" 1440
The only weird thing is that the optional file filter has to be in regular expression format like:
testname "c:\myfolder" 1440 ".txt$" (for files matching *.txt)
NewestFile.vbs:
Code: |
Option Explicit
Call main
Sub main
On Error Resume Next
Dim FSO, fsFolder, objArgs, item, NewestFile, regEx, strPath
Dim intAgeMinsMax, intAgeMins, strPattern
Set objArgs = WScript.Arguments
If (objArgs.count<2) Or (objArgs.count>3) Then
wscript.stdout.write "scriptres:BAD:Invalid number of parameters 2 or 3 (you supplied "& objArgs.count & ") (e.g. c:\temp 30 .txt$)" & vbCrLf
wscript.stdout.write " ---------------------------------------------------"& vbCrLf
wscript.stdout.write " newestfile.vbs ""path"" MaxAgeMins ""[expression]"" "& vbCrLf
wscript.stdout.write " ---------------------------------------------------"& vbCrLf
wscript.stdout.write " path = path to search for files (for UNC of LFN paths, use "") " & vbCrLf
wscript.stdout.write " maxagemins = 1440 (1 day)" & vbCrLf
wscript.stdout.write " expression [optional]= Regular expression to define a filter " & vbCrLf
wscript.stdout.write " eg: all files that end with LOG = LOG$ " & vbCrLf
wscript.stdout.write " all files that start with TAPE = ^TAPE " & vbCrLf
wscript.quit
end If
'------------------- params
strPath=objArgs(0)
intAgeMinsMax=objArgs(1)
If objArgs.count=3 Then
strPattern=objArgs(2)
Else
strPattern=""
End If
'------------------- age is number?
If Not IsNumeric(intAgeMinsMax) Then
wscript.stdout.write "scriptres:BAD:MaxAgeMins=" & intAgeMinsMax & " (this is not a valid number)"
wscript.quit
End If
'------------------- new object
Set FSO = CreateObject("Scripting.FileSystemObject")
If Err<>0 Then
wscript.stdout.write "scriptres:BAD:Unable to open Filesystem Object"
wscript.quit
End If
'------------------- folder listing
If Right(strPath,1)<>"\" Then strPath=strPath & "\"
Set fsFolder = fso.GetFolder(strPath)
If Err<>0 Then
wscript.stdout.write "scriptres:BAD:Unable to open path '" & strPath & "'"
wscript.quit
End If
'------------------- search through results
Set regEx = New RegExp
Set NewestFile=Nothing
For each item in fsFolder.Files
regEx.Pattern = strPattern
regEx.IgnoreCase = True
If regEx.Test(Item.Name) Then
If NewestFile is Nothing Then Set NewestFile=Item
If Item.DateLastModified>NewestFile.DateLastModified then Set NewestFile=Item
End If
Next
'------------------- results
If NewestFile is Nothing then
wscript.stdout.write "scriptres:BAD:No file found"
Else
intAgeMins = DateDiff("n", NewestFile.DateLastModified, Date+Time)
If CLng(intAgeMins) >= CLng(intAgeMinsMax) Then
wscript.stdout.write "scriptres:BAD:" & intAgeMins & " (" & NewestFile.Name & ")"
Else
wscript.stdout.write "scriptres:OK:"& intAgeMins & " (" & NewestFile.Name & ")"
End If
End If
'---------------- debug
'wscript.stdout.write "----------------------------------------" & vbCrLf
'wscript.stdout.write "Path=" & strPath & vbCrLf
'wscript.stdout.write "AgeMins=" & intAgeMins & vbCrLf
'wscript.stdout.write "Pattern=" & strPattern & vbCrLf
'wscript.stdout.write "----------------------------------------" & vbCrLf
End Sub
|
|
|
Back to top |
|
![](templates/subSilver/images/spacer.gif) |
KS-Soft Europe
Joined: 16 May 2006 Posts: 2832
|
Posted: Fri Nov 23, 2007 4:23 am Post subject: |
|
|
Great job!
Thank you for your contribution!
Regards,
Max |
|
Back to top |
|
![](templates/subSilver/images/spacer.gif) |
Joe-Vis
Joined: 24 Nov 2002 Posts: 12
|
Posted: Mon Mar 10, 2008 10:47 pm Post subject: How do I use this |
|
|
How do I use this |
|
Back to top |
|
![](templates/subSilver/images/spacer.gif) |
Joe-Vis
Joined: 24 Nov 2002 Posts: 12
|
Posted: Mon Mar 10, 2008 11:15 pm Post subject: Figured it out... |
|
|
use as Shell Script....
If there is a different way please let me know.
Thanks
Joe |
|
Back to top |
|
![](templates/subSilver/images/spacer.gif) |
KS-Soft Europe
Joined: 16 May 2006 Posts: 2832
|
Posted: Tue Mar 11, 2008 2:35 am Post subject: Re: Figured it out... |
|
|
Joe-Vis wrote: | use as Shell Script.... | Correct.
Joe-Vis wrote: | If there is a different way please let me know. | This script is designed for "Shell Script" test method. Why don't you like "Shell script" test method? You just should create a new script in "Script Manager", copy foregoing script into "Script" area and select "cmd /c cscript /B /E:VBScript %Script% %Params%" from "Start cmd" dropdown.
http://www.ks-soft.net/hostmon.eng/mframe.htm#tests.htm#shellmng
Regards,
Max |
|
Back to top |
|
![](templates/subSilver/images/spacer.gif) |
Joe-Vis
Joined: 24 Nov 2002 Posts: 12
|
Posted: Tue Mar 11, 2008 12:35 pm Post subject: No problem with Shell Script method |
|
|
Just was not sure if it was the right answer. It works great. |
|
Back to top |
|
![](templates/subSilver/images/spacer.gif) |
|