Jump to content

Empty Folder Cleanup


OldGuyWalking
 Share

Recommended Posts

I've looked for free apps that remove empty folders but hadn't found any so I wrote this. Over time I wind up with alot of unnecessary empty folders that I don't know are empty. Some of these are left from apps that I've uninstalled. Others are left after I've run programs that removed duplicate files but not the empty folders.

I've made some notes at the top of the script. It's not a sophisticated program. I created it based on an article I read in PCMag from 2003 and I've been doing much the same thing with batch files and a couple of manual steps. I'm now up to 10 different drives and doing it manually was eating up too much time.

I'm not aware of any applications that bomb if one of it's empty subfolders isn't in place. I haven't had any problems in that area that I'm aware of. However, since I don't own or use every program in the known universe .. please be cautious.

I wrote this in AutoIt 3.1.1.5 beta and compiled it under that version. However, I don't believe there's any reason why this won't run or compile under earlier versions.

If it won't compile under 3.1 let me kow what it stops on and I'll rewrite that part if I can.

; AutoIt Version: 3.1.1.5 beta
; Author: William Oliver
; Script uses comspec to call the DOS Dir and Sort commands to create a list of folders in reverse order. A batch file is created from that list and is run.
; The basic idea is that DOS won't let you delete a directory with something in it. An empty subfolder is "something" in its parent folder.
; Since this works from the lowest subfolder up, this will delete any empty subfolders before it gets to the parent folder which then, if empty, will be deleted.
;
; The DOS Dir syntax came from a 02-01-2003 PCMag article.  If interested do a Google search on "Empties.bat" and you'll get to it.
;
; Only tested on XP Home sp2 and XP Professional sp2.
;
; ToDo:
;   Compare the before and after array of folders and create the MD batch file with just the folder's that were deleted. Currently uses all folders.
;   Check to see if the media that is being used is read/write enabled. This does not work on CD-ROM's.

opt("MustDeclareVars", 1)

Dim $curFolder; Folder Selected under FileSelectFolder below.
Dim $sDelFldrBat ; Batch File Created in Selected Folder to Delete Folders in that Tree
Dim $sMakeFldrBat; Batch File Created in Selected Folder to Make Folders in that Tree
Dim $sFolderList1; List of Folders read in the first pass before empty folders deleted.
Dim $sFolderList2; List of Folders read in the second pass after empty folders deleted.
Dim $f ; Counter
Dim $iFolderCount1; Count of Folders read in the first pass
Dim $iFolderCount2; Count of Folders read in the second pass
Dim $iDiff   ; Difference between the two counts.
Dim $sFileRec  ; Folder Name read in from the Folder List.
Dim $sFileRec1  ; Folder Name with RD in front and quotes around folder name
Dim $sFileRec2  ; Folder Name with MD in front and quotes around folder name
Dim $iFile  ; File number
Dim $sFolders1  ; Array of folders read in the first pass
Dim $sFolders2  ; Array of folders read in the second pass
Dim $iMsgBoxAnswer; Answer box response from CodeWizard
Dim $iResult    ; If the WriteBatFile function returns a -1 then abort. 

; Select the folder you want to delete empty folders from.
$curFolder = FileSelectFolder("Delete Empty Folders - Select Location to Start From", "")
$iMsgBoxAnswer = MsgBox(36, "Delete Empty Folders", "This will delete all empty folders from" & @CRLF & @CRLF & $curFolder & @CRLF & @CRLF & "Do you want to continue?")
Select
    Case $iMsgBoxAnswer = 7;No
        Exit
EndSelect
; Set some variables to the current folder and the names of the files being created in those folders.
$sDelFldrBat = $curFolder & "\DelFolders.Bat"
$sMakeFldrBat = $curFolder & "\MakeFolders.Bat"
$sFolderList1 = $curFolder & "\FolderList1.txt"
$sFolderList2 = $curFolder & "\FolderList2.txt"
; Delete them if they already exist in this folder.
If FileExists($sDelFldrBat) Then
    FileDelete($sDelFldrBat)
