Todd Vargo
2/6/2009 11:06:00 PM
Leon wrote:
> I just want to check if the file have been modified an if so read the
> content. Thats bassically it.
> Can be done as you mentioned.
>
> I also want to name the proces so I can se in it windows task manager
> under processes.
> Not only wscript.exe - but something like "monitoring_folder" - just
> so I can see it and so that a another vbs script (from standard
> scheduler every minut) can monitor that this is running.
I don't know how to change the name of the process but I do know how to set
it to what I want. For me in Winodws 98, I just make a copy of wscript.exe
and rename it to whatever I want it to display. For example, you might copy
wscript.exe to monitoring_folder.exe. I have not tried this in XP to see if
it works for XP too.
The following script may do what you want. I included a mechanism to prevent
accidentally starting another instance. To run it, create a shortcut with,
monitoring_folder.exe FileMonitor.vbs
as the target or command line. Each time the flag file changes, a popup will
notify you (in lieu of an email). The popup also serves as a mechanism to
abort the script during your testing. You can remove it if you want the
script to run in an endless loop.
' FileMonitor.vbs
' This script includes a mechanism that prevents
' running multiple instances of itself.
Set WshShell = WScript.CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
' ==== Initialize single instance mechanism ====
tmp = fso.GetSpecialFolder(2) 'Get TEMP folder
tmp = tmp & ".\" & WScript.ScriptName & ".running"
On Error Resume Next
fso.DeleteFile(tmp) 'Remove any orphaned zero byte file
On Error Goto 0
If fso.FileExists(tmp) Then
WshShell.Popup WScript.ScriptFullName & _
" is already running!",2,"Oops...",48
WScript.Quit
Else
'Create a zero byte file in TEMP.
Set zbf = fso.OpenTextFile(tmp, 2, True)
End If
' ==== Single instance test complete ====
MonitorFile "flag.txt" ' <=== The file to monitor goes here
' ==== Clean up zero byte file ====
zbf.Close
fso.DeleteFile(tmp)
Wscript.Quit
Sub MonitorFile(flagfile)
If Not fso.FileExists(flagfile) Then
MsgBox flagfile & " does not exist.",vbCritical, _
WScript.ScriptFullName
Exit Sub
End If
Set f = fso.GetFile(flagfile)
DateLastModified = f.DateLastModified
Do
checkit = f.DateLastModified
If DateLastModified <> checkit Then
BtnCode = WshShell.Popup("Do you want to abort?", _
2, "FILE CHANGED", 36)
If BtnCode = vbYes Then Exit Do
DateLastModified = checkit
' Put code to send email (or whatever) here.
Else
Wscript.Sleep 2000
End If
Loop
End Sub
--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)