!ifexist Macro

From NSIS Wiki
Jump to navigationJump to search
Author: Zinthose (talk, contrib)


About

Macro to check for existence of file at compile time.

Example

    ${!IfNExist} "${__FILE__}"
        !error "File is Missing!  How is that possible!"
    !else
        !echo "File Exists!"
        InitPluginsDir
        SetOutPath $PLUGINSDIR\SourceCode
        file "${__FILE__}"
    !endif
 
    ${!IfExist} "Config\Special.ini"
        !warning "Using Config data from external file."
        ## TODO Inport Config Data
    !else
        !warning "Using Config data from source file."
        ## TODO Add Config Data
    !endif

Macro

/* ${!IfExist}
----------------------------------------------------------------------
    ${!IfExist} "C:\SomeFile.txt"
        !Error "File Exists!"
    !else
        !Error "File is Missing!"
    !endif */
    !define !IfExist `!insertmacro _!IfExist ""`
 
/* ${!IfNExist}
----------------------------------------------------------------------
    ${!IfNExist} "C:\SomeFile.txt"
        !Error "File is Missing!"
    !else
        !Error "File Exists!"
    !endif */
 
    !define !IfNExist `!insertmacro _!IfExist "n"`
    !macro _!IfExist _OP _FilePath
        !ifdef !IfExistIsTrue
            !undef !IfExistIsTrue
        !endif
        !tempfile "!IfExistTmp"
        !system `IF EXIST "${_FilePath}" Echo !define "!IfExistIsTrue" > "${!IfExistTmp}"`
        !include /NONFATAL "${!IfExistTmp}"
        !delfile /NONFATAL "${!IfExistTmp}"
        !undef !IfExistTmp
        !if${_OP}def !IfExistIsTrue
    !macroend