Solved

VBScript that can detect when file(s) to a DIR have been deleted, modified or added.

Posted on 2010-11-09
7
443 Views
Last Modified: 2012-05-10
Hello,

I have been using a vbScript that would create a file which would include the filenames of  any files added to the Directory specified in the vbScript from there last time the vbScript was run . This has been working great but I am now in need of this file (value specified in strReport ) to include the filenames of those files that have:

a) been added to the directory
b) been deleted from the directory
c) been modified in that directory (want to track users that open files and change values in said file and save)

I have the vbScript below. Any thoughts or suggestions would be greatly appreciated.

Thank You

Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
 
strFolder = "c:\folder_to_monitor"
strList = "all_current_files.txt"
strReport = "new_files_added_report.txt"
 
Set objOldFiles = CreateObject("Scripting.Dictionary")
objOldFiles.CompareMode = VbTextCompare
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
If objFSO.FileExists(strReport) Then
    objFSO.DeleteFile strReport, True
End If
 
If objFSO.FileExists(strList) Then
    Set objList = objFSO.OpenTextFile(strList, ForReading)
    
    Do Until objList.AtEndOfStream
        arrLine = Split(objList.ReadLine, vbTab)
        objOldFiles.Add arrLine(0), arrLine(1)
    Loop
    
    objList.Close
End If
 
Set objList = objFSO.OpenTextFile(strList, ForWriting, True)
Set objFolder = objFSO.GetFolder(strFolder)
 
For Each objFile In objFolder.Files
    strName = objFile.Name
    dtmCreated = objFile.DateCreated
    objList.WriteLine strName & vbTab & dtmCreated
    
    If Not objOldFiles.Exists(strName) Then
        Set objReport = objFSO.OpenTextFile(strReport, ForAppending, True)
        objReport.WriteLine strName & vbTab & dtmCreated
        objReport.Close
    End If
Next

Open in new window

0
Comment
Question by:Charlie_Melega
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 

Author Comment

by:Charlie_Melega
ID: 34098299
To further clarify on this, the primary objective of this vbScript should be to determine if: 1) File has been opened 2) File contents have been changed, line added / removed 3) File has been saved

Thanks for suggestions
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 34101337
the script you have posted indeed log the file that were added to the directory but not since the last run of the script but from the first run of the script.
which means that if no new files were added to the directory, all_current_files.txt still logs them.
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 34101415
tell me of the following meet your requirement:
each time you run the script the log file will be appended with a section which states:


•      time of running the script
•      added files
•      modified  files + DateLastModified
•      accessed files (but not modified) + DateLastAccessed
•      deleted files
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:Charlie_Melega
ID: 34102506
Hello Sedgewick,

Yes, the requirements that you state above are exactly what is needed. However, a NEW log file is created  if and only if one of these conditions has occurred since the last time the script was run.  I do  not want to append an existing file as this will not allow me to identify unique activity since the last run of the script. I have another 3rd party tool that I will be using so the output of the log file will only include the activity you referenced above, in the log file. If no activity has occurred from the last time the script was run, then no log file would be produced or simply an empty log file.

Thanks for your help!!
0
 

Author Comment

by:Charlie_Melega
ID: 34112825
I guess this may not be possible?
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 34150203
Hi, was this question answered here:
http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_26609153.html

or does this one have different requirements?

Regards,

Rob.
0
 

Author Comment

by:Charlie_Melega
ID: 34241652
Hello Rob,

Yes, you answered the question very nicely in that other post.

Thanks again
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question