• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 466
  • Last Modified:

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


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)
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
    End If

Open in new window

  • 4
  • 2
1 Solution
Charlie_MelegaAuthor Commented:
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
Meir RivkinFull stack Software EngineerCommented:
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.
Meir RivkinFull stack Software EngineerCommented:
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Charlie_MelegaAuthor Commented:
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!!
Charlie_MelegaAuthor Commented:
I guess this may not be possible?
Hi, was this question answered here:

or does this one have different requirements?


Charlie_MelegaAuthor Commented:
Hello Rob,

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

Thanks again
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now