Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

How to? List empty folders

Apr
1,825
17
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?
 
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
 
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?

Well, this is fairly hideous:

Code:
global /i /q if %@files[/h *] == 0 echo %_cwd
 
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.
 
well - one can not remove a folder if there is a file in a subfolder.....
 
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
 
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.
 
> 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....
 
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.
 
This might be a good idea for a plugin to make possible?
 
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
 
P.S. I find 97 of them on my system drive. Elevated, I find 127.
 
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!
 
I (Charles too) should have used "/H" with GLOBAL.

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.
 
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.
 
As the OP - I wanted to see the list of folders which have no files in that or any subfolder.
 
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:
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
 
[...]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:
Back
Top
[FOX] Ultimate Translator
Translate