How to? List empty folders

#1
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
236
3
58
The Hague
#2
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,556
46
Albuquerque, NM
prospero.unm.edu
#4
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
236
3
58
The Hague
#8
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
 
#9
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,556
46
Albuquerque, NM
prospero.unm.edu
#10
> 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,556
46
Albuquerque, NM
prospero.unm.edu
#11
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.
 
#14
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
 
#16
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!
 
#20
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:
#22
[...]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: