Delete empty folders (one-click function)

Here you can propose new features, make suggestions etc.

Moderators: white, Hacker, petermad, Stefan2

User avatar
Vochomurka
Power Member
Power Member
Posts: 814
Joined: 2005-09-14, 22:19 UTC
Location: Russia
Contact:

Re: Delete empty folders (one-click function)

Post by *Vochomurka »

Anselm wrote: 2022-05-26, 12:44 UTC I tried this (only at windows command line), so i can give some additional information:
  • development of xxcopy was discontinued
  • is is not free for commercial use
  • it is free for personal use
  • the installation copies files to \windows\system32
    1. Copied C:\WINDOWS\system32\XXCOPYSU.EXE
    2. Copied C:\WINDOWS\system32\XXPBAR64.EXE
    3. Copied C:\WINDOWS\system32\XXCONSOLE.EXE
    4. Copied C:\WINDOWS\system32\UIXXCOPY.BAT
    5. Copied C:\WINDOWS\system32\XXCOPY.CHM
It does not work with long path names, example:

Code: Select all

mkdir d:\1\2\a123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567
move d:\1\2 d:\1\b123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567
1. Yes, development was discontinued, and I've never stated the opposite. Nevertheless the program works;
2. True; so what?
3. I am a person, you seem to be one, too;
4. These are just default paths; they can be changed at the installation. Run Install.bat and read instructions;
5. It does; read the documentation about the /VL key. Max path length is 32767 characters
Single user license #329241
PowerPro scripts for Total Commander
User avatar
Horst.Epp
Power Member
Power Member
Posts: 6450
Joined: 2003-02-06, 17:36 UTC
Location: Germany

Re: Delete empty folders (one-click function)

Post by *Horst.Epp »

Vochomurka wrote: 2022-05-21, 16:45 UTC I use Xxcopy tool for this purpose. I created batch file with one line

Code: Select all

xxcopy.exe %1 /rmdir /x*.* /yy
It's exactly one-click button :D
If you change xxcopy.exe to xxcopysu.exe you can avoid the UAC prompt.
Windows 11 Home x64 Version 23H2 (OS Build 22631.3374)
TC 11.03 x64 / x86
Everything 1.5.0.1371a (x64), Everything Toolbar 1.3.2, Listary Pro 6.3.0.69
QAP 11.6.3.2 x64
Fla$her
Power Member
Power Member
Posts: 2244
Joined: 2020-01-18, 04:03 UTC

Re: Delete empty folders (one-click function)

Post by *Fla$her »

Anselm wrote: 2022-05-26, 12:50 UTCremoving empty folders (example):

Code: Select all

robocopy d:\1 d:\1 /S /MOVE /NDL
So he will delete folder 1 itself if it turns out to be empty. Need like this:

Code: Select all

%ComSpec% /q/c cd/d d:\1&robocopy d:\1 d:\1 /s /move /ndl /nfl /njh /njs
Overquoting is evil! 👎
User avatar
beb
Senior Member
Senior Member
Posts: 430
Joined: 2009-09-20, 08:03 UTC
Location: Odesa, Ukraine

Re: Delete empty folders (one-click function)

Post by *beb »

Working user command aimed to delete any empty (sub)directories within the current directory in the active panel:

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
Such a command can be placed by a user either in a button bar or in a menu.

From here: viewtopic.php?p=423466#p423466
#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: Delete empty folders (one-click function)

Post by *beb »

Fully PowerShell-powered one-click 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 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). 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
KozakMak
Senior Member
Senior Member
Posts: 352
Joined: 2021-05-24, 12:39 UTC
Location: UA

Re: Delete empty folders (one-click function)

Post by *KozakMak »

let's complicate the task :twisted:
delete any empty (sub)directories within the current directory in the active panel to recycle bin

any ideas?
OS: Win10 | TC: latest x64
User avatar
beb
Senior Member
Senior Member
Posts: 430
Joined: 2009-09-20, 08:03 UTC
Location: Odesa, Ukraine

Re: Delete empty folders (one-click function)

Post by *beb »

