Archive: Get File Size


Get File Size
I need to Get the File Size of a File and Set it
to the Installation "Space Required:" Operation.

File or Files.

I have an Installer that Does Copy and Renaming
Operations and Currently the Installer just shows
as 0.0KB .....

-MichaelFlya-


If you need get file size: Getting File Size

If you need get file size or directory size: GetSize


I was using your GetSize script, I had few questions about it.

I am calling GetSize function using this syntax:

Section CaclSize
${GetSize} "$EXEDIR" "/M=*.* /S= /G=1" size sumFiles sumDirect
IfErrors 0 +2
MessageBox MB_Ok "Space Error"
SectionEnd

I am getting Invalid command: ${GetSize} error. When I call GetSize function. Why do you call this function using ${GetSize} ... syntax as opposed to Call GetSize...?

How do I pass results that I get with GetSize function to Components MUI page so that Space required will not be 0.0KB but will change to the result I get with GetSize function?


1. In "GetSize" Archive Page is written: If function used without header then put function in script before call it

2. This script is actual then you don't know the size of install data, otherwise rather manual write size in script.

Name "Output"
OutFile "Output.exe"
InstallDir "$EXEDIR"

!define MUI_CUSTOMFUNCTION_GUIINIT GuiInit
!include "MUI.nsh"
!insertmacro MUI_LANGUAGE "English"

!include "FileFunc.nsh"
!insertmacro GetSize


PageEx Directory
PageCallbacks '' DirectoryShow ''
PageExEnd

Page instfiles


Function DirectoryShow
FindWindow $0 "#32770" "" $HWNDPARENT
GetDlgItem $1 $0 1050 ;// Control ID 1023->1050 (${NSISDIR}\Contrib\UIs\modern.exe)
SendMessage $1 ${WM_SETTEXT} 1 "STR:Space required: $R0Mb"
FunctionEnd

Function GuiInit
Banner::show /NOUNLOAD /set 76 "Analizing install data" "Please wait..."
${GetSize} "$EXEDIR" "/S=0M" $R0 $R1 $R2
Banner::destroy
ShowWindow $HWNDPARENT 2
FunctionEnd

Section
SectionEnd

Script use header


Attachment - modern.exe with changed control ID 1023->1050:

In other words, GetSize is a !define which turns ${GetSize} into !insertmacro GetSize which inturn calls the GetSize Function.

If you don't have !define GetSize before you use ${GetSize} you get the compile error which you described.

We use commands like ${GetSize} because it converts function calls into a single line of code which doesn't need !insertmacro either (just looks neater).

-Stu


I Got "Get one File Size" to Work, but With no
Luck on Setting it to the Space Requirement,
Well At least Correctly Anyway. Here is some
of what I was thinking, but still no Luck.


Name "Output"
OutFile "Output.exe"
InstallDir "$EXEDIR"

!define MUI_CUSTOMFUNCTION_GUIINIT GuiInit
!include "MUI.nsh"
!insertmacro MUI_LANGUAGE "English"

Page directory
Page instfiles

Function GuiInit
Push "$EXEDIR\modern.exe"
Call FileSizeNew
Pop $0
MessageBox MB_OK "$0"
!define Sizetext "File size: $0 bytes."
FunctionEnd

SpaceTexts "${Sizetext}"

Section
;Set Permanently (Not What I want)
AddSize 7

;But Instead use This If It Were Possible
;AddSize $0
;or
;AddSize ${Sizetext}
SectionEnd

Function FileSizeNew
Exch $0
Push $1
FileOpen $1 $0 "r"
FileSeek $1 0 END $0
FileClose $1
Pop $1
Exch $0
FunctionEnd


I Wish it were Simple Enough to just use Addsize.

-MichaelFlya-

AddSize is a compile-time instruction. Use SectionSetSize and SectionGetSize for run-time.

-Stu


How Would I use SectionSetSize and
SectionGetSize Properly in my Script
that I have posted?

-MichaelFlya-


Change Section to Section "" mySection
Then SectionSetSize ${mySection} "size_in_kb"
Or you could just use 0 instead of ${mySection} (0 is the Section's index.)

-Stu


Great, I finally got it to pass the results to the
Space Required: Field. However It is in Bytes
not KB. The below is the working Script. How
do I get it to be in KB not Bytes?


Name "Output"
OutFile "Output.exe"
InstallDir "$EXEDIR"

!define MUI_CUSTOMFUNCTION_GUIINIT GuiInit
!include "MUI.nsh"
!insertmacro MUI_LANGUAGE "English"

Page directory
Page instfiles

Section "" mySection
SectionEnd

Function GuiInit
Push "$EXEDIR\AnotherProgram.exe"
Call FileSizeNew
Pop $0
SectionSetSize ${mySection} "$0"
FunctionEnd

Function FileSizeNew
Exch $0
Push $1
FileOpen $1 $0 "r"
FileSeek $1 0 END $0
FileClose $1
Pop $1
Exch $0
FunctionEnd


-MichaelFlya-

Divide it by 1024...

-Stu


Thanks Afrow Uk. That Helped !!!!


Pop $0
IntOp $0 $0 / 1024
SectionSetSize ${mySection} "$0"


Altho It's Showing about 1 Number off most
of the time on the Last Digit...Comparing
with Windows.... But Close Enough.. Thanks.

-MichaelFlya-

I'm guessing that would be because IntOp will always round to the nearest whole number whereas in Windows it will always round up for file sizes.

-Stu