Charlie_Melega
asked on
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
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
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.
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.
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
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
ASKER
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.
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.
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
~bp
ASKER
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.
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.
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
ASKER
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
Again, many Thanks,
Charlie
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?
When you say "have the script produce the filenames", how do you want it to return the results?
ASKER
ideally;
Filename.extension date\time created
Filename.extension date\time created
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?
ASKER
writing to a text file would be ideal.
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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..
ASKER
testing shift-3's VBS cript, looks really good. Thanks for the time and help oyu've put into this.
Charlie
Charlie
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