KozakMak wrote: 2023-09-20, 06:41 UTC delete any empty (sub)directories within the current directory in the active panel...
No problem, I do it this way [without any 3rd party tools, and robocopy, which I don't recommend]:
usercmd.ini
[TotalCommander .ini only, no standalone .ps1 PowerShell script needed]

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
As far as I know, this is the best and the only effectively legit way to do it properly in PowerShell, in one pass and without any side-effects described here and there.
NB Unaliased code for better reading and understanding:
[deleting empty directories within the current directory from a standalone .ps1 PowerShell script]:

Code: Select all

Get-ChildItem (Get-Location) -force -recurse|Where {$_.PSIsContainer -and @(Get-ChildItem -literalPath $_.Fullname -force -recurse|Where {!$_.PSIsContainer}).Length -eq 0}|Remove-Item -force -recurse
KozakMak wrote: 2023-09-20, 06:41 UTC ... to recycle bin
Not sure about this part -- I don't utilize RecycleBin on my PCs.
Though, as I may assume if the RecycleBin is turned on it's a system-wise property, that affects all the common applications, including PowerShell Remove-Item a.k.a. del. Just check the above command and you will see.
Last edited by beb on 2023-09-20, 20:16 UTC, edited 1 time in total.
#278521 User License
Total Commander [always the latest version, including betas] x86/x64 on Win10 x64/Android 10
KozakMak
Senior Member
Senior Member
Posts: 352
Joined: 2021-05-24, 12:39 UTC
Location: UA

Re: Delete empty folders (one-click function)

Post by *KozakMak »

2beb
https://ibb.co/xqDr5my

must be run as elevated for some dirs :!:

...and of course recycle bin was bypassed
OS: Win10 | TC: latest x64
User avatar
beb
Senior Member
Senior Member
Posts: 430
Joined: 2009-09-20, 08:03 UTC
Location: Odesa, Ukraine

Re: Delete empty folders (one-click function)

Post by *beb »

KozakMak wrote: 2023-09-20, 10:18 UTC ...and of course recycle bin was bypassed
Feel free to try modifying the script to fit your needs regarding the relevant lookup results:
Google: PowerShell move folders to recycle bin

NB Out of curiosity, couldn't you be so kind as to remove this ritual "sorry for my poor English" from your signature, sinse it's just ridiculous.
Google: stop apologizing for bad English

Edit:
KozakMak wrote: 2023-09-20, 10:18 UTC ... https://ibb.co/xqDr5my ...
С:\Users\admin\AppData\...
By the way, I strongly do not recommend to nonchalantly carpet bombing locations alike %UserProfile%, %AppData% with such commands without proper consideration on where exactly you are and what exactly you are going to do. There are a bunch of pretty dumb apps that can store sensitive structures there (even represented by some seemingly aimless empty folders) and just would refuse to start anymore if you delete even the empty folders that were meant to belong to them to work, and they won't ever start again until you recreate the appropriate structures exactly as required (from the backups, or manually), or until you reinstall them.
#278521 User License
Total Commander [always the latest version, including betas] x86/x64 on Win10 x64/Android 10
KozakMak
Senior Member
Senior Member
Posts: 352
Joined: 2021-05-24, 12:39 UTC
Location: UA

Re: Delete empty folders (one-click function)

Post by *KozakMak »

beb wrote: 2023-09-20, 19:52 UTC and what exactly you are going to do.
циган знає що кобилі робить
I know what i'm doing :)

As a conclusion, I see that at the moment there are no transparent and safe solutions in this matter.
It should be something like RED:
- a list of everything that will be deleted should be displayed
- recycle bin must be used
- there should be error handling

...even TotalCmd with plugin search doesn't handle it correctly - try searching in %temp% and then deleting folder with write permission.
OS: Win10 | TC: latest x64
User avatar
beb
Senior Member
Senior Member
Posts: 430
Joined: 2009-09-20, 08:03 UTC
Location: Odesa, Ukraine

Re: Delete empty folders (one-click function)

Post by *beb »

2KozakMak

PowerShell_DeletEmptyFolders_withLog_example.ps1

Code: Select all

"";"displaying working directory:";([System.Environment]::CurrentDirectory)
"";"deleting empty directories..."
$stamp = (Get-Date -format "yyyyMMdd_HHmmssf");$log = $($stamp+'_del.log')
$output = $(Get-ChildItem (Get-Location) -force -recurse|Where {$_.PSIsContainer -and @(Get-ChildItem -literalPath $_.Fullname -force -recurse|Where {!$_.PSIsContainer}).Length -eq 0}|Remove-Item -force -recurse -Verbose -ErrorAction Ignore) 4>&1
"";"displaying deleted folders:";$output|Tee-Object $log -Append
"";"deleted folders logged in:";$log
"";pause
- a list of everything that will be deleted should be displayed
All the deleted empty folders are displayed in the example script.
- recycle bin must be used
What's the point? No way, this crap is not for me.
Instead, all the deleted folders are listed in the log file and can be restored by their full paths if needed.
- there should be error handling
In the example script error handling is simple -- ignore.

You can modify this script according to your needs, for instance:
PowerShell_DeletEmptyFolders_withLog_example_v2.ps1

Code: Select all

"";"displaying working directory:";([System.Environment]::CurrentDirectory)
"";"locating empty folders that can be deleted..."
$stamp = (Get-Date -format "yyyyMMdd_HHmmssf");$log = $($stamp+'_del.log')
$output = $(Get-ChildItem (Get-Location) -force -recurse|Where {$_.PSIsContainer -and @(Get-ChildItem -literalPath $_.Fullname -force -recurse|Where {!$_.PSIsContainer}).Length -eq 0}|Select-Object -ExpandProperty FullName|Out-File $log -Append) 4>&1
"";"displaying empty folders that can be deleted:";Get-Content($log)
This script locates all the empty folders that can be deleted, displays them, and saves their paths in the log file.
Then you can add a part to just delete those folders (or some of them) using their list in the log file.
#278521 User License
Total Commander [always the latest version, including betas] x86/x64 on Win10 x64/Android 10
KozakMak
Senior Member
Senior Member
Posts: 352
Joined: 2021-05-24, 12:39 UTC
Location: UA

Re: Delete empty folders (one-click function)

Post by *KozakMak »

for myself I decided to settle on this solution with .reg-menu

Code: Select all

Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\shell\del empty dirs\command]
@="cmd.exe /K for /f \"usebackq delims=\" %%d in (`\"dir \"%V\" /ad/b/s | sort /R\"`) do rd \"%%~d\""
OS: Win10 | TC: latest x64
User avatar
beb
Senior Member
Senior Member
Posts: 430
Joined: 2009-09-20, 08:03 UTC
Location: Odesa, Ukraine

