DriveSpace

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


Page for NSIS 2.07 and below users

You can use the latest version of headers (recommended) or the following function code (put the function code in your script before calling it)

Function Description

____________________________________________________________________________
 
                            DriveSpace v1.2
____________________________________________________________________________
 
Thanks sunjammer (Function "CheckSpaceFree")
 
 
Get total, occupied or free space of the drive.
 
 
Syntax:
${GetSize} "[Drive]" "[Options]" $var
 
"[Drive]"     ; Disk to check
              ;     
"[Options]"   ; /D=[T|O|F]
              ;     /D=T  - Total space (default)
              ;     /D=O  - Occupied space
              ;     /D=F  - Free space
              ; /S=[B|K|M|G]
              ;     /S=B  - size in Bytes (default)
              ;     /S=K  - size in Kilobytes
              ;     /S=M  - size in Megabytes
              ;     /S=G  - size in Gigabytes
              ;
$var          ; Result: Size
 
 
Note:
-Error flag if disk isn't exist or not ready
-Error flag if syntax error


Example:

Section
	${DriveSpace} "C:\" "/D=F /S=M" $R0
	; $R0="2530"   megabytes free on drive C:
SectionEnd

Function Code

Function DriveSpace
	!define DriveSpace `!insertmacro DriveSpaceCall`
 
	!macro DriveSpaceCall _DRIVE _OPTIONS _RESULT
		Push `${_DRIVE}`
		Push `${_OPTIONS}`
		Call DriveSpace
		Pop ${_RESULT}
	!macroend
 
	Exch $1
	Exch
	Exch $0
	Exch
	Push $2
	Push $3
	Push $4
	Push $5
	Push $6
	ClearErrors
 
	StrCpy $2 $0 1 -1
	StrCmp $2 '\' 0 +3
	StrCpy $0 $0 -1
	goto -3
	IfFileExists '$0\NUL' 0 error
 
	StrCpy $5 ''
	StrCpy $6 ''
 
	option:
	StrCpy $2 $1 1
	StrCpy $1 $1 '' 1
	StrCmp $2 ' ' -2
	StrCmp $2 '' default
	StrCmp $2 '/' 0 -4
	StrCpy $3 -1
	IntOp $3 $3 + 1
	StrCpy $2 $1 1 $3
	StrCmp $2 '' +2
	StrCmp $2 '/' 0 -3
	StrCpy $4 $1 $3
	StrCpy $4 $4 '' 2
	StrCpy $2 $4 1 -1
	StrCmp $2 ' ' 0 +3
	StrCpy $4 $4 -1
	goto -3
	StrCpy $2 $1 2
	StrCpy $1 $1 '' $3
 
	StrCmp $2 'D=' 0 unit
	StrCpy $5 $4
	StrCmp $5 '' +4
	StrCmp $5 'T' +3
	StrCmp $5 'O' +2
	StrCmp $5 'F' 0 error
	goto option
 
	unit:
	StrCmp $2 'S=' 0 error
	StrCpy $6 $4
	goto option
 
	default:
	StrCmp $5 '' 0 +2
	StrCpy $5 'T'
	StrCmp $6 '' 0 +3
	StrCpy $6 '1'
	goto getspace
 
	StrCmp $6 'B' 0 +3
	StrCpy $6 1
	goto getspace
	StrCmp $6 'K' 0 +3
	StrCpy $6 1024
	goto getspace
	StrCmp $6 'M' 0 +3
	StrCpy $6 1048576
	goto getspace
	StrCmp $6 'G' 0 error
	StrCpy $6 1073741824
 
	getspace:
	System::Call /NOUNLOAD 'kernel32::GetDiskFreeSpaceExA(t, *l, *l, *l)i(r0,.r2,.r3,.)'
 
	StrCmp $5 T 0 +3
	StrCpy $0 $3
	goto getsize
	StrCmp $5 O 0 +4
	System::Int64Op /NOUNLOAD $3 - $2
	Pop $0
	goto getsize
	StrCmp $5 F 0 +2
	StrCpy $0 $2
 
	getsize:
	System::Int64Op $0 / $6
	Pop $0
	goto end
 
	error:
	SetErrors
	StrCpy $0 ''
 
	end:
	Pop $6
	Pop $5
	Pop $4
	Pop $3
	Pop $2
	Pop $1
	Exch $0
FunctionEnd