Check if dir is empty: Difference between revisions
From NSIS Wiki
Jump to navigationJump to search
(2 intermediate revisions by 2 users not shown) | |||
Line 3: | Line 3: | ||
== Description == | == Description == | ||
Because "ifFileExists" uses the format "ifFileExists 'Directory\*.*'" to determine whether the -directory- exists or not, you can't use it to check whether the directory is empty or not.<br> | Because "ifFileExists" uses the format "ifFileExists 'Directory\*.*'" to determine whether the -directory- exists or not, you can't use it to check whether the directory is empty or not.<br> | ||
Here is a simple but useful function to check whether a directory is empty. Based largely on http://nsis.sourceforge.net | Here is a simple but useful function to check whether a directory is empty. Based largely on http://nsis.sourceforge.net/Delete_dir_only_if_empty . | ||
== How To Use == | == How To Use == | ||
Line 43: | Line 43: | ||
_notempty: | _notempty: | ||
FindClose $0 | FindClose $0 | ||
ClearErrors | |||
Pop $1 # Stack: $0 | Pop $1 # Stack: $0 | ||
StrCpy $0 0 | StrCpy $0 0 |
Latest revision as of 17:49, 9 November 2010
Author: camillo (talk, contrib) |
Description
Because "ifFileExists" uses the format "ifFileExists 'Directory\*.*'" to determine whether the -directory- exists or not, you can't use it to check whether the directory is empty or not.
Here is a simple but useful function to check whether a directory is empty. Based largely on http://nsis.sourceforge.net/Delete_dir_only_if_empty .
How To Use
Push <directory> Call isEmptyDir Pop <result>
Example
Push "c:\somedir\" Call isEmptyDir Pop $0 StrCmp $0 1 0 +2 MessageBox MB_OK "Directory is empty" StrCmp $0 0 0 +2 MessageBox MB_OK "Directory is NOT empty"
The Function
Function isEmptyDir # Stack -> # Stack: <directory> Exch $0 # Stack: $0 Push $1 # Stack: $1, $0 FindFirst $0 $1 "$0\*.*" strcmp $1 "." 0 _notempty FindNext $0 $1 strcmp $1 ".." 0 _notempty ClearErrors FindNext $0 $1 IfErrors 0 _notempty FindClose $0 Pop $1 # Stack: $0 StrCpy $0 1 Exch $0 # Stack: 1 (true) goto _end _notempty: FindClose $0 ClearErrors Pop $1 # Stack: $0 StrCpy $0 0 Exch $0 # Stack: 0 (false) _end: FunctionEnd