Search code examples
nsis

How to check freespace available on installdir in nsis?


I have a requirement to bundle a zip file to my installer. I need nsis code such that it checks the available free space on installdir and copy that to a variable.


Solution

  • NSIS has a section attribute that sort of helps you do this: AddSize.

    If you really need the amount in a variable you must use the system plugin:

    System::Call 'kernel32::GetDiskFreeSpaceEx(t"$instdir",*l.r1,*l,*l)'
    DetailPrint $1
    

    or if you need Win9x/NT4 support:

    !include LogicLib.nsh
    
    Function GetDiskFree
    Exch $0
    Push $1
    System::Call 'kernel32::GetDiskFreeSpaceEx(tr0,*l0s,*l,*l)i.r1'
    ${If} $1 < 1
        Exch $2 ;Throw away result from Ex
        System::Call 'kernel32::SetCurrentDirectory(tr0)'
        System::Call 'kernel32::GetDiskFreeSpace(i0,*i0r1,*i0r0,*i0r2,*i)'
        System::Call 'kernel32::SetCurrentDirectory(to)'
        IntOp $1 $1 * $0
        System::Int64Op $1 * $2
        Exch
        Pop $2
    ${EndIf}
    Exch 2
    Pop $0
    Pop $1
    FunctionEnd
    
    
    Section
    Push $instdir
    Call GetDiskFree
    Pop $0
    System::Int64Op $0 / 1024 ;to kb
    Pop $1
    DetailPrint "$1 KiB ($0)"
    SectionEnd