Find & Delete Empty Folders

English support forum

Moderators: white, Hacker, petermad, Stefan2

Fla$her
Power Member
Power Member
Posts: 2244
Joined: 2020-01-18, 04:03 UTC

Re: Find & Delete Empty Folders

Post by *Fla$her »

2beb
Yes, there is. Well, use my script then.
Overquoting is evil! 👎
User avatar
beb
Senior Member
Senior Member
Posts: 430
Joined: 2009-09-20, 08:03 UTC
Location: Odesa, Ukraine

Re: Find & Delete Empty Folders

Post by *beb »

beb wrote: 2022-11-01, 12:56 UTC 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)
After a couple of days of experiments I may say that the said issue regarding special folders' possible attributes drop-off can be partially addressed as follows:

TotalCommander user command:

Code: Select all

[em_delemptydirs]
cmd=cmd /v /q/c>nul robocopy . . /s /move /NFL /NDL /NJH /NJS&for /f %i in ('dir /b/a/s . ^| find /i "desktop.ini"') do set d=%~dpi&attrib +r !d:~0,-1!
Explanation:
  • cmd /v /q/c>nul part now has a /v switch that enables delayed environment variable expansion
  • robocopy . . /s /move /NFL /NDL /NJH /NJS part does its intended job to delete all the empty dirs within the current dir in the active panel
  • for /f %i in ('dir /b/a/s . ^| find /i "desktop.ini"') do set d=%~dpi&attrib +r !d:~0,-1! part:
    1. finds any folder containing desktop.ini file [dir /b/a/s . ^| find /i "desktop.ini"]
    2. detects the full path(s) of the said folder(s) [d=%~dpi]
    3. eliminates terminal backslash from the said path(s) [!d:~0,-1!] to properly feed it to the attrib command
      note: this very part needs the "delayed environment variable expansion" to be turned on
    4. sets the read-only attribute for the said folder(s) [attrib +r !d:~0,-1!]
      note: it is assumed a directory that contains a "desktop.ini" file is rather a special folder,
      so it is pretty reasonable to set (restore) the read-only attribute for it.
Thus, at least the read-only attribute for the special folders containing desktop.ini files can be effectively restored.
So, users would keep enjoying nice icons of such folders, or whatever.
For most users, it could be pretty enough regarding using the method in question.
#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: 430
Joined: 2009-09-20, 08:03 UTC
Location: Odesa, Ukraine

Re: Find & Delete Empty Folders

Post by *beb »

Fully PowerShell-powered user command to recursively delete any empty (sub)directories within the current directory in the active panel:

button:

Code: Select all

TOTALCMD#BAR#DATA
em_delEmptyDirs

wciconex.dll,85
DeleteEmptyDirs

0
10030
usercmd.ini:

Code: Select all

[em_delEmptyDirs]
cmd=Powershell -ExecutionPolicy Bypass
param=ls (gl) -for -rec|Where {$_.PSIsContainer -and @(ls -lit $_.Fullname -for -rec|Where {!$_.PSIsContainer}).Length -eq 0}|del -for -rec
usercmd.ini (the same as above in full syntax without any aliases and contractions, thus the longest version):

Code: Select all

[em_delEmptyDirs]
cmd=Powershell -ExecutionPolicy Bypass
param=Get-ChildItem (Get-Location) -force -recurse|Where {$_.PSIsContainer -and @(Get-ChildItem -literalPath $_.Fullname -force -recurse|Where {!$_.PSIsContainer}).Length -eq 0}|Remove-Item -force -recurse
NB As seen, the above-mentioned robocopy-based command has a critical downside since it removes all the attributes of the folders (R - Read only; S - System; H - Hidden) if any, except A (Archive).
The PowerShell-powered command doesn't have such a flaw.
#278521 User License
Total Commander [always the latest version, including betas] x86/x64 on Win10 x64/Android 10
Post Reply