How to? List empty folders

Apr 2, 2011
1,216
2
51
North Carolina, USA
I tried

DEL /a:d /e /s /x /n /y * but that said 0 files were deleted.

Any suggestions or should I make a suggestion on the feedback forum?
 
Apr 13, 2010
243
5
58
The Hague
If you want to list empty folders then why are you trying to DELete something. If you want to delete directories, use RD. If you want to list directories have a look at TREE. Of course DIR /A:D /S will also list directories but that gives you no information about any files in them. If you want to do that, you can use the list of directories DIR ... selects for you in a DO loop and test the contents of each directory on your criteria.

Going back to your example, DEL /S /X * will delete all files and remove any empty directories afterwards. Remember that DEL is file oriented so you might say that with the "convenience" option /X the directories are removed as a side-effect. If it is directories you want to remove, without any concern about the files they contain, then RD /S * would be more "appropriate".

Hope this helps,
DJ
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
3,827
48
Albuquerque, NM
prospero.unm.edu
If you want to list empty folders then why are you trying to DELete something.
He's not. DEL /N deletes Nothing. He's using a variant on a common trick to remove empty directories, in the hope that DEL will list the empty directories it would remove. Clever. But DEL does not list directories, only files, so the creativity is wasted.
 
Apr 13, 2010
243
5
58
The Hague
This works.

Code:
do d in /p dir /a:d /s /f /h * ( if %@files[/h %d] == 0 echo %d )
Or multi-line

Code:
do d in /p dir /a:d /s /f /h *
   iff %@files[/h %d] == 0 then
      echo %d
   endiff
enddo
 
Dec 7, 2009
221
2
Left Coast, USA
I had a look at the online help -- specifically the '/s' switch for 'rd' -- and was appropriately terrified. Egad.

Just by the by:

> global /i /q if %@files[/h *] == 0 echo %_cwd

Why is that 'fairly hideous'? (see earlier in thread) It seems to me it's precisely the sort of thing where TCC excels, when you need something done effectively with the least strain.
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
3,827
48
Albuquerque, NM
prospero.unm.edu
> global /i /q if %@files[/h *] == 0 echo %_cwd

Why is that 'fairly hideous'? (see earlier in thread)
Eh, I don't love GLOBAL. It's a crocky little command for adding recursion to externals which don't support it, and it works by changing the current working directory n times; kludgey at best.

DJ's approach is more elegant. But there really ought to be a way to do it without the DIR and implicit pipe....
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
3,827
48
Albuquerque, NM
prospero.unm.edu
Another bodge, this time using FOR:

Code:
for /r %d in ( nul ) if %@files[/h "%d\.."] == 0 echo %@path[%d]
It seems like FOR /R /D would be perfect for this purpose, but it doesn't return the current directory, only its subdirectories.
 
May 20, 2008
9,290
62
Syracuse, NY, USA
4UTILS has:
Code:
@EMPTY[dir] = 1 (empty), 0 (non-empty), -1 (error), -2 (not found)
It simplifies things a tad. But you'll still have to use a TCC mechanism for traversing the tree. I like GLOBAL for this particular task because it doesn't let you in places you don't belong in while suppressing error messages. While it's not elegant, here's my $.02.
Code:
global /i /q if %@empty[.] == 1 echo %_cwd
 
May 20, 2008
9,290
62
Syracuse, NY, USA
I (Charles too) should have used "/H" with GLOBAL.
Code:
global /h /i /q if %@empty[.] == 1 echo %_cwd
With that, I find 707 empty directories on my system drive, 765 if elevated!
 
May 20, 2008
9,290
62
Syracuse, NY, USA
Yes.

I note that "empty directory" can mean different things: no files in the directory itself, no files in the directory or any of its subdirectories, no files or subdirectories (other than the . and .. entries) at all.
If it has subdirectories, I wouldn't even be tempted to call it empty.
 
Aug 3, 2016
368
9
Netherlands
Code:
ATTRIB /D /S * 1>nul 2> empty.txt
Output is something like:
Code:
TCC: (Sys) There are no more files.
 "C:\Temp\TCMD20\plugins\*"
TCC: (Sys) There are no more files.
 "C:\Temp\TEST_TCMD\ff\*"
TCC: (Sys) There are no more files.
 "C:\Temp\TEST_TCMD\ProfileManager\*"
TCC: (Sys) There are no more files.
 "C:\Temp\TEST_TCMD\test1\*"
TCC: (Sys) There are no more files.
 "C:\Temp\WebServer2\LightTPD\logs\*"
So it needs a little post-processing, depending on what you want to do with those directories. That can be done with -for example- findstr /i /v "TCC:" empty.txt or TPIPE /grep .. /replace ... )
But the directory parsing is straightforward (and fast! :-)
 
Last edited:
Apr 2, 2011
1,216
2
51
North Carolina, USA
Code:
for each dir (do not transverse junctions (either soft or hard))
  if it has no subdirs
  if it has 0 files
  list %_CWD
endfor
 
Aug 3, 2016
368
9
Netherlands
[...]So it needs a little post-processing [...]
I want to get myself a little more familiar with sed, awk, grep and the likes and thought this was a good opportunity to dust off SED:
Code:
ssed.exe -e /^TCC.*denied.$/{;N;d} -e /^TCC:.*files.$/d -e s/\\\*// empty.txt
In TCC you have to escape the ^:
Code:
ssed.exe -e /^^TCC.*denied.$/{;N;d} -e /^^TCC:.*files.$/d -e s/\\\*// empty.txt
(looks like a cat walked on my keyboard ;-)

PS: Feel free to improve upon this code (Still have a lot to learn; this is just the 4th time I used SED and those times were so far apart that I basically had to start from zero).

PPS: You could also use this to report the folders you don't have access to: switch the "denied" and "files" text.
 
Last edited: