Get File Size using the system plugin

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



RequestExecutionLevel User
ShowInstDetails Show
 
Function GetFileSize32 ; Note: Not accurate for files larger than 4 GiB
Exch $1
Push $0
FileOpen $0 $1 r
StrCpy $1 "" ; Return empty string on errors
StrCmp $0 "" end
System::Call 'KERNEL32::GetFileSize(pr0, p0)i.r1'
FileClose $0
IntFmt $1 "%u" $1 ; Int32 --> UInt32
IntCmp -1 $1 0 end end
StrCpy $1 ""
end:
Pop $0
Exch $1
FunctionEnd
 
Function GetFileSize64
Exch $1
Push $0
FileOpen $0 $1 r
StrCpy $1 "" ; Return empty string on errors
StrCmp $0 "" end
System::Call 'KERNEL32::GetFileSize(pr0, *i.s)i.r1?e'
FileClose $0
Pop $0
IntCmp -1 $1 0 succ succ
IntCmp 0 $0 succ
Pop $0
StrCpy $1 ""
Goto end
succ:
Pop $0
IntFmt $0 "%#x" $0
IntFmt $1 "$0%.8x" $1
end:
Pop $0
Exch $1
FunctionEnd
 
Section
 
Push "$SysDir\Shell32.dll"
Call GetFileSize32
Pop $0
StrCmp $0 "" +3
  IntOp $1 $0 >> 20
  DetailPrint "Shell32 is $1 MiB ($0 bytes)"
 
Push "C:\bigtestfile.bin"
Call GetFileSize64
System::Int64Op 0 +
Pop $0
System::Int64Op $0 >> 30
Pop $1
DetailPrint "$1 GiB ($0 bytes)"
 
SectionEnd