DOS - Check if Folder is Empty

Posted on 2007-07-31
Last Modified: 2011-09-20
I'm trying to create a DOS batch file that will check to see if a particular folder is empty.  Any suggestions?
Question by:rtedi222
    LVL 43

    Accepted Solution

    Try something like this.  If there any files in the directory you set in dirtocheck varibale then the for loop will run once and set the variable empty to false then carry on to the label :next to save running through the loop for every file in that dir.


    @echo off
    set empty=true
    set dirtocheck=c:\temp\test
    for /f %%a in ('dir /b /a-d "%dirtocheck%"') do set empty=false & goto next
    echo Directory %dirtocheck% empty? : %empty%
    LVL 30

    Assisted Solution

    Here's another method. You can pass the directory as a parameter to the batch processing. If not parameter is specified, then the current directory is checked.

    @echo off


    set fileEmpty=N
    set dirCheck=%~1

    if "%1"=="" set dirCheck=.

    for /f %%a in ('dir "%dirCheck%" 2^>NUL ^| findstr /c:" 0 File(s)"') do set fileEmpty=Y

    if "%fileEmpty%"=="Y" echo %dirCheck% is empty

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
    Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now