Can I be notified when a directory contains more than a configurable number of files?

Article ID: 206
Category: Scripts
Applies to: All Versions
Updated: 2019-07-29

You can launch the following VBScript through the application scheduler (e.g. every 1 minute) to be notified when the number of files in a given folder exceeds a configurable limit.

Note: The file needs to be saved with the .vbs extension and called it through cscript.exe.

You set the directory in the variable: DIRECTORY, and the number of files in the variable MAX_FILES.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
' Counts the number of files in a folder (without traversing subfolders), useful
' if you need to ensure that the number of files in a folder doesn't exceed a
' certain limit
' 
' Returns 1 if the number of files is larger than MyLimit or 0 if the number
' of files is equal or less than MyLimit. Check return code through %ERRORLEVEL%.

Option Explicit

' Set your values here
const DIRECTORY = "C:\Batch"
const MAX_FILES = 200

Dim FS, FO, FC
Set FS = CreateObject("Scripting.FileSystemObject")
Set FO = FS.GetFolder(DIRECTORY) 
Set FC = FO.Files

WScript.Echo "Directory " & DIRECTORY & " contains " & FC.Count & " files."

' Set %ERRORLEVEL%
If FC.Count > MAX_FILES Then
       WScript.Quit(1)
Else
       WScript.Quit(0)
End If


Try EventSentry on-premise

FREE 30-day evaluation

Download Now