EndIf
If FileExists($sMakeFldrBat) Then
    FileDelete($sMakeFldrBat)
EndIf
If FileExists($sFolderList1) Then
    FileDelete($sFolderList1)
EndIf
If FileExists($sFolderList2) Then
    FileDelete($sFolderList2)
EndIf

; Write the folder names to FolderList1.txt in descending order.
SplashTextOn("", "Building First List of Folders for" & @CRLF & $curFolder, "400", "160", "-1", "-1", 33, "Arial", "16", "700")
RunWait(@ComSpec & " /c " & 'DIR /AD /B /S | SORT /R >' & "FolderList1.txt", $curFolder, @SW_HIDE)
Sleep(500)
SplashOff()

; If the first list exists then read it into an array.
If FileExists($sFolderList1) Then
    $iFile = FileOpen($sFolderList1, 0)
    If $iFile <> - 1 Then
        $sFolders1 = StringSplit(FileRead($iFile, FileGetSize($sFolderList1)), @LF)
    EndIf
    FileClose($iFile)
    $iFolderCount1 = UBound($sFolders1)
    
; Create two batch files. Add the RD (DOS Remove Directory command) and the MD (Make Directory DOS command) and the quotes around the folder name
; in case there are spaces in the folder names.
    
    For $f = 1 To $iFolderCount1 - 2
        $sFileRec = StringStripWS($sFolders1[$f], 3)
        $sFileRec1 = "RD " & Chr(34) & $sFileRec & Chr(34)
        $sFileRec2 = "MD " & Chr(34) & $sFileRec & Chr(34)
        $iResult = WriteBatFile($sDelFldrBat, $sFileRec1)
        If $iResult = -1 Then
            MsgBox(0,"Error","Error Occured while writing to " & $sDelFldrBat & @LF & "Exiting Program")
            Exit
        EndIf
    
        $iResult = WriteBatFile($sMakeFldrBat, $sFileRec2)
        If $iResult = -1 Then
            MsgBox(0,"Error","Error Occured while writing to " & $sMakeFldrBat & @LF & "Exiting Program")
            Exit
        EndIf
    Next
EndIf

; If the Del Folder batch file wasn't created then exit. There's either no folder or the user is trying to run this on a CD/DVD or some other media this won't work on.
If Not FileExists($sDelFldrBat) Then
    Exit
EndIf

; Creates the list of folders that are left. Reads the folderlist2.txt to get the remaining folder count. Displays both counts and the difference, if any.
RunWait(@ComSpec & " /c " & $sDelFldrBat, $curFolder, @SW_HIDE)
SplashTextOn("", "Building Second List of Folders for" & @CRLF & $curFolder, "400", "160", "-1", "-1", 33, "Arial", "16", "700")
RunWait(@ComSpec & " /c " & 'DIR /AD /B /S | SORT /R >' & "FolderList2.txt", $curFolder, @SW_HIDE)
Sleep(500)
SplashOff()
If FileExists($sFolderList2) Then
    $iFile = FileOpen($sFolderList2, 0)
    If $iFile <> - 1 Then
        $sFolders2 = StringSplit(FileRead($iFile, FileGetSize($sFolderList2)), @LF)
    EndIf
    FileClose($iFile)
    $iFolderCount2 = UBound($sFolders2) - 2
EndIf
$iFolderCount1 = $iFolderCount1 - 2
$iDiff = $iFolderCount1 - $iFolderCount2
If $iDiff < 0 Then
    $iDiff = 0
EndIf
MsgBox(0, "Delete Empty Folders", " First Count = " & $iFolderCount1 & @CRLF & "Second Count = " & $iFolderCount2 & @CRLF & "    Deleted = " & $iDiff)
Exit

Func WriteBatFile($sFileBat, $sFileRec)
    Local $iWFile
    $iWFile = FileOpen($sFileBat, 1)
    If $iWFile = -1 Then
        MsgBox(0, "Error", "Unable to Open " & $sFileBat)
        Return -1
    EndIf
    FileWriteLine($iWFile, $sFileRec)
    FileClose($iWFile)
EndFunc  ;==>WriteBatFile
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...