Recursively remove empty parent directories: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
(note inusage)
No edit summary
Line 39: Line 39:
${RMDirUP} "$INSTDIR"  ;remove $INSTDIR's parents (if each is empty ONLY)
${RMDirUP} "$INSTDIR"  ;remove $INSTDIR's parents (if each is empty ONLY)
</highlight-nsis>
</highlight-nsis>
== Another function (by jiake) ==
<highlight-nsis>
Function RecursiveRemoveEmptyDirectory
Exch $R0
Push $R1
Push $R2
lbl_loop:
# Strip the last backslash
StrCpy $R1 $R0 "" -1
StrCmp $R1 "\" 0 +2
StrCpy $R0 $R0 -1
# Check if it is root path
StrCpy $R1 $R0 2
StrCmp $R1 $R0 lbl_end
# Check if it is empty
FindFirst $R1 $R2 "$R0\*.*"
lbl_find:
StrCmp $R2 "." +2
StrCmp $R2 ".." 0 +3
FindNext $R1 $R2
Goto lbl_dir
FindClose $R1
# After skip "." & ".."
StrCmp $R2 "" 0 lbl_end
# No more files found, it is an empty folder
RMDir $R0
# Get the parent folder the loop
GetFullPathName $R0 "$R0\.."
Goto lbl_loop
lbl_end:
Pop $R2
Pop $R1
Pop $R0
FunctionEnd
Section -Test
# Create a temp direcotry
CreateDirectory "$TEMP\1\22\333\4444\55555"
# Recursive remove empty directories
Push "$TEMP\1\22\333\4444\55555"
Call RecursiveRemoveEmptyDirectory
SectionEnd
</highlight-nsis>
== Known issues ==
If a directory include more than one empty sub directories, this function fails.


[[Category:Disk, Path & File Functions]]
[[Category:Disk, Path & File Functions]]

Revision as of 07:38, 26 July 2013

Author: ParallaxTZ (talk, contrib)


Description

This function and example function call can be used to recursively delete empty parent folders of a given folder.

(Courtesy of the folks at Redbug Technologies (www.redbugtech.com))

The Script

Function un.RMDirUP
         !define RMDirUP '!insertmacro RMDirUPCall'
 
         !macro RMDirUPCall _PATH
                push '${_PATH}'
                Call un.RMDirUP
         !macroend
 
         ; $0 - current folder
         ClearErrors
 
         Exch $0
         ;DetailPrint "ASDF - $0\.."
         RMDir "$0\.."
 
         IfErrors Skip
         ${RMDirUP} "$0\.."
         Skip:
 
         Pop $0
FunctionEnd

Usage

The function declaration (as above) must appear before the call in the installation script.

RMDir /r "$INSTDIR"     ;remove $INSTDIR and all files/subfolders
${RMDirUP} "$INSTDIR"   ;remove $INSTDIR's parents (if each is empty ONLY)

Another function (by jiake)

Function RecursiveRemoveEmptyDirectory
	Exch $R0
	Push $R1
	Push $R2
lbl_loop:
	# Strip the last backslash
	StrCpy $R1 $R0 "" -1
	StrCmp $R1 "\" 0 +2
	StrCpy $R0 $R0 -1
	# Check if it is root path
	StrCpy $R1 $R0 2
	StrCmp $R1 $R0 lbl_end
	# Check if it is empty
	FindFirst $R1 $R2 "$R0\*.*"
lbl_find:
	StrCmp $R2 "." +2
	StrCmp $R2 ".." 0 +3
	FindNext $R1 $R2
	Goto lbl_dir
	FindClose $R1
	# After skip "." & ".."
	StrCmp $R2 "" 0 lbl_end
	# No more files found, it is an empty folder
	RMDir $R0
	# Get the parent folder the loop
	GetFullPathName $R0 "$R0\.."
	Goto lbl_loop
lbl_end:
	Pop $R2
	Pop $R1
	Pop $R0
FunctionEnd
 
Section -Test
	# Create a temp direcotry
	CreateDirectory "$TEMP\1\22\333\4444\55555"
	# Recursive remove empty directories
	Push "$TEMP\1\22\333\4444\55555"
	Call RecursiveRemoveEmptyDirectory
SectionEnd

Known issues

If a directory include more than one empty sub directories, this function fails.