Page 1 of 1
fast way to find empty folders?
Posted: 12 Mar 2017 14:25
by catalinnc
i have a "target" folder with lots and lots of folders/subfolders and files...
some of the folders/subfolders are empty...
i need a very fast way to find this empty folders and generate an "empty_folder.txt" file (zero bytes) inside them...
for now i use this:
Code: Select all
for /f "delims=" %%D in ('dir /a:d /b /s target') do (
dir /b "%%D" | > nul find /v "" || > "%%D\empty_folder.txt" type nul
)
can this be done faster than this?
_
Re: fast way to find empty folders?
Posted: 13 Mar 2017 00:04
by fugitive
How about this one,The code is more complex, but not used "find".
@echo off
for /f "delims=" %%a in ('dir /ad /b /s target') do call :check "%%a"
pause&exit /b
:check
set "ept="
for /f "delims=" %%b in ('dir /a /b "%~1"') do set ept=no
if not defined ept cd.>"%~1\empty_folder.txt"
goto :eof
Re: fast way to find empty folders?
Posted: 13 Mar 2017 05:07
by fugitive
Or,try this code
Code: Select all
for /f "delims=" %%a in ('dir /ad /b /s') do rd "%%~a"&& (md "%%~a" &cd.>%%~a\empty_folder.txt )
My friend told me : Alway try to avoid using “find\findstr”.
Re: fast way to find empty folders?
Posted: 13 Mar 2017 06:02
by aGerman
My friend told me : Alway try to avoid using “find\findstr”.
He's right in case of multiple callings of any external utilities (such as find.exe or findstr.exe).
Your idea to use a helper variable is quite good even if I would avoid calling a label because this slows down the execution again. Another improvement could be using FOR /R /D as outer loop in order to get rid of the buffering of the FOR /F loop.
Steffen
EDIT:
That's what I was talking about ...
Code: Select all
@echo off &setlocal
for /r /d %%i in (*) do (
set "e=1"
for /f %%j in ('dir /b "%%i"') do if defined e set "e="
if defined e >"%%i\empty_folder.txt" type nul
)
Re: fast way to find empty folders?
Posted: 14 Mar 2017 12:40
by catalinnc
@fugitive thanks a lot for your "rd" suggestion...
@aGerman thanks a for your ideea but on win xp (sp2) the /r option do not work...
_
Re: fast way to find empty folders?
Posted: 15 Mar 2017 03:28
by penpen
catalinnc wrote:on win xp (sp2) the /r option do not work...
I couldn't believe this, so i've tested it:
Yes, it does work under win xp sp2.
penpen
Re: fast way to find empty folders?
Posted: 15 Mar 2017 12:54
by catalinnc
ooops...(*) works...i was using (target\*)...
_