简体   繁体   中英

How to check the space in the drive by taking the path from the Destination Folder When installing the software using NSIS

How to check the space in the drive by taking the path from the Destination Folder When installing the software.

I am able to check the space of the specific drive (for eg, "C") using the below code snippet.

But I want to take the drive or path from the Destination folder dynamically and check the space of the drive whether there is enough space or not.

!define sysGetDiskFreeSpaceEx 'kernel32::GetDiskFreeSpaceExA(t, *l, *l, *l) i'    
function CheckSpaceFunc
      IntCmp $2 0 ignorequota
      ; obey quota
      System::Call '${sysGetDiskFreeSpaceEx}(r1,.r2,,.)'
      goto converttokb
      ; ignore quota
      ignorequota:
      System::Call '${sysGetDiskFreeSpaceEx}(r1,.,,.r2)'
      converttokb:
      ; convert the large integer byte values into managable kb
      System::Int64Op $2 / 1024
      Pop $2
      ; check space
      System::Int64Op $2 > $0
      Pop $2
    functionend


    Section "TestApp"

      SectionIn RO


      StrCpy $0 40000 ; kb u need
      StrCpy $1 'c:' ; check drive c: for space
      Call CheckSpaceFunc
      IntCmp $2 1 okay
      MessageBox MB_OK "Error: Not enough disk space"
      okay:

    SectionEnd

Could anyone please help me

The built-in directory page ( Page Directory or !insertmacro MUI_PAGE_DIRECTORY ) will perform the free space check for you and takes care of all the details.

It might be tempting to just do StrCpy $1 $InstDir 3 to get the drive letter and perform the check yourself but this can give you the wrong result because NTFS supports mounting other volumes as a folder .

GetDiskFreeSpaceEx does support directory paths but I believe the path has to exist so if you want to use $InstDir before $InstDir has been created then you must chop off subfolders until GetDiskFreeSpaceEx succeeds (or you only have a invalid drive letter left of the path).

Your !define should also be changed from GetDiskFreeSpaceExA to GetDiskFreeSpaceEx because it uses the t string type. This will make it Unicode compatible.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM