FileJoin: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (Updated author and download links, and changed format of some pages.)
No edit summary
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
== Links ==
{{PageAuthor|Instructor}}
; 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
{{User:Instructor/Headers/Template}}


== The Function ==
== Function Description ==
<highlight-nsis>/* ____________________________________________________________________________
<highlight-nsis>
____________________________________________________________________________


                             DriveSpace v1.1
                             FileJoin v1.4
____________________________________________________________________________
____________________________________________________________________________


Thanks sunjammer (Function "CheckSpaceFree")
Thanks Afrow UK (Based on his idea of Function "JoinFiles")




Get total, occupied or free space of the drive.
Join two files in one (File1 + File2 = File3).




Syntax:
Syntax:
${GetSize} "[Drive]" "[Options]" $var
${FileJoin} "[File1]" "[File2]" "[File3]"


"[Drive]"    ; Disk to check
"[File1]"    ; Input File1
              ;   
"[File2]"     ; Input File2
"[Options]"   ; /D=[T|O|F]
"[File3]"     ; Output File3
              ;     /D=T  - Total space (default)
               ;  If [File3]="" Then add [File2] to [File1]
              ;     /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 input files aren't exists
-Error flag if output file path isn't exists
</highlight-nsis>


Note:
-Error flag if disk isn't exist or not ready
-Error flag if syntax error




<b>Example1 (Join:  a.log + b.log = Z.log):</b>
<highlight-nsis>Section
${FileJoin} "C:\a.log" "C:\logs\b.log" "C:\Z.log"
SectionEnd
</highlight-nsis>


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


<b>Example2 (Add:  a.log + b.log = a.log):</b>
<highlight-nsis>Section
${FileJoin} "C:\a.log" "C:\logs\b.log" "C:\a.log"
SectionEnd
</highlight-nsis>


;---------------------------------------------------------------------------
== Function Code ==


Function DriveSpace
<highlight-nsis>
!define DriveSpace `!insertmacro DriveSpaceCall`
Function FileJoin
!define FileJoin `!insertmacro FileJoinCall`


!macro DriveSpaceCall _DRIVE _OPTIONS _RESULT
!macro FileJoinCall _FILE1 _FILE2 _FILE3
Push `${_DRIVE}`
Push `${_FILE1}`
Push `${_OPTIONS}`
Push `${_FILE2}`
Call DriveSpace
Push `${_FILE3}`
Pop ${_RESULT}
Call FileJoin
!macroend
!macroend


Exch $2
Exch
Exch $1
Exch $1
Exch
Exch
Exch 2
Exch $0
Exch $0
Exch
Exch 2
Push $2
Push $3
Push $3
Push $4
Push $4
Push $5
Push $5
Push $6
ClearErrors
ClearErrors


StrCpy $2 $0 1 -1
IfFileExists $0 0 error
StrCmp $2 '\' 0 +3
IfFileExists $1 0 error
StrCpy $0 $0 -1
StrCpy $3 0
goto -3
IntOp $3 $3 - 1
IfFileExists '$0\NUL' 0 error
StrCpy $4 $2 1 $3
StrCmp $4 \ +2
StrCmp $4 '' +3 -3
StrCpy $4 $2 $3
IfFileExists '$4\*.*' 0 error
 
StrCmp $2 $0 0 +2
StrCpy $2 ''
StrCmp $2 '' 0 +3
StrCpy $4 $0
goto +3
GetTempFileName $4
CopyFiles /SILENT $0 $4
FileOpen $3 $4 a
IfErrors error
FileSeek $3 -1 END
FileRead $3 $5
StrCmp $5 '$\r' +3
StrCmp $5 '$' +2
FileWrite $3 '$\r$'


StrCpy $5 ''
;FileWrite $3 '$\r$--Divider--$\r$'
StrCpy $6 ''


option:
FileOpen $0 $1 r
StrCpy $2 $1 1
IfErrors error
StrCpy $1 $1 '' 1
FileRead $0 $5
StrCmp $2 ' ' -2
IfErrors +3
StrCmp $2 '' default
FileWrite $3 $5
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
goto -3
StrCpy $2 $1 2
FileClose $0
StrCpy $1 $1 '' $3
FileClose $3
 
StrCmp $2 '' end
StrCmp $2 'D=' 0 unit
Delete '$EXEDIR\$2'
StrCpy $5 $4
Rename $4 '$EXEDIR\$2'
StrCmp $5 '' +4
IfErrors 0 end
StrCmp $5 'T' +3
Delete $2
StrCmp $5 'O' +2
Rename $4 $2
StrCmp $5 'F' 0 error
IfErrors 0 end
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 '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 $3 - $2
Pop $0
goto getsize
StrCmp $5 F 0 +2
StrCpy $0 $2
 
getsize:
System::Int64Op $0 / $6
Pop $0
goto end


error:
error:
SetErrors
SetErrors
StrCpy $0 ''


end:
end:
Pop $6
Pop $5
Pop $5
Pop $4
Pop $4
Line 161: Line 121:
Pop $2
Pop $2
Pop $1
Pop $1
Exch $0
Pop $0
FunctionEnd
FunctionEnd
</highlight-nsis>
</highlight-nsis>


Page author: [[User:Instructor|Instructor]]
[[Category:Text Files Manipulation Functions]]

Latest revision as of 12:00, 30 November 2005

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

____________________________________________________________________________
 
                            FileJoin v1.4
____________________________________________________________________________
 
Thanks Afrow UK (Based on his idea of Function "JoinFiles")
 
 
Join two files in one (File1 + File2 = File3).
 
 
Syntax:
${FileJoin} "[File1]" "[File2]" "[File3]"
 
"[File1]"     ; Input File1
"[File2]"     ; Input File2
"[File3]"     ; Output File3
              ;  If [File3]="" Then add [File2] to [File1]
 
Note:
-Error flag if input files aren't exists
-Error flag if output file path isn't exists


Example1 (Join: a.log + b.log = Z.log):

Section
	${FileJoin} "C:\a.log" "C:\logs\b.log" "C:\Z.log"
SectionEnd


Example2 (Add: a.log + b.log = a.log):

Section
	${FileJoin} "C:\a.log" "C:\logs\b.log" "C:\a.log"
SectionEnd

Function Code

Function FileJoin
	!define FileJoin `!insertmacro FileJoinCall`
 
	!macro FileJoinCall _FILE1 _FILE2 _FILE3
		Push `${_FILE1}`
		Push `${_FILE2}`
		Push `${_FILE3}`
		Call FileJoin
	!macroend
 
	Exch $2
	Exch
	Exch $1
	Exch
	Exch 2
	Exch $0
	Exch 2
	Push $3
	Push $4
	Push $5
	ClearErrors
 
	IfFileExists $0 0 error
	IfFileExists $1 0 error
	StrCpy $3 0
	IntOp $3 $3 - 1
	StrCpy $4 $2 1 $3
	StrCmp $4 \ +2
	StrCmp $4 '' +3 -3
	StrCpy $4 $2 $3
	IfFileExists '$4\*.*' 0 error
 
	StrCmp $2 $0 0 +2
	StrCpy $2 ''
	StrCmp $2 '' 0 +3
	StrCpy $4 $0
	goto +3
	GetTempFileName $4
	CopyFiles /SILENT $0 $4
	FileOpen $3 $4 a
	IfErrors error
	FileSeek $3 -1 END
	FileRead $3 $5
	StrCmp $5 '$\r' +3
	StrCmp $5 '$' +2
	FileWrite $3 '$\r$'
 
	;FileWrite $3 '$\r$--Divider--$\r$'
 
	FileOpen $0 $1 r
	IfErrors error
	FileRead $0 $5
	IfErrors +3
	FileWrite $3 $5
	goto -3
	FileClose $0
	FileClose $3
	StrCmp $2 '' end
	Delete '$EXEDIR\$2'
	Rename $4 '$EXEDIR\$2'
	IfErrors 0 end
	Delete $2
	Rename $4 $2
	IfErrors 0 end
 
	error:
	SetErrors
 
	end:
	Pop $5
	Pop $4
	Pop $3
	Pop $2
	Pop $1
	Pop $0
FunctionEnd