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

Hi,
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.
Steve4400Asked:
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.

AllTripCommented:
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.

http://www.computerhope.com/msdos.htm


0
NrisimhaCommented:

DIR /A /S /AD>output.txt

Open in new window


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

@ECHO OFF
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.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

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.
0
Bill PrewCommented:
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

~bp
0

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 PrewCommented:
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.

~bp
0
AllTripCommented:
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.
0
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.
0
Bill PrewCommented:
Very welcome, glad that was useful.

~bp
0
Steve4400Author Commented:
thanks
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.