Ensure that folder is empty

February 9, 2009 at 15:51:53
Specs: Windows 2003 SP2
During my daily process, I need to move files from one folder to another and then kick off a data load. Before starting to move files to my target, I want to ensure that my target folder is empty. If it isn't, then I want to exit.
When I initially googled this, I got some help using the FOR command. However, that doesn't work (no idea why) on the Windows server at my client. I am looking for an alternative to using FOR.

See More: Ensure that folder is empty

Report •

February 9, 2009 at 16:34:02
In what way does it "not work"? Do you get an error message? Are there any files (which ones?) in the folder that the FOR command doesn't find and it thinks it's empty?

Try this alternative:

if exist x:\folder\* (
echo Folder not empty.
exit /b 1
) else (
xcopy s:\source\* x:\folder

Report •

February 9, 2009 at 21:54:58
When I say it doesn't work, the message it used to give is:

'for' is not recognized as an internal or external command, batch script etc.etc

When I tried 'for' today, it worked. But I will try your solution. I tried

if exist %target%\DET*.* (
echo something
exit 1
this worked.


Report •

Related Solutions

Ask Question