[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2010-01-04
16
Medium Priority
?
464 Views
Last Modified: 2013-11-14
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
0
Comment
Question by:Charlie_Melega
  • 6
  • 5
  • 2
  • +2
16 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 26172635
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
 
LVL 38

Expert Comment

by:Shift-3
ID: 26172764
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
 
LVL 59

Expert Comment

by:Bill Prew
ID: 26173252
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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 

Author Comment

by:Charlie_Melega
ID: 26173543
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
 
LVL 59

Expert Comment

by:Bill Prew
ID: 26173718
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
 

Author Comment

by:Charlie_Melega
ID: 26174069
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
 
LVL 38

Expert Comment

by:Shift-3
ID: 26174559
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
 

Author Comment

by:Charlie_Melega
ID: 26175633
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
 
LVL 38

Expert Comment

by:Shift-3
ID: 26175703
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
 

Author Comment

by:Charlie_Melega
ID: 26175823
ideally;

Filename.extension       date\time created
0
 
LVL 38

Expert Comment

by:Shift-3
ID: 26175951
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
 

Author Comment

by:Charlie_Melega
ID: 26176069
writing to a text file would be ideal.
0
 
LVL 16

Expert Comment

by:t0t0
ID: 26177234
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
 
LVL 38

Accepted Solution

by:
Shift-3 earned 2000 total points
ID: 26181527
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
 
LVL 16

Expert Comment

by:t0t0
ID: 26187210
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
 

Author Comment

by:Charlie_Melega
ID: 26202803
testing shift-3's VBS cript, looks really good.  Thanks for the time and help oyu've put into this.


Charlie
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
This article is about my experience upgrading my consulting machine to Windows 10 Version 1709 (The Fall 2017 Creator Update)
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

830 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