You are here

How to check if a dir is empty in NSIS

3 posts / 0 new
Last post
Ken Herbert
Ken Herbert's picture
Offline
Last seen: 1 hour 36 min ago
DeveloperModerator
Joined: 2010-05-25 18:19
How to check if a dir is empty in NSIS

I've just found a rogue directory being left behind by one of my dev tests, so in the next release I'd like to handle it automatically where it already exists on a users PC (plus it is about time I got my head around some NSIS).

The folder in question is %LOCALAPPDATA%\cache\picard, so I need to delete the picard folder if it exists (this I can do), then delete the cache dir if it is now empty (this I am stuck on).

The code so far:

!macro CustomCodePreInstall
   ${If} ${FileExists} "$LOCALAPPDATA\cache\picard\*.*"
     RMDir /r "$LOCALAPPDATA\cache\picard"
     ${If} ${FileExists} "$LOCALAPPDATA\cache\*.*"
       MessageBox MB_OK "'$LOCALAPPDATA\cache\*.*' exists"
       ${If} ${FileExists} "$LOCALAPPDATA\cache"
         MessageBox MB_OK "'$LOCALAPPDATA\cache' exists"
       ${EndIf}
     ${EndIf}
   ${EndIf}
!macroend

Running this the picard folder is deleted, but the checks for the cache folder (with and without *.*) both prove truthy, despite that the folder is definitely now empty.

So is there a predefined way to check if a directory is empty, or otherwise how could I include a function such as DirState in my code?

Bart.S
Offline
Last seen: 6 months 2 days ago
Developer
Joined: 2008-07-23 07:56
RMDir "$LOCALAPPDATA\cache"

NSIS ManualWithout /r, the directory will only be removed if it is completely empty. If /r is specified, the directory will be removed recursively [...]

Ken Herbert
Ken Herbert's picture
Offline
Last seen: 1 hour 36 min ago
DeveloperModerator
Joined: 2010-05-25 18:19
That easy

Thanks, too easy.

Considering I'm familiar with rmdir and what the /r switch does (or some version of them) on just about every command line I've ever used (plus I used it in my code above) I guess I feel kinda stupid for over-thinking this.

Once again thank you.

Log in or register to post comments