Batch file that monitors the number of files in a directory
Sometimes the fastest and easiest way to complete a task is a good old MS-DOS batch (.bat) file. A friend recently needed to get a quick fix in place for their production machine.
“I need to whip up a batch file that monitors the number of files in a directory. Basically, I want to see if there are 100 files in a directory and then start and stop a service to do some quick cleanup.”
The For command can be used to loop through the files in a folder and get a count and then do something based on that number.
@ECHO OFF
SETLOCAL
SETLOCAL ENABLEDELAYEDEXPANSIONSET count=0
for %%o IN (D:\temp\*.*) DO (
echo %%o
SET /A count=count + 1
)echo %count%
IF "%count%"=="100" ECHO NET STOP MyApp
ENDLOCAL ENABLEDELAYEDEXPANSION
ENDLOCAL