GetSize: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (Added category links.)
 
(8 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{|align=right
{{PageAuthor|Instructor}}
|<small>Author: [[{{ns:2}}:Instructor|Instructor]] ([[{{ns:3}}:Instructor|talk]], [[{{ns:-1}}:Contributions/Instructor|contrib]])</small>
|}
<br style="clear:both;">
== Links ==
; Latest version of headers "nsh.zip":
: http://forums.winamp.com/showthread.php?s=&threadid=203228&goto=lastpost


If function used without header then put function in script before call it
Function "GetSize" was superseded by the [[Locate plugin]].


== The Function ==
{{User:Instructor/Headers/Template}}
<highlight-nsis>/*
 
== Function Description ==
 
<highlight-nsis>
____________________________________________________________________________
____________________________________________________________________________


                             GetSize v1.8
                             GetSize v1.9
____________________________________________________________________________
____________________________________________________________________________


2004 Shengalts Aleksander (Shengalts@mail.ru)
Thanks KiCHiK (Function "FindFiles")


Thanks KiCHiK (Function "FindFiles")


Features:
Features:
1. Find the size of a file, files mask or directory
1. Find the size of a file, files mask or directory.
2. Find the sum of the files, directories and subdirectories
2. Find the sum of the files, directories and subdirectories.
 


Syntax:
Syntax:
${GetSize} "[Path]" "[Options]" $var1 $var2 $var3


Push "[Path]"      ; [Path]
"[Path]"      ; Disk or Directory
                  ;    Disk or Directory
              ;
Push "[Options]"  ; /M=[mask]
"[Options]"  ; /M=[mask]
                  ;    /M=*.*        - Find all (default)
              ;    /M=*.*        - Find all (default)
                  ;    /M=*.doc      - Find Work.doc, 1.doc ...
              ;    /M=*.doc      - Find Work.doc, 1.doc ...
                  ;    /M=Pho*        - Find PHOTOS, phone.txt ...
              ;    /M=Pho*        - Find PHOTOS, phone.txt ...
                  ;    /M=win???.exe  - Find winamp.exe, winver.exe ...
              ;    /M=win???.exe  - Find winamp.exe, winver.exe ...
                  ;    /M=winamp.exe  - Find winamp.exe only
              ;    /M=winamp.exe  - Find winamp.exe only
                  ; /S=No:No[B|K|M|G]
              ; /S=No:No[B|K|M|G]
                  ;    /S=      - Don't find file size (faster) (default)
              ;    /S=      - Don't find file size (faster) (default)
                  ;    /S=0:0B  - Find only files of 0 Bytes exactly
              ;    /S=0:0B  - Find only files of 0 Bytes exactly
                  ;    /S=5:9K  - Find only files of 5 to 9 Kilobytes
              ;    /S=5:9K  - Find only files of 5 to 9 Kilobytes
                  ;    /S=:10M  - Find only files of 10 Megabyte or less
              ;    /S=:10M  - Find only files of 10 Megabyte or less
                  ;    /S=1G    - Find only files of 1 Gigabyte or more
              ;    /S=1G    - Find only files of 1 Gigabyte or more
                  ; /G=[1|0]
              ; /G=[1|0]
                  ;    /G=1    - Find with subdirectories (default)
              ;    /G=1    - Find with subdirectories (default)
                  ;    /G=0    - Find without subdirectories
              ;    /G=0    - Find without subdirectories
                  ;
              ;
Call GetSize      ;
$var1         ; Result1: Size
                  ;
$var2         ; Result2: Number of files
Pop $var1         ;   Size
$var3         ; Result3: Number of directories
Pop $var2         ;   Sum of files
Pop $var3         ;   Sum of directories




Line 54: Line 50:
-Error flag if disk or directory isn't exist
-Error flag if disk or directory isn't exist
-Error flag if syntax error
-Error flag if syntax error
</highlight-nsis>






Example (1):
<b>Example (1):</b>
Section
<highlight-nsis>Section
; Find file size "C:\WINDOWS\Explorer.exe" in kilobytes
; Find file size "C:\WINDOWS\Explorer.exe" in kilobytes


Push "C:\WINDOWS"
${GetSize} "C:\WINDOWS" "/M=Explorer.exe /S=0K /G=0" $0 $1 $2
Push "/M=Explorer.exe /S=0K /G=0"
; $0="220" Kb
Call GetSize
; $1="1"  files
Pop $0  ; $0="220" Kb
; $2=""    directories
Pop $1  ; $1="1"  files
Pop $2  ; $2=""    directories


IfErrors 0 +2
IfErrors 0 +2
MessageBox MB_OK "Error"
MessageBox MB_OK "Error"
SectionEnd
SectionEnd
</highlight-nsis>


Example (2):
<b>Example (2):</b>
Section
<highlight-nsis>Section
; Find folder size "C:\Installs\Reanimator\Drivers" in megabytes
; Find folder size "C:\Installs\Reanimator\Drivers" in megabytes


Push "C:\Installs\Reanimator\Drivers"
${GetSize} "C:\Installs\Reanimator\Drivers" "/S=0M" $0 $1 $2
Push "/S=0M"
; $0="132" Mb
Call GetSize
; $1="555" files
Pop $0  ; $0="132" Mb
; $2="55"  directories
Pop $1  ; $1="555" files
Pop $2  ; $2="55"  directories


IfErrors 0 +2
IfErrors 0 +2
MessageBox MB_OK "Error"
MessageBox MB_OK "Error"
SectionEnd
SectionEnd
</highlight-nsis>


Example (3):
<b>Example (3):</b>
Section
<highlight-nsis>Section
; Find sum of files and folders "C:\WINDOWS" (no subfolders)
; Find sum of files and folders "C:\WINDOWS" (no subfolders)


Push "C:\WINDOWS"
${GetSize} "C:\WINDOWS" "/G=0" $0 $1 $2
Push "/G=0"
; $0=""    size
Call GetSize
; $1="253" files
Pop $0  ; $0=""    size
; $2="46"  directories
Pop $1  ; $1="253" files
Pop $2  ; $2="46"  directories


IfErrors 0 +2
IfErrors 0 +2
MessageBox MB_OK "Error"
MessageBox MB_OK "Error"
SectionEnd*/
SectionEnd
</highlight-nsis>


== Function Code ==


;---------------------------------------------------------------------------
<highlight-nsis>
Function GetSize
Function GetSize
!define GetSize `!insertmacro GetSizeCall`
!macro GetSizeCall _PATH _OPTIONS _RESULT1 _RESULT2 _RESULT3
Push `${_PATH}`
Push `${_OPTIONS}`
Call GetSize
Pop ${_RESULT1}
Pop ${_RESULT2}
Pop ${_RESULT3}
!macroend
Exch $1
Exch $1
Exch
Exch
Line 126: Line 132:
ClearErrors
ClearErrors


StrCpy $R9 $0 '' -1
StrCpy $R9 $0 1 -1
StrCmp $R9 '\' 0 +3
StrCmp $R9 '\' 0 +3
StrCpy $0 $0 -1
StrCpy $0 $0 -1
Line 148: Line 154:
StrCmp $R9 '/' 0 -4
StrCmp $R9 '/' 0 -4


StrCpy $9 0
StrCpy $9 -1
IntOp $9 $9 + 1
StrCpy $R9 $1 1 $9
StrCpy $R9 $1 1 $9
StrCmp $R9 '' +4
StrCmp $R9 '' +2
StrCmp $R9 '/' +3
StrCmp $R9 '/' 0 -3
IntOp $9 $9 + 1
goto -4
StrCpy $8 $1 $9
StrCpy $8 $1 $9
StrCpy $8 $8 '' 2
StrCpy $8 $8 '' 2
Line 253: Line 258:
IntCmp $R6 $6 0 0 findnext
IntCmp $R6 $6 0 0 findnext
IntOp $R4 $R4 + 1
IntOp $R4 $R4 + 1
System::Int64Op $R3 + $R6
System::Int64Op /NOUNLOAD $R3 + $R6
Pop $R3
Pop $R3


Line 263: Line 268:
show:
show:
StrCmp $5$6 '' nosize
StrCmp $5$6 '' nosize
System::Int64Op $R3 / $1
System::Int64Op /NOUNLOAD $R3 / $1
Pop $9
Pop $9
DetailPrint 'Size:$9 $2  Files:$R4  Folders:$R5'
DetailPrint 'Size:$9 $2  Files:$R4  Folders:$R5'
Line 327: Line 332:
</highlight-nsis>
</highlight-nsis>


[[{{ns:14}}:Disk, Path & File Functions]]
[[Category:Disk, Path & File Functions]]

Latest revision as of 12:58, 17 July 2020

Author: Instructor (talk, contrib)


Function "GetSize" was superseded by the Locate plugin.

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

____________________________________________________________________________
 
                            GetSize v1.9
____________________________________________________________________________
 
Thanks KiCHiK (Function "FindFiles")
 
 
Features:
1. Find the size of a file, files mask or directory.
2. Find the sum of the files, directories and subdirectories.
 
 
Syntax:
${GetSize} "[Path]" "[Options]" $var1 $var2 $var3
 
"[Path]"      ; Disk or Directory
              ;
"[Options]"   ; /M=[mask]
              ;     /M=*.*         - Find all (default)
              ;     /M=*.doc       - Find Work.doc, 1.doc ...
              ;     /M=Pho*        - Find PHOTOS, phone.txt ...
              ;     /M=win???.exe  - Find winamp.exe, winver.exe ...
              ;     /M=winamp.exe  - Find winamp.exe only
              ; /S=No:No[B|K|M|G]
              ;     /S=      - Don't find file size (faster) (default)
              ;     /S=0:0B  - Find only files of 0 Bytes exactly
              ;     /S=5:9K  - Find only files of 5 to 9 Kilobytes
              ;     /S=:10M  - Find only files of 10 Megabyte or less
              ;     /S=1G    - Find only files of 1 Gigabyte or more
              ; /G=[1|0]
              ;     /G=1     - Find with subdirectories (default)
              ;     /G=0     - Find without subdirectories
              ;
$var1         ; Result1: Size
$var2         ; Result2: Number of files
$var3         ; Result3: Number of directories
 
 
Note:
-Error flag if disk or directory isn't exist
-Error flag if syntax error


Example (1):

Section
	; Find file size "C:\WINDOWS\Explorer.exe" in kilobytes
 
	${GetSize} "C:\WINDOWS" "/M=Explorer.exe /S=0K /G=0" $0 $1 $2
	; $0="220" Kb
	; $1="1"   files
	; $2=""    directories
 
	IfErrors 0 +2
	MessageBox MB_OK "Error"
SectionEnd

Example (2):

Section
	; Find folder size "C:\Installs\Reanimator\Drivers" in megabytes
 
	${GetSize} "C:\Installs\Reanimator\Drivers" "/S=0M" $0 $1 $2
	; $0="132" Mb
	; $1="555" files
	; $2="55"  directories
 
	IfErrors 0 +2
	MessageBox MB_OK "Error"
SectionEnd

Example (3):

Section
	; Find sum of files and folders "C:\WINDOWS" (no subfolders)
 
	${GetSize} "C:\WINDOWS" "/G=0" $0 $1 $2
	; $0=""    size
	; $1="253" files
	; $2="46"  directories
 
	IfErrors 0 +2
	MessageBox MB_OK "Error"
SectionEnd

Function Code

Function GetSize
	!define GetSize `!insertmacro GetSizeCall`
 
	!macro GetSizeCall _PATH _OPTIONS _RESULT1 _RESULT2 _RESULT3
		Push `${_PATH}`
		Push `${_OPTIONS}`
		Call GetSize
		Pop ${_RESULT1}
		Pop ${_RESULT2}
		Pop ${_RESULT3}
	!macroend
 
	Exch $1
	Exch
	Exch $0
	Exch
	Push $2
	Push $3
	Push $4
	Push $5
	Push $6
	Push $7
	Push $8
	Push $9
	Push $R3
	Push $R4
	Push $R5
	Push $R6
	Push $R7
	Push $R8
	Push $R9
	ClearErrors
 
	StrCpy $R9 $0 1 -1
	StrCmp $R9 '\' 0 +3
	StrCpy $0 $0 -1
	goto -3
	IfFileExists '$0\*.*' 0 error
 
	StrCpy $3 ''
	StrCpy $4 ''
	StrCpy $5 ''
	StrCpy $6 ''
	StrCpy $8 0
	StrCpy $R3 ''
	StrCpy $R4 ''
	StrCpy $R5 ''
 
	option:
	StrCpy $R9 $1 1
	StrCpy $1 $1 '' 1
	StrCmp $R9 ' ' -2
	StrCmp $R9 '' sizeset
	StrCmp $R9 '/' 0 -4
 
	StrCpy $9 -1
	IntOp $9 $9 + 1
	StrCpy $R9 $1 1 $9
	StrCmp $R9 '' +2
	StrCmp $R9 '/' 0 -3
	StrCpy $8 $1 $9
	StrCpy $8 $8 '' 2
	StrCpy $R9 $8 '' -1
	StrCmp $R9 ' ' 0 +3
	StrCpy $8 $8 -1
	goto -3
	StrCpy $R9 $1 2
	StrCpy $1 $1 '' $9
 
	StrCmp $R9 'M=' 0 size
	StrCpy $4 $8
	goto option
 
	size:
	StrCmp $R9 'S=' 0 gotosubdir
	StrCpy $6 $8
	goto option
 
	gotosubdir:
	StrCmp $R9 'G=' 0 error
	StrCpy $7 $8
	StrCmp $7 '' +3
	StrCmp $7 '1' +2
	StrCmp $7 '0' 0 error
	goto option
 
	sizeset:
	StrCmp $6 '' default
	StrCpy $9 0
	StrCpy $R9 $6 1 $9
	StrCmp $R9 '' +4
	StrCmp $R9 ':' +3
	IntOp $9 $9 + 1
	goto -4
	StrCpy $5 $6 $9
	IntOp $9 $9 + 1
	StrCpy $1 $6 1 -1
	StrCpy $6 $6 -1 $9
	StrCmp $5 '' +2
	IntOp $5 $5 + 0
	StrCmp $6 '' +2
	IntOp $6 $6 + 0
 
	StrCmp $1 'B' 0 +4
	StrCpy $1 1
	StrCpy $2 bytes
	goto default
	StrCmp $1 'K' 0 +4
	StrCpy $1 1024
	StrCpy $2 Kb
	goto default
	StrCmp $1 'M' 0 +4
	StrCpy $1 1048576
	StrCpy $2 Mb
	goto default
	StrCmp $1 'G' 0 error
	StrCpy $1 1073741824
	StrCpy $2 Gb
 
	default:
	StrCmp $4 '' 0 +2
	StrCpy $4 '*.*'
	StrCmp $7 '' 0 +2
	StrCpy $7 '1'
 
	StrCpy $8 1
	Push $0
	SetDetailsPrint textonly
 
	nextdir:
	IntOp $8 $8 - 1
	Pop $R8
	FindFirst $0 $R7 '$R8\$4'
	IfErrors show
	StrCmp $R7 '.' 0 +5
	FindNext $0 $R7
	StrCmp $R7 '..' 0 +3
	FindNext $0 $R7
	IfErrors show
 
	dir:
	IfFileExists '$R8\$R7\*.*' 0 file
	IntOp $R5 $R5 + 1
	goto findnext
 
	file:
	StrCpy $R6 0
	StrCmp $5$6 '' 0 +3
	IntOp $R4 $R4 + 1
	goto findnext
	FileOpen $9 '$R8\$R7' r
	IfErrors +3
	FileSeek $9 0 END $R6
	FileClose $9
	StrCmp $5 '' +2
	IntCmp $R6 $5 0 findnext
	StrCmp $6 '' +2
	IntCmp $R6 $6 0 0 findnext
	IntOp $R4 $R4 + 1
	System::Int64Op /NOUNLOAD $R3 + $R6
	Pop $R3
 
	findnext:
	FindNext $0 $R7
	IfErrors 0 dir
	FindClose $0
 
	show:
	StrCmp $5$6 '' nosize
	System::Int64Op /NOUNLOAD $R3 / $1
	Pop $9
	DetailPrint 'Size:$9 $2  Files:$R4  Folders:$R5'
	goto subdir
	nosize:
	DetailPrint 'Files:$R4  Folders:$R5'
 
	subdir:
	StrCmp $7 0 preend
	FindFirst $0 $R7 '$R8\*.*'
	StrCmp $R7 '.' 0 +5
	FindNext $0 $R7
	StrCmp $R7 '..' 0 +3
	FindNext $0 $R7
	IfErrors +7
 
	IfFileExists '$R8\$R7\*.*' 0 +3
	Push '$R8\$R7'
	IntOp $8 $8 + 1
	FindNext $0 $R7
	IfErrors 0 -4
	FindClose $0
	StrCmp $8 0 0 nextdir
 
	preend:
	StrCmp $R3 '' nosizeend
	System::Int64Op $R3 / $1
	Pop $R3
	nosizeend:
	StrCpy $2 $R4
	StrCpy $1 $R5
	StrCpy $0 $R3
	goto end
 
	error:
	SetErrors
	StrCpy $0 ''
	StrCpy $1 ''
	StrCpy $2 ''
 
	end:
	SetDetailsPrint both
	Pop $R9
	Pop $R8
	Pop $R7
	Pop $R6
	Pop $R5
	Pop $R4
	Pop $R3
	Pop $9
	Pop $8
	Pop $7
	Pop $6
	Pop $5
	Pop $4
	Pop $3
	Exch $2
	Exch
	Exch $1
	Exch 2
	Exch $0
FunctionEnd