We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now


DOS Script - Check if a folder is empty

Medium Priority
Last Modified: 2013-11-10
During my daily processing, I need to move files from a source(incoming) folder to a target(processing) folder. Once the file is in my target folder, I kick off my application which processes this file. After my application completes processing, another script moves the file out of the processing folder.
Before I move my files from the incoming to the processing folder, I want to ensure that my processing folder is empty. I saw some solutions to the problem using the FOR command, but that command doesn't work at this client. How do I ensure that my target foler is empty before moving any new files into it?
Watch Question

Lee W, MVPTechnology and Business Process Advisor
Most Valuable Expert 2013

What do you mean the FOR command doesn't work at that client?  The For Command is built in to Windows Server.  How does it now work?  (are you trying to run it in command.com instead of cmd.exe?)
I did run cmd.exe
The error I used to get is:
'for' is not recognized as an internal or external command, operable program or batch file

Same for find. After reading your comment, when I tried to run for and find again, it says syntax not correct. So these commands do seem to be working now. I am at a client who are extremely restrictive, and I guess something might have changed since the last time I tried this out.

If possible, I would still like to see a solution to this problem without using for/find simply because I am not sure if my automation user will have access to these commands, and I don't want it to fail the first time it runs.

One more pointer: My target folder, if not empty, will contain files starting with DET*. I tried using:

if exist %Tgt%\DET*.* (
write to log file
exit 1

This seems to be working. I was looking for a more elegant solution, where I don't need to use the DET*.* option.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Lee W, MVPTechnology and Business Process Advisor
Most Valuable Expert 2013

That kind of error suggests the server's path statement is messed up.  Can you post it?


I will post the PATH as soon as possible. I have access to the server only 2 hours a day, and my 2 hours for today are now up. (This is not even production yet).

Again, a solution without fancy commands and reasonably flexible would be enough.


Here is the path variable:

Path=C:\Program Files\Support Tools\;C:\Program Files\HP\NCU;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\IBM\DB2RuntimeClient 7\bin\;C:\Program Files\IBM\DB2RuntimeClient 7\function;C:\Program Files\IBM\DB2RuntimeClient 7\help\;C:\Program Files\BMC Software\Control-M Agent\Default\EXE\;C:\Program Files\GnuWin32\bin;C:\Program Files\Microsoft SQL Server\90\Tools\binn\
Lee W, MVPTechnology and Business Process Advisor
Most Valuable Expert 2013

ok, that looks good... can you take a screen shot of the FOR command failing?  where it displays the command executed and the error message?


the for isn't failing now, it used to be failing in the past. I tried again today, and it worked.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.