Werbung: SecurityConsole.de verwaltet Ihre Computer mit Security Essentails aus der Cloud!
30 Tage kostenfrei testen und 20% Rabatt für Ihre Bestellung mit Promocode: WBF2685582
(Promocode gültig bis 31.12.2011)

Group:  English: Windows Server » microsoft.public.windows.server.scripting
Thread: monitor for file creation using variables

HTVi
TV Discussion Newsgroups

monitor for file creation using variables
JSGB 6/7/2007 12:14:02 PM
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


Re: monitor for file creation using variables
Jeffery Hicks <jhicks[ at ]sapien.com> 6/8/2007 11:58:16 AM
JSGB wrote:
[Quoted Text]
> 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."

Home | Search | Terms | Imprint Contact
Newsgroups Reader - provided by WiredBox.Net
Suche nach Orten, Städten, Postleitzahlen, Vorwahlen, Kfz-Kennzeichen