How to check a folder if file count has increased and identify file(s) by filename that increased the count

Hello,

I am looking for the best method to check a folder if file count has increased and identify the file(s) by filename that increased the file count in that folder\directory.
What is the best source to tap into for this?  I am looking at Performance Counter or WMI but would greatly appreciate any detail from the experts out there as to the best course of action.


Best Regards,


Charlie
Charlie_MelegaAsked:
Who is Participating?
 
Shift-3Commented:
Ok, this revision will write a tab-delimited list of new files and their creation dates to the file configured in the strReport variable.


Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
 
strFolder = "c:\files"
strList = "filelist.txt"
strReport = "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
 
knightEknightCommented:
Well, depending on what you need, you may be able to design a solution using the FC.EXE utility.

First, create a list of all files in the directory like this:

  dir /b  c:\mypath\*.*  > c:\filelist_1.txt

Then later create the list again:

  dir /b  c:\mypath\*.*  > c:\filelist_2.txt

Then you can compare the two files to see if they are different:

  fc.exe  c:\filelist_1.txt  c:\filelist_2.txt
 
0
 
Shift-3Commented:
You can continuously monitor using WMI with a script like the one from this article.  Unfortunately that method is resource-intensive, and monitoring too many evens may slow down your system.

If this is a concern, and if you don't need to check that often, you could write a script to compare directory listings.  This could be done in batch using knightEknight's example, or with vbscript using a dictionary object.  I can give an example of the latter method if you want.
0
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.

 
Bill PrewCommented:
What do you want to happen when a change is detected?  

What frequency do you need to check, does some action need to occur within 1 second of a change? Within 1 minute?  Within 1 hour?  etc.

Have you considered a third party utility, or is that out of the question?

~bp
0
 
Charlie_MelegaAuthor Commented:
Thanks to all for the great feedback.
I am looking to poll for any changes to a specific directory once every hour. If there is a change, I will be generating an email that notifies of this change as well as the file(s) added. I will be doing this with a 3rd party monitoring tool but exceed the metric source which I believe knightEknight may have provided.  (I am testing now) I don't want to alert if the file count has decreased since last poll , only increased so I may need to edit this a bit or look for a way to ensure only.
0
 
Bill PrewCommented:
Can files ever be deleted?  Like what if 2 files are deleted, and 2 new ones added?  That would mean the same count as last check, does that mean you don't want to know about that?

~bp
0
 
Charlie_MelegaAuthor Commented:
Definitely a valid point. I would want to know only about those files that have been added. If a scenario occurs such as what you describe, that would present a problem as FC check would not generate any output even though a file(s) has been added.
0
 
Shift-3Commented:
Paste the script below into a text file with a .vbs extension.  Customize the value of the strFolder variable on line 4 with the location of the folder to check.  Customize the email variables on lines 7-12 with the appropriate values.

Running the script will check the folder for any filenames which were not present when the script was last run.  If there are any, it will email a list of them to the specified address.  This could be set up to run periodically with a scheduled task.


Const ForReading = 1
Const ForWriting = 2
 
strFolder = "c:\files"
strList = "filelist.txt"
 
strEmailFrom = "do.not.reply@example.com"
strEmailTo = "user@example.com"
strEmailSubject = "files added to " & strFolder
strEmailBody = "The following files were added to the folder " & _
    strFolder & ":" & vbCrLf & vbCrLf
strSMTP = "smtpserver.example.com"
 
Set objOldFiles = CreateObject("Scripting.Dictionary")
objOldFiles.CompareMode = VbTextCompare
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
If objFSO.FileExists(strList) Then
    Set objList = objFSO.OpenTextFile(strList, ForReading)
    
    Do Until objList.AtEndOfStream
        objOldFiles.Add objList.ReadLine, ""
    Loop
    
    objList.Close
End If
 
Set objList = objFSO.OpenTextFile(strList, ForWriting, True)
Set objFolder = objFSO.GetFolder(strFolder)
 
intCount = 0
 
For Each objFile In objFolder.Files
    strName = objFile.Name
    objList.WriteLine strName
    
    If Not objOldFiles.Exists(strName) Then
        ReDim Preserve arrNewFiles(intCount)
        arrNewFiles(intCount) = strName
        intCount = intCount + 1
    End If
Next
 
If intCount > 0 Then
    strEmailBody = strEmailBody & Join(arrNewFiles, vbCrLf)
    
    Set objEmail = CreateObject("CDO.Message")
    objEmail.From = strEmailFrom
    objEmail.To = strEmailTo
    objEmail.Subject = strEmailSubject
    objEmail.Textbody = strEmailBody
    objEmail.Configuration.Fields.Item _
        ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    objEmail.Configuration.Fields.Item _
        ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strSMTP
    objEmail.Configuration.Fields.Item _
        ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
    objEmail.Configuration.Fields.Update
    objEmail.Send
End If

Open in new window

0
 
Charlie_MelegaAuthor Commented:
Shift-3 , this is excellent, Thank You. I am testing now. I have edited the script to remove the email notification portion as I want to integrate the email action with the 3rs party tool I am using. (It has a good number of variables I can add into the email body). I seem to be running into an issue. Just for validation, what lines should be removed for the email task portion of the script to be deleted? My goal is to have the script produce the filenames of any added files to the folder path since the last time the script was run.

Again, many Thanks,
Charlie
0
 
Shift-3Commented:
Remove or comment out lines 7-12 and 48-60 to get rid of the email functionality.

When you say "have the script produce the filenames", how do you want it to return the results?
0
 
Charlie_MelegaAuthor Commented:
ideally;

Filename.extension       date\time created
0
 
Shift-3Commented:
Ok, but do you want it to echo the results back to the console or write them to a text file or call an executable with the filenames as arguments, or what?
0
 
Charlie_MelegaAuthor Commented:
writing to a text file would be ideal.
0
 
t0t0Commented:
Interesting....

By the way, has anyone considered a file might change and not just be created?

FOR's %%~t variable only shows the file's creation date and time.

DIR's /tc does the same however, DIR also has a /tw which can be used to compare if a file has been written to (or changed) since the last filescan.

The 'A' attribute could also indicate a filechange....but it is not soley reliable.

What if a file is simply 'replaced' by a file with the same filename? Naturally, replaced files will show up in the 'created' category...

Obviously, any solution based on FOR....('DIR /B...')... will not suffice.
0
 
t0t0Commented:
I think it would be nice to see this one cracked using a batch file solution....afterall, this is very much a DOS related issue..
0
 
Charlie_MelegaAuthor Commented:
testing shift-3's VBS cript, looks really good.  Thanks for the time and help oyu've put into this.


Charlie
0
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.

All Courses

From novice to tech pro — start learning today.