AutoHotkey Community

It is currently May 26th, 2012, 7:20 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Delete empty folders
PostPosted: June 4th, 2009, 11:04 am 
Offline

Joined: October 18th, 2006, 8:07 pm
Posts: 169
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 5th, 2009, 11:34 am 
Online
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
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%


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 5th, 2009, 12:33 pm 
Offline

Joined: January 12th, 2007, 4:30 am
Posts: 531
Location: Norway
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.)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 5th, 2009, 1:06 pm 
Online
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 5th, 2009, 1:51 pm 
Offline

Joined: January 12th, 2007, 4:30 am
Posts: 531
Location: Norway
That's what I thought but I wasn't sure. So this line is the key to deleting the "deepest" folder first:
Code:
Sort, FL, R D`n

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!


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Scratch, Xx7 and 17 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group