 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
daorc
Joined: 18 Oct 2006 Posts: 169
|
Posted: Thu Jun 04, 2009 10:04 am Post subject: Delete empty folders |
|
|
Hi all.
Here's a little script to delete empty all empty (less than 100kB) folders. I used 100kB instead of 0kB as having empty folders within other empty folders increases the size.
The user is prompted to select the folder containing all the empty folders to be deleted.
Hope this helps someone
| Code: | SetBatchLines, -1 ; Make the operation run at maximum speed.
folders_deleted = 0
FileSelectFolder, WhichFolder ; Ask the user to pick a folder.
Loop, %WhichFolder%\*, 2 ;,1
{
currentfolder:=A_LoopFileFullPath
FolderSizeKB = 0
Loop, %CurrentFolder%\*.*, , 1
{
FolderSizeKB += %A_LoopFileSizeKB%
}
;msgbox %CurrentFolder%`n%FolderSizeKB%
if FolderSizeKB < 100
{
;msgbox
fileremovedir,%CurrentFolder%,1
folders_deleted+=1
}
}
msgbox Deleted %folders_deleted% folders.
return |
|
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 8688
|
Posted: Fri Jun 05, 2009 10:34 am Post subject: |
|
|
Nice Idea!
Here is my variant that loops "inside-out".. should be faster
| Code: | SetBatchLines -1
FileSelectFolder, Folder, , % (Del:=0), Purge Empty Folders
If ( ErrorLevel Or Folder="" )
Return
Loop, %Folder%\*, 2, 1
FL .= ((FL<>"") ? "`n" : "" ) A_LoopFileFullPath
Sort, FL, R D`n ; Arrange folder-paths inside-out
Loop, Parse, FL, `n
{
FileRemoveDir, %A_LoopField% ; Do not remove the folder unless is empty
If ! ErrorLevel
Del := Del+1, RFL .= ((RFL<>"") ? "`n" : "" ) A_LoopField
}
MsgBox, 64, Empty Folders Purged : %Del%, %RFL% |
|
|
| Back to top |
|
 |
Murp|e
Joined: 12 Jan 2007 Posts: 531 Location: Norway
|
Posted: Fri Jun 05, 2009 11:33 am Post subject: |
|
|
That's funny, I also made my own version of this lately. daorc, it's important that you check the errorlevel after the fileselectfolder command. If the user clicks the Cancel button in the fileselectfolder window, your WhichFolder variable remains empty which could potentially have some nasty side effects! Also, deleting all folders less than 100 kb seeems quite risky as small files can be very important and a file of 100 kb can hold a whole lot of text.
I'm sure Skan's code is very good, but as often is the case I don't understand it.
Skan: What do you mean by "inside-out"? Does it mean that you start deleting empty folders at the "deepest" level and then work your way back up the folder tree? I think I had problems with my script because if a folder contained an empty subfolder, only the subfolder would be deleted. If I would run the script again it would delete the "now-empty" folder as well.
Here's my version:
| Code: |
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
FileSelectFolder, TopFolder, ::{20d04fe0-3aea-1069-a2d8-08002b30309d}, 3, Select topmost folder. All empty subfolders will be deleted.
if not(InStr(FileExist(TopFolder), "D"))
{
soundbeep
msgbox, Aborting...
exitapp
}
DelFolderCount := 0
DelErrorCount := 0
Loop, %TopFolder%\*.*, 2, 1
{
counter := 0
Loop, %A_LoopFileFullPath%\*.*, 0, 1
{
counter++
}
if counter = 0
{
FileRemoveDir, %A_LoopFileFullPath%, 1
if errorlevel <> 0
DelErrorCount++
else
DelFolderCount++
Fileappend, %ErrorLevel% errors when deleting %A_LoopFileFullPath%`n, DeleteLog.txt
}
}
msgbox, Deleted %DelFolderCount% empty folders. (%DelErrorCount% errors.) |
|
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 8688
|
Posted: Fri Jun 05, 2009 12:06 pm Post subject: |
|
|
| Murp|e wrote: | | Skan: What do you mean by "inside-out"? Does it mean that you start deleting empty folders at the "deepest" level and then work your way back up the folder tree? |
Exactly.. to avoid the following problem mentioned
| You wrote: | | I think I had problems with my script because if a folder contained an empty subfolder, only the subfolder would be deleted. If I would run the script again it would delete the "now-empty" folder as well. |
|
|
| Back to top |
|
 |
Murp|e
Joined: 12 Jan 2007 Posts: 531 Location: Norway
|
Posted: Fri Jun 05, 2009 12:51 pm Post subject: |
|
|
That's what I thought but I wasn't sure. So this line is the key to deleting the "deepest" folder first:
It sorts the filelist in reverse alphabetical order which means the folders with the longest paths (Deepest folders) are sorted before the folders with the shortest paths. A clever and elegant solution -well done! |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|