Question Locked

Check if directory is empty

0 helpful
2 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hi,

I wanted to write a batch file where when ran it will check if a certain directory is empty and if it isnt, it will copy all the files within that directory to another location. I have to check if the folder is empty because the batch file also creates a folder named as today's date to move the files to. I dont want to create a date folder if there is nothing to copy. I found the following code online and it works perfectly but as i am only starting out writing batch i have no idea what it means. I just dont see how it does what it does by looking at it. the code is as follows:

for /f %%a in ('dir /b /a-d "%dirtocheck%"') do set empty=false & goto movestuff

Where 'dirtocheck' is the location of the directory i want to check for the presence of files. Can somebody explain it to me? Thanks a lot.

2 replies

by rob miners

You are using the set command to create an environment EG: set dirtocheck=C:\data\files

When you set dirtocheck it becomes a variable that can be used as %dirtocheck%

Basically it tells the "for" command where to look for the folder to check.

for /f %%a in ('dir /b /a-d "%dirtocheck%"') do set empty=false & goto movestuff

For more information on the SET command open a cmd.exe prompt and type in set /?

This discussion is closed to new replies.