Create batch file to check empty folder

January 25, 2010 at 08:37:49
Specs: Windows XP
I need to create a batch file to check through a list of folders and see if it is empty. If it is empty, write the folder name to a text file.

I found a command to check if folder is empty.

To check if folder is empty.
dir "C:\*.*" /b /a | find /v "NoTlIkElY" >nul && echo NOT empty || echo empty

However, I do not know how to create a loop to check through all the folders and output to a text file.

Please help.

Thanks


See More: Create batch file to check empty folder

Report •

#1
January 25, 2010 at 14:12:54
i like that script, pretty slick use of errorlevel from "find"!
i never would have thought to use it like that.

make it into a for/r loop:

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


ps: i don't think you have to use the "NoTlIkElY" string, just "" will do, i think
but i left it the way you had it just in case!


Report •

#2
January 25, 2010 at 16:37:56
Hi,

Thanks. I found the script online actually, not done by me.

Can I use dir/b/s/ad |FIND /c ":\" to get the number of folder, and pass the result into the loop?

Kindly guide me on this.

Thanks. :)

And how do I output the empty folder name to a text file?



Report •

#3
January 25, 2010 at 20:06:21
@echo off>FILES.TXT
for /r %%a in (.) do (
dir /b "%%a" | find /v "NoTlIkElY" >nul && echo %%a NOT empty || >>FILES.TXT echo %%a empty
)

this should put the results int FILES.TXT.
I'm not sure what you mean about passing the count (number of subdirs) into the loop...how do you want to use it? it's just one number, and has no effect on the action of the loop...


Report •

Related Solutions

#4
January 26, 2010 at 09:08:37
dir "C:\*.*" /b /a | find /v "NoTlIkElY" >nul && echo NOT empty || echo empty

Is there a headache-oneline-code award on this site ? I would like to nominate one ... ;)


Report •

#5
January 26, 2010 at 09:39:49
Stole some of Mechanix's code ( http://www.computing.net/answers/pr... ) to check on directory emptiness.


This code is tested:

You may need to finetune for special files (hidden, system), but it looks to be working OK:

@echo on

set outfile=C:\emptydirs.log
set where2look=C:\

del %outfile%

for /F "tokens=*" %%a in ('dir %where2look% /B /AD /S') do (
dir "%%a" /b > nul 2> nul
if errorlevel 1 (
echo Directory "%%a" is empty>> %outfile%
)
)


Report •

Ask Question