Find & Delete Empty Folders

English support forum

Moderators: white, Hacker, petermad, Stefan2

User avatar
white
Power Member
Power Member
Posts: 4622
Joined: 2003-11-19, 08:16 UTC
Location: Netherlands

Re: Find & Delete Empty Folders

Post by *white »

Gert wrote: 2022-10-28, 09:01 UTC I tried the suggestion by @white:
white wrote: 2014-02-26, 11:22 UTC
and it uses the Command-> "Synchrinize Dirs.." functionality which maybe due to this added sub directory in the program folder now has an "empty directories" checkbox (but I seem to remember it, so not sure whether that is really a new addition).
The "Empty directories" checkbox always exists in the Synchronize dirs window. The purpose of the added sub folder to the program folder is simply to have a folder without any sub folders at a fixed location which can be used to synchronize with.

BTW, nowadays you can autostart the comparing by adding an equal sign (=) to the button command before "DeleteEmptyFolders". Copy/paste this button to TC's button bar:

Code: Select all

TOTALCMD#BAR#DATA
SYNCOPEN =DeleteEmptyFolders

wcmicons.dll
Delete empty folders in right panel


-1
This way you only need to click this button and then click the Synchronize button (or press Enter).
Gert wrote: 2022-10-28, 09:01 UTC Could we not better not offer those base folders at all that contain deep in the tree still some files in some subfolder? It would make me feel much more comfortable pressing the button.
Yes, you are right. The synchronize dirs function is hard to understand but I actually think this might be considered a bug. I think it can be considered a bug of the synchronize dirs function for 2 reasons:
  1. When you set the file type filter to *.* and click Compare and manually uncheck the found files, the checkboxes of the parent folder an higher up folders are deselected automatically.
  2. When you use the file type filter |* as the above mentioned button does, the parent folder of the excluded files are removed from the result list, but all folders higher up are not. So basically it is half implemented one way and half another way.
Gert
Junior Member
Junior Member
Posts: 5
Joined: 2022-10-11, 09:17 UTC

Re: Find & Delete Empty Folders

Post by *Gert »

Thanks @Fla$her, but I use the "Synchronize Dirs.." quite a lot and it does its job. It correctly also deletes the empty folders in one go, no multiple invocations required as demonstrated for example in the below picture, before and after delete using "Synchronize Dirs.." against the special empty folder in the total commander (the folder created as described by @white that appears when pressing the new button).
Image: https://abload.de/img/emptyfolderdeletione8dps.png
Gert
Junior Member
Junior Member
Posts: 5
Joined: 2022-10-11, 09:17 UTC

Re: Find & Delete Empty Folders

Post by *Gert »

white wrote: 2022-10-28, 11:56 UTC ....
Yes, you are right. The synchronize dirs function is hard to understand but I actually think this might be considered a bug. I think it can be considered a bug of the synchronize dirs function for 2 reasons:
  1. When you set the file type filter to *.* and click Compare and manually uncheck the found files, the checkboxes of the parent folder an higher up folders are deselected automatically.
  2. When you use the file type filter |* as the above mentioned button does, the parent folder of the excluded files are removed from the result list, but all folders higher up are not. So basically it is half implemented one way and half another way.
