Need to count files in a directory or just determine if a directory is empty

I need to count files in a directory or... basiclly I just want to know if a directory is empty.  Not having much luck with Google searches.  Thanks.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

The command you want to use is dir.  The dir command allows you to see the available files in a directory.

You may first have to use chdir to navigate to the folder you want to check.  Then use dir.  If it is empty you will see no files.  Here is a link to some commands and how to use them.


DIR /A /S /AD>output.txt

Open in new window

If the output.txt is empty that means you have no files in directory
Ernie BeekExpertCommented:
Something like:

for /r %%a in (.) do (dir /b "%%a" | find /v "" >nul && echo >nul || echo %%a empty)

This checks all the directories from the current path down to see if they are empty.
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Steve4400Author Commented:
Thanks AllTrip,
My situation is a little more complex.  I an on a LAN and use dir for processing and logging.  If I switch directories with dir I loose my log file.
Bill PrewIT / Software Engineering ConsultantCommented:
Here's a fairly simple way to do it.  Not sure what you want to do if it is empty, but place that in the braces.  You may also want to make sure the directory exists first.

if not exist "c:\temp\" md "c:\temp\"
dir /b /a-d "c:\temp\">NUL 2>&1 || (
  ECHO directory is empty
  REM insert processing you want to perform when empty here

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bill PrewIT / Software Engineering ConsultantCommented:
The use of the DIR command should not hurt anything in your script, are you perhaps thinking of the CD command, which would switch you to that folder?  The DIR command doesn't do that.

billprew is correct.  the dir command will not switch you over to any folder.  The chdir command does that.  The dir command simply allows you to see the available files in a directory.
Steve4400Author Commented:
Thanks all - I don't know where I came up with the issue of dir but you folks are correct and I got it working!!  billprew - that scriptlet worked fine.  Thanks again.
Bill PrewIT / Software Engineering ConsultantCommented:
Very welcome, glad that was useful.

Steve4400Author Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.