Old 16th January 2013, 10:55   #1
PoRtAbLe_StEaLtH
Junior Member
 
Join Date: Jul 2011
Posts: 49
IsEmptyDir check

Im not an expert .. but find NSIS an intriguing language.
I love the simplicity, and small overhead.

I discover new things everyday.

For instance.. today.. i learned:
PHP Code:
IfFileExists 
if used with a wildcard to detect if files exists, will always return true, due to ".", "..".

Example:
PHP Code:
IfFileExists `$INSTDIR\Dir\*.*0 skip
     MessageBox MB_OK 
"Did not skip."
skip
Using Logic:
PHP Code:
${If} ${FileExists} `$INSTDIR\Dir\*.*`
     
MessageBox MB_OK "Did not skip."
${EndIf} 
Both examples will execute MessageBox, because it will detect ".", "..".

I have put together a useful script that will allow both above examples to work.

PHP Code:
;!include logiclib.nsh

!define IsEmptyDir `"" LL_IsEmptyDir`
!
macro _LL_IsEmptyDir _a _b _t _f
    
!insertmacro _LOGICLIB_TEMP
    
${EmptyDir} `${_b}` $_LOGICLIB_TEMP
    
!insertmacro _$_LOGICLIB_TEMP 1 `${_t}` `${_f}`
!
macroend
!macro EmptyDir _DIR _VAR
    Push 
${_DIR}
    
Call EmptyDir
    Pop 
${_VAR}
!
macroend
!define EmptyDir `!insertmacro EmptyDir`
Function 
EmptyDir
renamed isempty from:
http://nsis.sourceforge.net/Check_if_dir_is_empty
  # Stack ->                    # Stack: <directory>
  
Exch $0                       # Stack: $0
  
Push $1                       # Stack: $1, $0
  
FindFirst $$"$0\*.*"
  
strcmp $"." 0 _notempty
    FindNext 
$$1
    strcmp 
$".." 0 _notempty
      ClearErrors
      FindNext 
$$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 

Now you can do this:
PHP Code:
${IfNot} ${IsEmptyDir} `$INSTDIR\Dir`
     
MessageBox MB_OK "Did not skip."
${EndIf} 

im sure the script can be shortened/improved..
But im not an expert..
infact.. i would truly love it if someone could improve the "isEmptyDir" script w/ Logic.
PoRtAbLe_StEaLtH is offline   Reply With Quote
Old 16th January 2013, 12:29   #2
gfm688
Junior Member
 
Join Date: Jul 2010
Posts: 12
System::Call Shlwapi::PathIsDirectoryEmpty(t"$INSTDIR\Dir")i.r0
StrCmp $0 1 0 +2
MessageBox MB_OK "Directory is empty"
gfm688 is offline   Reply With Quote
Old 16th January 2013, 12:48   #3
PoRtAbLe_StEaLtH
Junior Member
 
Join Date: Jul 2011
Posts: 49
Amazing

Quote:
Originally Posted by gfm688 View Post
System::Call Shlwapi::PathIsDirectoryEmpty(t"$INSTDIR\Dir")i.r0
StrCmp $0 1 0 +2
MessageBox MB_OK "Directory is empty"

Amazing.

all of that code can now be shrunken to this:
PHP Code:
!define IsEmptyDir `"" LL_IsEmptyDir`
!
macro _LL_IsEmptyDir _a _b _t _f
    
!insertmacro _LOGICLIB_TEMP
         System
::Call Shlwapi::PathIsDirectoryEmpty(t"${_b}")i.r0 $_LOGICLIB_TEMP
    
!insertmacro _$_LOGICLIB_TEMP 1 `${_t}` `${_f}`
!
macroend 
to call:
PHP Code:
;!include LogicLib.nsh

${IfNot} ${IsEmptyDir} `$Dir_to_check`
     
"code to execute"
${EndIf} 

Thank you gfm688.

Last edited by PoRtAbLe_StEaLtH; 16th January 2013 at 14:44. Reason: Improved macro
PoRtAbLe_StEaLtH is offline   Reply With Quote
Old 17th January 2013, 00:28   #4
gfm688
Junior Member
 
Join Date: Jul 2010
Posts: 12
!define IsEmptyDir `"" LL_IsEmptyDir`
!macro _LL_IsEmptyDir _a _b _t _f
!insertmacro _LOGICLIB_TEMP
System::Call `Shlwapi::PathIsDirectoryEmpty(t"${_b}")i.s`
Pop $_LOGICLIB_TEMP
!insertmacro _= $_LOGICLIB_TEMP 1 `${_t}` `${_f}`
!macroend
gfm688 is offline   Reply With Quote
Old 17th January 2013, 04:28   #5
PoRtAbLe_StEaLtH
Junior Member
 
Join Date: Jul 2011
Posts: 49
Quote:
Originally Posted by gfm688 View Post
!define IsEmptyDir `"" LL_IsEmptyDir`
!macro _LL_IsEmptyDir _a _b _t _f
!insertmacro _LOGICLIB_TEMP
System::Call `Shlwapi::PathIsDirectoryEmpty(t"${_b}")i.s`
Pop $_LOGICLIB_TEMP
!insertmacro _= $_LOGICLIB_TEMP 1 `${_t}` `${_f}`
!macroend
how come it works for me without popping?
PoRtAbLe_StEaLtH is offline   Reply With Quote
Old 17th January 2013, 09:18   #6
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,097
Quote:
Originally Posted by gfm688 View Post
!define IsEmptyDir `"" LL_IsEmptyDir`
!macro _LL_IsEmptyDir _a _b _t _f
!insertmacro _LOGICLIB_TEMP
System::Call `Shlwapi::PathIsDirectoryEmpty(t"${_b}")i.s`
Pop $_LOGICLIB_TEMP
!insertmacro _= $_LOGICLIB_TEMP 1 `${_t}` `${_f}`
!macroend
You should change this code so it compares <> 0, BOOL can be > 1, it usually means success is non-zero...

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote
Old 17th January 2013, 12:23   #7
PoRtAbLe_StEaLtH
Junior Member
 
Join Date: Jul 2011
Posts: 49
Thank you

Quote:
Originally Posted by Anders View Post
You should change this code so it compares <> 0, BOOL can be > 1, it usually means success is non-zero...
How's this?

PHP Code:
!define IsEmptyDir `"" LL_IsEmptyDir`
    !
macro _LL_IsEmptyDir _a _b _t _f
    
!insertmacro _LOGICLIB_TEMP
         System
::Call `Shlwapi::PathIsDirectoryEmpty(t"${_b}")i.s`
    
Pop $_LOGICLIB_TEMP
    
!insertmacro _<> $_LOGICLIB_TEMP 0 `${_t}` `${_f}`
!
macroend 

any recommendations for checking stack in a function/macro?
PoRtAbLe_StEaLtH is offline   Reply With Quote
Reply
Go Back   Winamp & Shoutcast Forums > Developer Center > NSIS Discussion

Tags
dir, empty, file, iffileexists, isemtpydir

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump