Thursday, January 11, 2018

VBS Special! Monitor Network Files

Here's a special one for you!  If you are ever at work and need to monitor a particular file for changes, I have a script for you!

I created this small script to monitor a log file for which I am an administrator. This log file was an indicator if a user was beta testing a piece of software that I wrote. I needed to know when the file was being used, so I knew when to anticipate bug reports and whether or not I would receive feedback.

The method of using this is easy. Simply copy and paste this code into notepad, and save the file with the .vbs extension.

The file will first ask you to choose a file you wish to monitor.  Once you chose the file, you can choose how many seconds you want to monitor it. It is not limited to a certain amount of time, you can enter enough seconds to cover hours, or even days. The script will silently run in the background, and as soon as your chosen file is modifies, you will get a messagebox popup!

---

 dim iSize  
 'create objects  
 set objFSO = createobject("Scripting.FileSystemObject")  
 set objFile = objFSO.GetFile(strFile)  
 Set wShell=CreateObject("WScript.Shell")  
 'Allow a user to select a file with the file dialog box  
 Set oExec=wShell.Exec("mshta.exe ""about:""")  
 'get the file into a string variable  
 strFile = oExec.StdOut.ReadLine  
 'ask the user for input  
 iLength = inputBox("Please Enter the number of seconds you wish to Monitor this file")  
 'validate input, set default if invalid  
 if not isnumeric(iLength) then  
  msgbox "Error! Invalid Time Amount, using default, 1800 seconds (half an hour)"  
  iLength = 1800  
 end if  
 'set original file size  
 iSize = clng(objfile.size)  
 'show the current size  
 msgbox "Current Size " & iSize  
 'loop for designated number of seconds  
 for i = 1 to clng(iLength)  
  'Check if the size has changed (once per second)  
  if clng(objfile.size) <> isize then  
   'notify user of the change  
   msgbox objfile.Name & " Modified!"  
   'end  
   wscript.quit  
  end if  
  'wait a second (or so)  
  wscript.sleep 999  
 next   
 'If no changes detected and timer has run out, notify user  
 msgbox "End of Checking Changes... File Size: " & clng(objFile.Size) & "Kb"  

No comments:

Post a Comment

VBA Add an animated Notification Box to your Excel Program

For those of us who create programs and add-ins in Excel, we are very, very familiar with the message box.  The message box gives us the opp...