Attention: We have retired the ASP.NET Community Blogs. Learn more >

Contents tagged with MS-DOS

  • 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 ENABLEDELAYEDEXPANSION

    SET 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

    Technorati Tags: ,