Thanks
white wrote:
again for the detailed info. Most important is that it does it job correctly (as shown by the picture I added to the previous comment Image: https://abload.de/img/emptyfolderdeletione8dps.png) and no plugins are required. I will try to add the '=' symbol to the button as well.
Fla$her
Power Member
Power Member
Posts: 2309
Joined: 2020-01-18, 04:03 UTC

Re: Find & Delete Empty Folders

Post by *Fla$her »

Gert wrote: 2022-10-28, 15:35 UTCThanks @Fla$her, but I use the "Synchronize Dirs.." quite a lot and it does its job.
That's not an explanation. I also use SynchDirs often. But for the banal removal of empty directories, it is easier to click on the button and get the result without unnecessary calls to dialogs, which are essentially intended for other purposes.

Code: Select all

TOTALCMD#BAR#DATA
%ComSpec% /q/c cd/d
"%P"&>nul robocopy "%P:~,-1" "%P:~,-1" /s /move /ndl /nfl /njh /njs
wcmicon2.dll,76
Delete empty subfolders

1
Last edited by Fla$her on 2022-10-28, 21:16 UTC, edited 1 time in total.
Overquoting is evil! 👎
User avatar
Horst.Epp
Power Member
Power Member
Posts: 6492
Joined: 2003-02-06, 17:36 UTC
Location: Germany

Re: Find & Delete Empty Folders

Post by *Horst.Epp »

Fla$her wrote: 2022-10-28, 20:58 UTC

Code: Select all

TOTALCMD#BAR#DATA
%ComSpec% /q/c cd/d
"%P"&>nul robocopy "%P:~0,-1" "%P:~0,-1" /s /move /ndl /nfl /njh /njs
wcmicon2.dll,76
Delete empty subfolders

1
Thats a nice and fast solution which is now on my button bar :D
Windows 11 Home x64 Version 23H2 (OS Build 22631.3527)
TC 11.03 x64 / x86
Everything 1.5.0.1373a (x64), Everything Toolbar 1.3.3, Listary Pro 6.3.0.73
QAP 11.6.3.2 x64
Fla$her
Power Member
Power Member
Posts: 2309
Joined: 2020-01-18, 04:03 UTC

Re: Find & Delete Empty Folders

Post by *Fla$her »

You can even do with one line:

Code: Select all

TOTALCMD#BAR#DATA
%ComSpec% /q/c >nul robocopy . . /s /move /ndl /nfl /njh /njs

wcmicon2.dll,76
Delete empty subfolders

1
Overquoting is evil! 👎
User avatar
beb
Senior Member
Senior Member
Posts: 435
Joined: 2009-09-20, 08:03 UTC
Location: Odesa, Ukraine

Re: Find & Delete Empty Folders

Post by *beb »

I like to use "robocopy . . /s /move" method to delete empty folders via cmd.
Alas, whenever I've been trying to put it into a user em_ command it doesn't work.
Somehow it doesn't accept %P (or %P:~0,-1 to remove "\" ending) or %T (or %X%T) parameters.
Do you guys have any experience or ideas on how to wrap "robocopy . . /s /move" into a working user em_ command?

Edit: It's just a miracle. As soon, as I typed the question here the solution came :)
The key is found in the Fla$her's and white's messages.
Especially one noting that the path of the active panel is the default start folder.

Working user command:

Code: Select all

[em_deletemptydirs]
cmd=robocopy . . /s /move
or, regarding the timing factor and the robocopy log-limiting syntax (the impact of which can be assessed here: viewtopic.php?p=423479#p423479), even better as follows:

Code: Select all

[em_deletemptydirs]
cmd=robocopy . . /s /move  /NFL /NDL /NJH /NJS
or, respecting the "%ComSpec% /q/c >nul" part in front according to Fla$her

Code: Select all

[em_deletemptydirs]
cmd=%ComSpec% /q/c>nul robocopy . . /s /move /NFL /NDL /NJH /NJS
User command aim: to delete any empty (sub)directories within the current directory in the active panel

Such a command can be placed by a user either in a button bar or in a menu.

PS Feel free to use the below cmd to create empty folders structure for testing purposes:

Code: Select all

md 1\1\1 1\1\2 1\1\3
md 1\2\1 1\2\2 1\2\3
md 1\3\1 1\3\2 1\3\3
md 2\1\1 2\1\2 2\1\3
md 2\2\1 2\2\2 2\2\3
md 2\3\1 2\3\2 2\3\3
md 3\1\1 3\1\2 3\1\3
md 3\2\1 3\2\2 3\2\3
md 3\3\1 3\3\2 3\3\3
md 4&echo. 2>4\txt.txt
Then, in this example, em_deletemptydirs will delete everything ("1", "2", "3" folders and their subfolders, which are empty) except the "4" directory which is not empty (containing "txt.txt" file)

NB robocopy syntax: https://ss64.com/nt/robocopy.html

Edit: simplified code regarding user notes in the below posts
initial code

Code: Select all

[em_deletemptydirs]
cmd=%ComSpec% /q/c cd/d "%P"&robocopy . . /s /move
Last edited by beb on 2022-10-31, 12:23 UTC, edited 4 times in total.
#278521 User License
Total Commander [always the latest version, including betas] x86/x64 on Win10 x64/Android 10
Fla$her
Power Member
Power Member
Posts: 2309
Joined: 2020-01-18, 04:03 UTC

Re: Find & Delete Empty Folders

Post by *Fla$her »

beb wrote: 2022-10-30, 07:05 UTC The key is the '%ComSpec% /q/c cd/d "%P"&' part that I took from Fla$her's message.
In the last button I don't have 'cd/d "%P"&'. It doesn't make sense in the 'Command:' field.
And given the speed, I also don't see the point in outputting data to the console.
Overquoting is evil! 👎
User avatar
white
Power Member
Power Member
Posts: 4622
Joined: 2003-11-19, 08:16 UTC
Location: Netherlands

Re: Find & Delete Empty Folders

Post by *white »

beb wrote: 2022-10-30, 07:05 UTC

Code: Select all

[em_deletemptydirs]
cmd=%ComSpec% /q/c cd/d "%P"&robocopy . . /s /move
Special variables like %P work only in Parameters field, not in Command field. So the command cd/d "%P" doesn't do anything other then give an error.

It still works because it can be left out. The path of the active panel is the default start folder.

Be aware that when running %ComSpec% as Administrator while TC is not running as Administrator, the command prompt will start in the folder c:\windows\system32.
Last edited by white on 2022-10-30, 11:27 UTC, edited 1 time in total.
User avatar
Horst.Epp
Power Member
Power Member
Posts: 6492
Joined: 2003-02-06, 17:36 UTC
Location: Germany

Re: Find & Delete Empty Folders

Post by *Horst.Epp »

Thats my version which runs in any mode

Code: Select all

[em_DeleteEmptyDirs]
cmd=%ComSpec%
param=/q/c cd/d "%P"&robocopy . . /s /move
Windows 11 Home x64 Version 23H2 (OS Build 22631.3527)
TC 11.03 x64 / x86
Everything 1.5.0.1373a (x64), Everything Toolbar 1.3.3, Listary Pro 6.3.0.73
QAP 11.6.3.2 x64
Fla$her
Power Member
Power Member
Posts: 2309
Joined: 2020-01-18, 04:03 UTC

Re: Find & Delete Empty Folders

Post by *Fla$her »

2Horst.Epp
I write that it is not necessary. Take my version, it's faster since it doesn't waste resources on logging.
Overquoting is evil! 👎
User avatar
beb
Senior Member
Senior Member
Posts: 435
Joined: 2009-09-20, 08:03 UTC
Location: Odesa, Ukraine

Re: Find & Delete Empty Folders

Post by *beb »

white wrote: 2022-10-30, 09:29 UTC ... The path of the active panel is the default start folder...
Oh, that's an important note, I didn't know this was a thing.
So the user command can be simplified even as follows:

Code: Select all

[em_deletemptydirs]
cmd=robocopy . . /s /move
Last edited by beb on 2022-10-30, 12:53 UTC, edited 1 time in total.
#278521 User License
Total Commander [always the latest version, including betas] x86/x64 on Win10 x64/Android 10
User avatar
beb
Senior Member
Senior Member
Posts: 435
Joined: 2009-09-20, 08:03 UTC
Location: Odesa, Ukraine

Re: Find & Delete Empty Folders

Post by *beb »

2Fla$her
As to the robocopy syntax:

Code: Select all

1.  /NP : No Progress - don’t display % copied.
2.  /NS : No Size - don’t log file sizes.
3.  /NC : No Class - don’t log file classes.
4. /NFL : No File List - don’t log file names.
5. /NDL : No Directory List - don’t log directory names.
6. /NJH : No Job Header, ROBOCOPY © header.
7. /NJS : No Job Summary: Stats for Copied, Skipped, Mismatch, Failed, Extras.[/list]
You are using only the 4-7 to choke the logging/output, do the 1-3 become useless?

Edit: Trying to answer my own question:
just have run a real-life test (in a random directory with 2694 subdirectories and about 22 GB of data)
the results are pretty consistent and as follows:

Code: Select all

full robocopy output
2022-10-30 15:58:11.05
2022-10-30 15:58:21.12
delta: 21.12-11.05=10.07s

full no log (robocopy /NP /NS /NC /NFL /NDL /NJH /NJS) 1st pass
2022-10-30 15:58:21.12
2022-10-30 15:58:22.29
delta: 22.29-21.12=1.17s

short no log (robocopy /NFL /NDL /NJH /NJS)
2022-10-30 15:58:22.30
2022-10-30 15:58:23.45
delta: 23.45-22.30=1.15s

full no log (robocopy /NP /NS /NC /NFL /NDL /NJH /NJS) 2nd pass
2022-10-30 15:58:23.45
2022-10-30 15:58:24.58
delta: 24.58-23.45=1.13s
Conclusions:
1. running robocopy with no limited output is not the best idea if time matters,
it takes much longer (about up to 10 times longer in the specific example):
where time deltas: 10.07s vs 1.17s, 1.15s, 1.13s
2. running robocopy with a full (1-7) or short (4-7) set of the log-limiting parameters
doesn't make big difference (beyond statistical/rounding error):
where time deltas: 1.17s, 1.13s (params 1-7) vs 1.15s (params 4-7)
#278521 User License
Total Commander [always the latest version, including betas] x86/x64 on Win10 x64/Android 10
Fla$her
Power Member
Power Member
Posts: 2309
Joined: 2020-01-18, 04:03 UTC

Re: Find & Delete Empty Folders

Post by *Fla$her »

beb wrote: 2022-10-30, 12:41 UTC So the user command can be simplified even as follows:

Code: Select all

[em_deletemptydirs]
cmd=robocopy . . /s /move
No. Exactly as I wrote it. I repeat:
Fla$her wrote: 2022-05-27, 01:38 UTCSo he will delete current folder itself if it turns out to be empty.
Therefore, %ComSpec% /q/c >nul is the necessary part in front.
And for the log suppression, as you have already been able to test (and I did it many years ago), it is enough to use the 4 specified switches.
Overquoting is evil! 👎
User avatar
beb
Senior Member
Senior Member
Posts: 435
Joined: 2009-09-20, 08:03 UTC
Location: Odesa, Ukraine

Re: Find & Delete Empty Folders

Post by *beb »

It turns out the [robocopy '"dir1" "dir1" /s /move] command is resulting in dropping off all the attributes of the source folders (R - Read only; S - System; H - Hidden) if any, except A (Archive).
So, alas, this method negatively impacts a user who has special folder attributes within the source directory - all those attributes would be lost.
And there seems no simple workaround.
It's, to say the least, very disappointing.

Edit

It would work with the extra step (an intermediate directory like "dir2") to preserve the source folders attributes:

Code: Select all

robocopy  "dir1" "dir2" /s /move /COPY:DAT /DCOPY:EDAT&robocopy "dir2" "dir1" /s /move /COPY:DAT /DCOPY:EDAT
However, the main idea and the charm of the command where "the source and the destination are the same" gets lost.
Last edited by beb on 2022-11-01, 18:54 UTC, edited 1 time in total.
#278521 User License
Total Commander [always the latest version, including betas] x86/x64 on Win10 x64/Android 10
Post Reply