Re: Monitor content of a specific file every 2 seconds

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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)

.



Relevant Pages

  • Re: Detecting is hyperthreading is enabled with WMI?
    ... It is too bad that WMI does not give this info. ... have to be done to the script. ... Public Sub DisplayProcessorInfo ... dim ProcessorSet, Processor ...
    (microsoft.public.windowsxp.wmi)
  • Cant make this page work
    ... I can't make this script work properly. ... The script at the bottom of the html page ... Does someone have a perl ... sub output_trace_headers { ...
    (comp.lang.javascript)
  • Re: Maybe I should try a different approach
    ... entire time my script is running, then have it close when my script is done and maybe have some dots keep adding to the text message until the script is fully completed? ... ' The "Three Ugly Hack" Script, ... Sub oATO_vbTimerEvent' timer event handler... ... Public Property Let Left ...
    (microsoft.public.scripting.vbscript)
  • Re: Win32_Product doesnt list all installed Applications
    ... 'Must have ADSI and WMI installed on PC running script. ... CONST ForReading = 1 ... Sub Connect ... strHTML = "Smoke'm if you Got'em" ...
    (microsoft.public.windows.server.scripting)
  • Win32_Product doesnt list all installed Applications
    ... 'Must have ADSI and WMI installed on PC running script. ... CONST ForReading = 1 ... Sub Connect ... strHTML = "Smoke'm if you Got'em" ...
    (microsoft.public.windows.server.scripting)