My first extended version of your DelEmpty.AHK is ready, working OK in my tests.
I must admit, not very much of your code has survived, but it helped me a lot, thanks again.
I labelled it version 2.0, considering yours as 1.x.
Feature overview:
+ Does not alter any directories existing on both sides, only deletes empty dirs.
+ DelAll mode: Optionally all empty dirs can be deleted (like in TC's SyncTool now) with second parameter /ALL.
+ Preview mode: Only telling what it would delete. No final deletion is done whithout additional third parameter.
+ Confirmation mode: first display target path, then delete after positive confirmation.
+ Cancel/Skip options while previewing.
+ Automatic parameter format correction (as far as possible) and different error messages.
Parameter format for TC (silent mode without confirmation; syntax is also displayed when called without parameters):
Code: Select all
"%T\" "%P\" /X
Works as expected in my tests, but use at your own risk.
If someone finds any bugs, please let me know.
Download of compiled exe (together with source): Download V2.02 (dropbox)
AHK source:
Code: Select all
; Usage with TC:
; You have to put the following into the TC Parameters field:
;
; Silent Execute mode: only tells about errors, no unneeded status info
; "%T\" "%P\" /X
;
; Confirmation mode: replace "/X" with "/XC" to ask for confirmation before deleting each dir
; "%T\" "%P\" /XC
;
; Preview mode: leave out the "/X" to only display the name of folders to delete (no deletion is done):
; "%T\" "%P\"
;
; DelAll mode: replace the second path with "/ALL" to delete ALL empty folders in the first directory:
; "%T\" /ALL /XC
;
; IMPORTANT:
; order of parameters must be kept:
; TARGET as #1, COMPARE-SOURCE or /ALL as #2, /X as #3
#NoEnv
Version = 2.02
StringUpper, ScriptName, A_ScriptName
Syntax = /////////////////////////////////////////////////////////////////
Syntax = %Syntax%`n/// %ScriptName% V%Version% (W) StatusQuo 2007-2008
Syntax = %Syntax%`n
Syntax = %Syntax%`nSyntax: %ScriptName% <target-dir_delete-empty>\\ [<source-dir_compare>\\ | /ALL] [/X | /XC]
Syntax = %Syntax%`n ==> A trailing backspace has to be doubled or omitted!!
; Syntax = %Syntax%`n (attaching a dot instead should help, too => "D:\.") ; ! not working in some tests !
Syntax = %Syntax%`n`n=>In TC use:
Syntax = %Syntax%`n`n "`%T\" "`%P\" /X
Syntax = %Syntax%`nto delete single empty dirs in the target (inactive) panel silently.
Syntax = %Syntax%`n`n "`%T\" "`%P\" /XC
Syntax = %Syntax%`ndoes the same, but asks for confirmation for every dir,
Syntax = %Syntax%`n`n "`%T\" "`%P\"
Syntax = %Syntax%`nto only report the empty dirs to the user, without really deleting them (preview mode).
Syntax = %Syntax%`n`n "`%T\" /ALL /XC
Syntax = %Syntax%`nto delete ALL empty dirs in the target (inactive) panel after asking for confirmation.
Syntax = %Syntax%`n`n`n=> Special thanks to Hacker for the inspiration and examples.
nFound = 0
nDeleted = 0
StringUpper, test, 3 ; test = %3%
boIsSilent := (test = "/X")
boIsConfirm := (test = "/XC")
boIsPreview := (test=)
StringUpper, test, 2 ; test = %2%
boDelAllEmpty := (test = "/ALL")
test = %2%
test := StrLen(test)
; 0 parameters - too few parameters
if (%test% = 0)
{ MsgBox, 16, %A_ScriptName% %Version%, Parameter error: `nMissing parameters. `n`n%Syntax%
exit 255
}
; 1 parameter, 2nd parameter empty => cause can be missing DOUBLE backslash at parameter end
If test = 0
{ MsgBox, 16, %A_ScriptName% %Version%, Parameter error: `nToo few parameters OR missing DOUBLE backslash at parameter ends. `n`n%Syntax%
exit 254
}
; dir for comparing not existing
if (boDelAllEmpty)
{
2 := "NUL"
} else {
IfNotExist, %2%
{ MsgBox, 16, %A_ScriptName% %Version%, Parameter error: `nNot found: Directory structure for comparing. `n`n%Syntax%
exit 253
}
}
; correct format of first 2 parameters
Loop, 2
{
StringMid, TestPar, %A_Index%, 1
; skip, if Parameter is an option, no dir name
test := SubStr(TestPar, 1, 1)
IfEqual, test, /
{ continue
}
; add missing backslash at string end
test := SubStr(TestPar, StrLen(TestPar), 1)
IfNotEqual, test, `\
{ %A_Index% = %TestPar%\
}
; remove duplicate backslashes at string end
Loop
{ StringMid, TestPar, %A_Index%, 1
if StrLen(TestPar)<2 ; only 1 char left
break
test := SubStr(TestPar, StrLen(TestPar)-1, 1)
IfNotEqual, test, `\ ; done
break
%A_Index% := SubStr(TestPar, 1, StrLen(TestPar)-1)
}
}
Loop, %1%*.*, 2, 1
{
Empty=1
Loop, %A_LoopFileLongPath%\*.*, 0, 1 ; recursively search for contained directories
{ Empty=0
Break
}
; after here we get only empty dirs (without files)
IfEqual, Empty, 0 ; if contents are found, go to next directory
{ Continue
}
;IfExist, %2% ; %2% existing, already tested above
{ target_process=%A_LoopFileLongPath%
StringReplace, source_compare, target_process, %1%, %2% ; build path to compare
IfExist, %source_compare% ; dir exists in compare-source dir - no delete, go on with next dir
{ Continue
} }
;nFound = 1
nFound += 1
if boIsSilent ; /X given: Silent Execute mode, only delete
{ FileRemoveDir, %target_process%, 1 ; force: also delete, if it contains folders, but no files
nDeleted += 1
}
else if boIsConfirm ; /XC given: Confirmation mode, delete AND echo
{ ;Msgbox, 3,, (Confirmation mode) `n`nDelete : %target_process%`n-because-`nnot found in: %2%
Msgbox, 3,, (Confirmation mode) `n`nNot found in: %2%`n-so-`ndelete : %target_process%
IfMsgbox, No
continue
IfMsgBox, Cancel
exit
FileRemoveDir, %target_process%, 1 ; force: also delete, if it contains folders, but no files
nDeleted += 1
} else ; without /X, /XC: Preview mode, only show, do not delete anything
{ ;Msgbox, 1,, (Preview mode, only show) `n`nDelete : %target_process%`n-because-`nnot found in: %2%
Msgbox, 1,, (Preview mode, only show) `n`nNot found in: %2%`n-so-`ndelete : %target_process%
IfMsgBox, Cancel
exit
} }
sOut =
if nFound=0
{ sOut = No empty dirs found in `n %1%
}
else
{ if nFound > 1
sOut = %nFound% empty dirs found in `n %1%
else
sOut = %nFound% empty dir found in `n %1%
;if nDeleted
sOut = %sOut% `n%nDeleted% removed.
}
if not boIsSilent ; in Silent Execute mode also don't display summary
MsgBox %sOut%
- Update to V2.01
- Update to V2.02: added support for parameter "/ALL"