[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


DOS - Check if Folder is Empty

Posted on 2007-07-31
Medium Priority
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

Steve Knight earned 500 total points
ID: 19601970
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

SteveGTR earned 500 total points
ID: 19602669
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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Loops Section Overview
Suggested Courses
Course of the Month19 days, 22 hours left to enroll

872 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