GetSize: Difference between revisions
From NSIS Wiki
Jump to navigationJump to search
m (Updated author links.) |
m (Added category links.) |
||
Line 326: | Line 326: | ||
FunctionEnd | FunctionEnd | ||
</highlight-nsis> | </highlight-nsis> | ||
[[{{ns:14}}:Disk, Path & File Functions]] |
Revision as of 22:01, 30 April 2005
Author: Instructor (talk, contrib) |
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
The Function
/* ____________________________________________________________________________ GetSize v1.8 ____________________________________________________________________________ 2004 Shengalts Aleksander (Shengalts@mail.ru) 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: Push "[Path]" ; [Path] ; Disk or Directory Push "[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 ; Call GetSize ; ; Pop $var1 ; Size Pop $var2 ; Sum of files Pop $var3 ; Sum 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 Push "C:\WINDOWS" Push "/M=Explorer.exe /S=0K /G=0" Call GetSize Pop $0 ; $0="220" Kb Pop $1 ; $1="1" files Pop $2 ; $2="" directories IfErrors 0 +2 MessageBox MB_OK "Error" SectionEnd Example (2): Section ; Find folder size "C:\Installs\Reanimator\Drivers" in megabytes Push "C:\Installs\Reanimator\Drivers" Push "/S=0M" Call GetSize Pop $0 ; $0="132" Mb Pop $1 ; $1="555" files Pop $2 ; $2="55" directories IfErrors 0 +2 MessageBox MB_OK "Error" SectionEnd Example (3): Section ; Find sum of files and folders "C:\WINDOWS" (no subfolders) Push "C:\WINDOWS" Push "/G=0" Call GetSize Pop $0 ; $0="" size Pop $1 ; $1="253" files Pop $2 ; $2="46" directories IfErrors 0 +2 MessageBox MB_OK "Error" SectionEnd*/ ;--------------------------------------------------------------------------- Function GetSize 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 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 0 StrCpy $R9 $1 1 $9 StrCmp $R9 '' +4 StrCmp $R9 '/' +3 IntOp $9 $9 + 1 goto -4 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 $R3 + $R6 Pop $R3 findnext: FindNext $0 $R7 IfErrors 0 dir FindClose $0 show: StrCmp $5$6 '' nosize System::Int64Op $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