fast way to find empty folders?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
catalinnc
Posts: 39
Joined: 12 Jan 2015 11:56

fast way to find empty folders?

#1 Post by catalinnc » 12 Mar 2017 14:25

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?
_

fugitive
Posts: 19
Joined: 09 Mar 2017 02:26

Re: fast way to find empty folders?

#2 Post by fugitive » 13 Mar 2017 00:04

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

fugitive
Posts: 19
Joined: 09 Mar 2017 02:26

Re: fast way to find empty folders?

#3 Post by fugitive » 13 Mar 2017 05:07

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”.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: fast way to find empty folders?

#4 Post by aGerman » 13 Mar 2017 06:02

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
)

catalinnc
Posts: 39
Joined: 12 Jan 2015 11:56

Re: fast way to find empty folders?

#5 Post by catalinnc » 14 Mar 2017 12:40

@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...
_

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: fast way to find empty folders?

#6 Post by penpen » 15 Mar 2017 03:28

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

catalinnc
Posts: 39
Joined: 12 Jan 2015 11:56

Re: fast way to find empty folders?

#7 Post by catalinnc » 15 Mar 2017 12:54

ooops...(*) works...i was using (target\*)...
_

Post Reply