Re: monitor for file creation using variables
- From: Jeffery Hicks <jhicks@xxxxxxxxxx>
- Date: Fri, 08 Jun 2007 07:58:16 -0400
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."
.
- Prev by Date: Re: Move client printers to different print server
- Next by Date: Re: Intermittent LDAP 'Table not found' error
- Previous by thread: Re: Move client printers to different print server
- Next by thread: Clearing DNS Suffix search order
- Index(es):
Relevant Pages
|