Contoso.se

Welcome to contoso.se! My name is Anders Bengtsson and this is my blog about Microsoft infrastructure and system management. I am a principal engineer in the FastTrack for Azure team, part of Azure CXP, at Microsoft. Contoso.se has two main purposes, first as a platform to share information with the community and the second as a notebook for myself.

Everything you read here is my own personal opinion and any code is provided "AS-IS" with no warranties.

Anders Bengtsson

MVP
MVP awarded 2007,2008,2009,2010

My Books
Service Manager Unleashed
Service Manager Unleashed
Orchestrator Unleashed
Orchestrator 2012 Unleashed
OMS
Inside the Microsoft Operations Management Suite

Contoso.se

Welcome to contoso.se! My name is Anders Bengtsson and this is my blog about Azure infrastructure and system management. I am a senior engineer in the FastTrack for Azure team, part of Azure Engineering, at Microsoft.  Contoso.se has two main purposes, first as a platform to share information with the community and the second as a notebook for myself.

Everything you read here is my own personal opinion and any code is provided "AS-IS" with no warranties.



MVP awarded 2007,2008,2009,2010

My Books

Service Manager Unleashed


Orchestrator 2012 Unleashed


Inside the Microsoft Operations Management Suite

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 


2 Comments

  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 comment

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.