Re: Delete empty folders (one-click function)

Post by *beb »

2KozakMak

PowerShell_DeletEmptyFolders_Display_Log_Remove.ps1

Code: Select all

"creating test folders structure:"
$md = '.\empty\empty'    ;New-Item $md -force -Type directory         >$null; $md
$md = '.\empty\folder'   ;New-Item $md -force -Type directory         >$null; $md
$md = '.\empty\structure';New-Item $md -force -Type directory         >$null; $md
$md = '.\empty\file.txt' ;New-Item $md -force -Type file -value "file">$null; $md

"";"working directory:";([System.Environment]::CurrentDirectory)

"";"empty folders that can be deleted:"
$stamp = (Get-Date -format "yyyyMMdd_HHmmssf");$log = $($stamp+'_del.log')
Get-ChildItem (Get-Location) -force -recurse|Where {$_.PSIsContainer -and @(Get-ChildItem -literalPath $_.Fullname -force -recurse|Where {!$_.PSIsContainer}).Length -eq 0}|Select-Object -ExpandProperty FullName|Out-File $log
Get-Content($log)

"";"empty folders actually being deleted:"
Get-ChildItem (Get-Location) -force -recurse|Where {$_.PSIsContainer -and @(Get-ChildItem -literalPath $_.Fullname -force -recurse|Where {!$_.PSIsContainer}).Length -eq 0}|Remove-Item -force -recurse -Verbose -ErrorAction Ignore 4>&1

"";"see deleted folders list in a log file:";$log
"";pause
Example script output:
https://i.imgur.com/9l9QcSA.png
#278521 User License
Total Commander [always the latest version, including betas] x86/x64 on Win10 x64/Android 10
KozakMak
Senior Member
Senior Member
Posts: 352
Joined: 2021-05-24, 12:39 UTC
Location: UA

Re: Delete empty folders (one-click function)

Post by *KozakMak »

working directory - must be %P for button
OS: Win10 | TC: latest x64
User avatar
beb
Senior Member
Senior Member
Posts: 430
Joined: 2009-09-20, 08:03 UTC
Location: Odesa, Ukraine

Re: Delete empty folders (one-click function)

Post by *beb »

working directory - must be %P for button
Nope. If your "working directory" implies it is a directory opened in the active panel, you don't need to pass any parameters to the script at all.

Just try a button like this:

Code: Select all

TOTALCMD#BAR#DATA
PowerShell y:\our\path\to\the\script\PowerShell_DeletEmptyFolders_Display_Log_Remove.ps1

TOTALCMD.EXE
PowerShell_DeletEmptyFolders_Display_Log_Remove

0
-1
#278521 User License
Total Commander [always the latest version, including betas] x86/x64 on Win10 x64/Android 10
Post Reply