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.


' 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


Tags