Re: monitor for file creation using variables



JSGB wrote:
Greetings, I am fairly new to scripting so pardon me if this posed incorrectly. I want to monitor a folder for a file to be modified which sounds easy eough and i found the script below to do that task. However, the file that i am monitoring will have a different name based on the current date i.e. CAS_%YEAR%%MONTH%%DAY%.log the file for today would be CAS_20070607.log. Is it possible to monitor for a file based on date variables as such? Thanks very much for taking the time to read this post any help that you can provide would be greatly appreciated!

Best regards,
JSGB


strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\cimv2")

Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceModificationEvent WITHIN 10 WHERE " _
& "TargetInstance ISA 'CIM_DataFile' and " _
& "TargetInstance.Name='c:\\scripts\\index.vbs'")

Do
Set objLatestEvent = colMonitoredEvents.NextEvent
Wscript.Echo "File: " & objLatestEvent.TargetInstance.Name
Wscript.Echo "New size: " & objLatestEvent.TargetInstance.FileSize
Wscript.Echo "Old size: " & objLatestEvent.PreviousInstance.FileSize
Loop



It depends on how you are running this script and how long it takes between the time the monitoring starts and the time a file is modified. If it is the same day you could create your query defining a file name using the VBScript date time functions to return year, month etc.

dtYear=CStr(Year(Now))
dtMonth=CStr(Month(Now))
dtDay=CStr(Day(Now))
myfile="CAS_" & dtYear & Pad(dtMonth,2,"0",True) & Pad(dtDay,2,"0",True) & ".log"
WScript.Echo myFile

Function Pad(strText, nLen, strChar, bFront)
Dim nStartLen
If strChar = "" Then
strChar = "0"
End If
nStartLen = Len(strText)
If Len(strText) >= nLen Then
Pad = strText
Else
If bFront Then
Pad = String(nLen - Len(strText), strChar) _
& strText
Else
Pad = strText & String(nLen - Len(strText), _
strChar)
End If
End If
End Function

Your other option is to use the LIKE operator and use a wild card:

"TargetInstance.name LIKE 'c:\\path\\CAS_2007%'"

Although I've never used a wild card with this type of query so I don't know if it will work.

--
Jeffery Hicks
SAPIEN Technologies - Scripting, Simplified.

blog: http://blog.SAPIEN.com
Community: http://www.scriptinganswers.com
Training: http://www.ScriptingTraining.com
Books: http://www.SAPIENPress.com
Editor: http://www.primalscript.com
Tools: http://www.scriptingoutpost.com

"Those who forget to script it are doomed to repeat it."

.



Relevant Pages

  • Re: Slightly [OT] Network Monitoring/Alerting tools
    ... Of course if your server is prone to running out of resources you should also be collecting that data via snmp with thresholds to notify you before you have an outage or at least graphing it so you'll understand the trends. ... We were able to use the monitor to look for ... Most of the common things you would want are already built in so you don't have to script them - or install scripts on all the clients. ...
    (Fedora)
  • Re: trouble with Javascript Pop-up
    ... I was talking about some script to center your page on the monitor, ... the text link itself, the pop-up window generated ... You might play with the size of the fragment box...and you aren't using a ...
    (microsoft.public.publisher.webdesign)
  • Windows Script to monitor file size on remote servers
    ... I have recently written a script to monitor the file size on ... tricky part was to determine %systemroot% on remote systems (not ... or FQDNs of the servers you wish to connect to. ...
    (microsoft.public.scripting.vbscript)
  • Re: trouble with Javascript Pop-up
    ... have been that I had my monitor set to 120 dpi, ... Here's the script for the pop-up: ... There is a centering code, but in the script, ... You might play with the size of the fragment box...and you aren't using a ...
    (microsoft.public.publisher.webdesign)
  • Re: Command or setting to force monitor on?
    ... Keystrokes vbs script? ... But the same symptoms as without cscript, monitor not waking up, when I ... I set up a batch file containing the commands to first start IE, ... could move to the keystrokes command. ...
    (microsoft.public.windowsxp.general)