2014-03-19 168 views
2

我需要检查是否已插入USB加密狗。我使用下面的代码可以这样做:NSIS检查是否存在空目录

!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 

但是,这仅如果有E上的任何文件(或目录)的作品:。我如何检查一个空目录是否存在?

+1

该语法应该适用于普通目录,但您的示例只有一个驱动器号。 NSIS在内部使用FindFirstFile,MSDN表示“C:\ *”等对根目录有效...... – Anders

+0

@Anders确实有效。谢谢。 – dagur

+0

更新:它并不总是有效。看到我自己的解决方案下面 – dagur

回答

1

你的意思是一个特定的文件夹?

IfFileExists可与文件,通配符或目录一起使用。

!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 
+0

我确实尝试过,并没有奏效。虽然在我尝试这一点时可能还有其他问题。无论如何,我相信通配符是强制性的。这是从NSIS文档:如果你想检查一个文件是否是一个目录,使用IfFileExists DIRECTORY \ *。* _ – dagur

0

看看this function

另请注意,您的宏不起作用。宏是重复的代码,您不能多次使用相同的标签(在同一部分/功能中)。

+0

我认为你误解了我的问题,我真的不在乎目录是否为空或不是。但是,谢谢标签提示,NSIS肯定有很多缺陷。 – dagur

1

这里是一个代码来执行它从安装部分

!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." 

用法:从卸载部分

Push "Path to check" 
Call isEmptyDir 
Pop $0 

用法:

Push "Path to check" 
Call un.isEmptyDir 
Pop $0 

希望这有助于。

1

鉴于这已成为一个受欢迎的问题,我以为我会张贴我自己的解决方案。我上面尝试的答案对我来说并不可靠。我还没有测试khayk提供的解决方案,这可能是最好的解决方案。最后,我通过创建一个虚拟文件来解决问题,检查目录是否存在,然后删除虚拟文件。 Hacky,但适合我的使用。

!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