FreeDiskSpace

From NSIS Wiki
Jump to navigationJump to search
Author: Dr.Sweety (talk, contrib)


Note: This code will not work on Win95 RTM. Win95 OSR2 or WinNT4 is required.

Description

Based on the function CheckSpaceFree from sunjammer I created the following function which returns the free space (in kb) available for the path specified

The Script

OutFile "FreeSpace.exe"
!define sysGetDiskFreeSpaceEx 'kernel32::GetDiskFreeSpaceEx(t, *l, *l, *l)i'
 
; $0 - Path to check (can be a drive 'C:' or a full path 'C:\Windows')
; $1 - Return value, free space in kb
 
function FreeDiskSpace
  Exch $0
  System::Call '${sysGetDiskFreeSpaceEx}(r0,.,,.r0)'
  ; convert the large integer byte values into managable kb
  System::Int64Op $0 / 1024
  Exch $0
functionend
 
section -
  Push '$WinDir' ; check how much free space is left for the path C:\Windows
  Call FreeDiskSpace
  Pop $1
  MessageBox MB_OK "Free disk space in $0 : $1 kb"
 
  StrCpy $2 12345 ; Free space required by you (in kb)
  System::Int64Op $1 > $2 ; Compare the space required and the space available
  Pop $3 ; Get the result ...
 
  IntCmp $3 1 okay ; ... and compare it
    MessageBox MB_OK "Error: Not enough disk space!"
  okay:
    MessageBox MB_OK "Enough disk space available!"
sectionend