I need to check if an USB dongle has been inserted. I use the following code to do so:
!macro HAS_USB_DONGLE
IfFileExists "E:\*.*" hasDongle 0
MessageBox MB_OK "USB Dongle is not inserted. Please insert the USB dongle and re-run this installer."
Abort
hasDongle:
!macroend
This however only works if there are any files (or directories) on E:. How can I check if an empty directory exists?
Do you mean a specific folder?
IfFileExists
can be used with a file, a wildcard, or a directory .
!macro HAS_USB_DONGLE
IfFileExists "E:\ThisIsTheFolderYouAreLookingFor" hasDongle 0
MessageBox MB_OK "USB Dongle is not inserted. Please insert the USB dongle and re-run this installer."
Abort
hasDongle:
!macroend
Here is a code to perform it
!macro uni_isEmptyDir un
Function ${un}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
!macroend
; make isEmptyDir function available both for installer and uninstaller
!insertmacro uni_isEmptyDir ""
!insertmacro uni_isEmptyDir "un."
Usage from the installer section:
Push "Path to check"
Call isEmptyDir
Pop $0
Usage from the uninstall section:
Push "Path to check"
Call un.isEmptyDir
Pop $0
Hope this helps.
Seeing as this has become a popular question, I thought I'd post my own solution. The answers which I tried above were not reliable for me. I have not tested the solution provided by khayk, that may be the best one. In the end I solved the problem by creating a dummy file, checking if directory exists and then delete the dummy file. Hacky, but works well for my use.
!macro HAS_USB_DONGLE
FileOpen $0 "$USB_DIR\dummy" w
FileClose $0
ClearErrors
IfFileExists "$USB_DIR*" hasDongle 0
MessageBox MB_OK "USB Dongle is not inserted. Please insert the USB dongle and re-run this installer."
Abort
hasDongle:
Delete "$USB_DIR\dummy"
ClearErrors
!macroend
Take a look at this function .
Also note, that your macro would not work. Macros are repeated code and you can't use the same label multiple times (within the same section/function).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.