Count Files

Here is a script that will count file in a directory. If there is more than X file in the directory a event will be generated in the local event viewer. You can create a event rule that runs the script and a collection rule that collections event ID 2 from your machine. The local event will have event ID 2, you can also add criteria to check if description includes “There is more than”.

In the following example the script will monitor if C:\ have more than 2 files.

Set fs = CreateObject(“Scripting.FileSystemObject”)
folderName = “c:\”
numbers = fs.GetFolder(folderName).Files.Count
if (numbers > 2) Then
Const EVENT_WARNING = 2
Set objShell = CreateObject(“Wscript.Shell”)
objShell.LogEvent 2, “There are more than 2 files in the directory. The directory is ” & folderName & “. Number of files are ” & numbers  End If 

About

Microsoft

2 thoughts on “Count Files

  1. Hi Anders,
    The problem was that the quotes ” were different on my keyboard. The script works fine when I changed them
    Thx again
    JB

  2. Hi Anders,
    Thx for this.
    When I run the script (VB script I assume??) the following error occurs:

    Line 1
    Char 23
    Error Invalid character
    Code 800A0408

    Can u help pls?
    Thx,
    John Bradshaw

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.