Text Functions Header

Nullsoft Scriptable Install System

Previous | Contents | Next

E.2 Text Functions Header

E.2.1 Introduction

Include header:

!include "TextFunc.nsh"

Call functions:

Section Install
	${LineRead} "C:\a.log" "-1" $R0
	; $R0="Last line$\r$\n"
SectionEnd
Section un.Install
	${TrimNewLines} "Last line$\r$\n" $R0
	; $R0="Last line"
SectionEnd

E.2.2 LineFind

  • Find specified lines in text file, and edit or view these lines in callback function.

Syntax:

${LineFind} "[File1]" "[File2|/NUL]" "[LineNumbers]" "Function"
"[File1]"         ; Input text file
                  ;
"[File2|/NUL]"    ; [File2]
                  ;   Output text file
                  ;   If empty then File2=File1
                  ; [/NUL]
                  ;   No output text file (only read File1)
                  ;
"[LineNumbers]"   ; [No|-No|No:No|{No}|{-No}|{No:No}]
                  ;   1:-1     all lines to change (default)
                  ;   2        second line from start
                  ;   -3       third line from end
                  ;   5:9      range of lines from 5 to 9
                  ;   {2}      only second line from start to output
                  ;   {-3}     only third line from end to output
                  ;   {5:9}    only range of lines from 5 to 9 to output
                  ;
"Function"        ; Callback function for specified lines

Function "Function"
	; $R9       current line
	; $R8       current line number
	; $R7       current line negative number
	; $R6       current range of lines
	; $R5       handle of a file opened to read
	; $R4       handle of a file opened to write ($R4="" if "/NUL")

	; you can use any string functions
	; $R0-$R3  are not used (save data in them).
	; ...

	Push $var      ; If $var="StopLineFind"  Then exit from function
	               ; If $var="SkipWrite"     Then skip current line (ignored if "/NUL")
FunctionEnd

Note: - Error flag if input file doesn't exist
- Error flag if output file path doesn't exist
- Ranges must be specified on growth (2 4:5 9:-8 -5:-4 -2:-1)
- Output file will not be updated if no changes made.

Example1 (delete first two symbols):

Section
	${LineFind} "C:\a.log" "C:\a-edited.log" "3:-1" "Example1"
	IfErrors 0 +2
	MessageBox MB_OK "Error"
SectionEnd

Function Example1
	${TrimNewLines} '$R9' $R9
	StrCpy $R9 $R9 '' 2
	StrCpy $R9 '$R9$\r$\n'
	;start from 3 line and delete first two symbols

	Push $0
FunctionEnd

Example2 (show changed lines):

Section
	${LineFind} "C:\a.log" "a.log" "{5:12 15 -6:-5 -1}" "Example2"
	IfErrors 0 +2
	MessageBox MB_OK "Error"
SectionEnd

Function Example2
	${TrimNewLines} '$R9' $R9
	StrCpy $R9 "$R9   ~Changed line ($R8)~$\r$\n"

	Push $0
FunctionEnd

Example3 (delete lines):

Section
	${LineFind} "C:\a.log" "\logs\a.log" "2:3 10:-5 -3:-2" "Example3"
	IfErrors 0 +2
	MessageBox MB_OK "Error"
SectionEnd

Function Example3
	StrCpy $0 SkipWrite

	Push $0
FunctionEnd

Example4 (insert lines):

Section
	${LineFind} "C:\a.log" "" "10" "Example4
	IfErrors 0 +2
	MessageBox MB_OK "Error"
SectionEnd

Function Example4
	FileWrite $R4 "---First Line---$\r$\n"
	FileWrite $R4 "---Second Line ...---$\r$\n"

	Push $0
FunctionEnd

Example5 (replace in file with count of changes - "WordFunc.nsh" required):

!include "WordFunc.nsh"

Section
	StrCpy $R0 0
	${LineFind} "C:\a.log" "C:\logs\a.log" "1:-1" "Example5"
	IfErrors 0 +2
	MessageBox MB_OK "Error" IDOK +2
	MessageBox MB_OK "Changed lines=$R0"
SectionEnd

Function Example5
	StrCpy $1 $R9

	${WordReplace} '$R9' ' ' '_' '+*' $R9

	StrCmp $1 $R9 +2
	IntOp $R0 $R0 + 1
	;$R0   count of changed lines

	Push $0
FunctionEnd

Example6 (line string to cut or delete):

Section
	${LineFind} "\a.log" "C:\logs\a.log" "" "Example6"
	IfErrors 0 +2
	MessageBox MB_OK "Error" IDOK +2
	MessageBox MB_OK "Processed lines=$R1:$R2"
SectionEnd

Function Example6
	;(Cut lines from a line to another line (also including that line))
	StrCmp $R0 finish stop
	StrCmp $R0 start finish
	StrCmp $R9 'Start Line$\r$\n' 0 skip
	StrCpy $R0 start
	StrCpy $R1 $R8
	goto code
	finish:
	StrCmp $R9 'Finish Line$\r$\n' 0 code
	StrCpy $R0 finish
	StrCpy $R2 $R8
	goto code
	skip:
	StrCpy $0 SkipWrite
	goto output
	stop:
	StrCpy $0 StopLineFind
	goto output

	;;(Delete lines from a line to another line (also including that line))
	; StrCmp $R0 finish code
	; StrCmp $R0 start finish
	; StrCmp $R9 'Start Line$\r$\n' 0 code
	; StrCpy $R0 start
	; StrCpy $R1 $R8
	; goto skip
	; finish:
	; StrCmp $R9 'Finish Line$\r$\n' 0 skip
	; StrCpy $R0 finish
	; StrCpy $R2 $R8
	; skip:
	; StrCpy $0 SkipWrite
	; goto output

	code:
	;...

	output:
	Push $0
FunctionEnd

Example7 (read lines):

Section
	${LineFind} "C:\a.log" "/NUL" "1:-1" "Example7"
	IfErrors 0 +2
	MessageBox MB_OK "Error"
SectionEnd

Function Example7
	MessageBox MB_OKCANCEL '$$R9  "Line"=[$R9]$\n$$R8     "#" =[$R8]' IDOK +2
	StrCpy $0 StopLineFind

	Push $0
FunctionEnd

E.2.3 LineRead

  • Get line in file specified with number.

Syntax:

${LineRead} "[File]" "[LineNumber]" $var
"[File]"         ; Input text file
                 ;
"[LineNumber]"   ; [No|-No]
                 ;   3    line number from start
                 ;   -5   line number from end
                 ;
$var             ; Result: Line

Note:
- Error flag if input file doesn't exist
- Error flag if line number not found

Example:

Section
	${LineRead} "C:\a.log" "-1" $R0
	; $R0="Last line$\r$\n"
SectionEnd

E.2.4 FileReadFromEnd

  • Read text file from end line by line.

Syntax:

${FileReadFromEnd} "[File]" "Function"
"[File]"      ; Input text file
"Function"    ; Callback function

Function "Function"
	; $9       current line
	; $8       current line number
	; $7       current line negative number

	; $R0-$R9  are not used (save data in them).
	; ...

	Push $var      ; If $var="StopFileReadFromEnd"  Then exit from function
FunctionEnd

Note:
- Error flag if input file doesn't exist

Example1:

Section
	${FileReadFromEnd} "C:\a.log" "Example1"

	IfErrors 0 +2
	MessageBox MB_OK "Error"
SectionEnd

Function Example1
	MessageBox MB_OKCANCEL '"Line"=[$9]$\n   "#"=[$8]$\n  "-#"=[$7]' IDOK +2
	StrCpy $0 StopFileReadFromEnd

	Push $0
FunctionEnd

Example2 (Reverse text file):

Section
	GetTempFileName $R0
	FileOpen $R1 $R0 w
	${FileReadFromEnd} "C:\a.log" "Example2"
	FileClose $R1

	IfErrors 0 +2
	MessageBox MB_OK "Error" IDOK +2
	Exec '"notepad.exe" "$R0"'
SectionEnd

Function Example2
	StrCmp $7 -1 0 +5
	StrCpy $1 $9 1 -1
	StrCmp $1 '$\n' +3
	StrCmp $1 '$\r' +2
	StrCpy $9 '$9$\r$\n'

	FileWrite $R1 "$9"

	Push $0
FunctionEnd

E.2.5 LineSum

  • Get sum of lines in text file.

Syntax:

${LineSum} "[File]" $var
"[File]"      ; Input file
$var          ; Result: Sum of lines

Note:
- Error flag if input file doesn't exist

Example:

Section
	${LineSum} "C:\a.log" $R0
	; $R0="54"
SectionEnd

E.2.6 FileJoin

  • 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 don't exist
- Error flag if output file path doesn't exist

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

E.2.7 TextCompare

  • Compare two text files.

Syntax:

${TextCompare} "[File1]" "[File2]" "[Option]" "Function"
"[File1]"     ; File1      Compare these lines
"[File2]"     ; File2      Compare with these lines
"[Options]"   ; (line-by-line):
              ; FastDiff   Compare line N (File1) with line N (File2)
              ;            Call function if Different lines found
              ; FastEqual  Compare line N (File1) with line N (File2)
              ;            Call function if Equal lines found
              ; (line number independent):
              ; SlowDiff   Compare line N (File1) with all lines (File2)
              ;            Call function if line N (File1) Different
              ; SlowEqual  Compare line N (File1) with all lines (File2)
              ;            Call function if line N (File1) Equal
"Function"    ; Callback function

Function "Function"
	; $9    "Line File1"
	; $8    "Line number"
	; $7    "Line File2"  (empty if SlowDiff)
	; $6    "Line number" (empty if SlowDiff)

	; $R0-$R9  are not used (save data in them).
	; ...

	Push $var    ; If $var="StopTextCompare"  Then exit from function
FunctionEnd

Note:
- Error flag if File1 or File2 doesn't exist
- Error flag if syntax error

Example (Different or Equal):

Section
	StrCpy $R0 ''
	${TextCompare} "C:\1.txt" "C:\2.txt" "FastDiff" "Example1"
	IfErrors 0 +2
	MessageBox MB_OK "Error" IDOK +4

	StrCmp $R0 NotEqual 0 +2
	MessageBox MB_OK "Files differ" IDOK +2
	MessageBox MB_OK "Files identical"
SectionEnd

Function Example1
	StrCpy $R0 NotEqual
	StrCpy $0 StopTextCompare

	Push $0
FunctionEnd

Example (Compare line-by-line - Different):

Section
	StrCpy $R0 'Text1.txt'
	StrCpy $R1 'Text2.txt'

	GetTempFileName $R2
	FileOpen $R3 $R2 w
	FileWrite $R3 "$R0 | $R1$\r$\n"
	${TextCompare} "$R0" "$R1" "FastDiff" "Example2"
	IfErrors 0 +2
	MessageBox MB_OK "Error" IDOK +2

	Exec "notepad.exe $R2"
FunctionEnd

Function Example2
	FileWrite $R3 '$8=$9'
	FileWrite $R3 '$6=$7$\r$\n'

	Push $0
FunctionEnd

Example (Compare line-by-line - Equal):

Section
	StrCpy $R0 'Text1.txt'
	StrCpy $R1 'Text2.txt'

	GetTempFileName $R2
	FileOpen $R3 $R2 w
	FileWrite $R3 "$R0 | $R1$\r$\n"
	${TextCompare} "$R0" "$R1" "FastEqual" "Example3"
	IfErrors 0 +2
	MessageBox MB_OK "Error" IDOK +2

	Exec "notepad.exe $R2"
FunctionEnd

Function Example3
	FileWrite $R3 '$8|$6=$9'

	Push $0
FunctionEnd

Example (Compare all lines - Different):

Section
	StrCpy $R0 'Text1.txt'
	StrCpy $R1 'Text2.txt'

	GetTempFileName $R2
	FileOpen $R3 $R2 w
	FileWrite $R3 "$R0 | $R1$\r$\n"
	${TextCompare} "$R0" "$R1" "SlowDiff" "Example4"
	IfErrors 0 +2
	MessageBox MB_OK "Error" IDOK end

	FileWrite $R3 "$\r$\n$R1 | $R0$\r$\n"
	${TextCompare} "$R1" "$R0" "SlowDiff" "Example4"
	FileClose $R3
	IfErrors 0 +2
	MessageBox MB_OK "Error" IDOK end

	Exec "notepad.exe $R2"

	end:
FunctionEnd

Function Example4
	FileWrite $R3 '$8=$9'

	Push $0
FunctionEnd

Example (Compare all lines - Equal):

Section
	StrCpy $R0 'Text1.txt'
	StrCpy $R1 'Text2.txt'

	GetTempFileName $R2
	FileOpen $R3 $R2 w
	FileWrite $R3 "$R0 | $R1$\r$\n"
	${TextCompare} "$R0" "$R1" "SlowEqual" "Example5"
	IfErrors 0 +2
	MessageBox MB_OK "Error" IDOK +2

	Exec "notepad.exe $R2"
FunctionEnd

Function Example5
	FileWrite $R3 '$8|$6=$9'

	Push $0
FunctionEnd

Example (Show variables):

Section
	${TextCompare} "C:\1.txt" "C:\2.txt" "FastDiff" "Example6"

	IfErrors 0 +2
	MessageBox MB_OK "Error"
SectionEnd

Function Example6
	MessageBox MB_OKCANCEL '\
		$$9    "Line File1" =[$9]$\n\
		$$8    "Line #"      =[$8]$\n\
		$$7    "Line File2" =[$7]$\n\
		$$6    "Line #"      =[$6]'\
		IDOK +2
	StrCpy $0 StopTextCompare

	Push $0
FunctionEnd

E.2.8 TextCompareS

E.2.9 ConfigRead

  • Read value from entry name in config file.

Syntax:

${ConfigRead} "[File]" "[Entry]" $var
"[File]"      ; config file
              ;
"[Entry]"     ; entry name
              ;
$var          ; Result:  Value

Note:
- Error flag if entry not found
- Error flag if file doesn't exist

Example1:

Section
	${ConfigRead} "C:\AUTOEXEC.BAT" "SET winbootdir=" $R0
	;$R0=C:\WINDOWS
SectionEnd

Example2:

Section
	${ConfigRead} "C:\apache\conf\httpd.conf" "Timeout " $R0
	;$R0=30
SectionEnd

E.2.10 ConfigReadS

E.2.11 ConfigWrite

  • Write value from entry name in config file.

Syntax:

${ConfigWrite} "[File]" "[Entry]" "[Value]" $var
"[File]"      ; config file
              ;
"[Entry]"     ; entry name
              ;
"[Value]"     ; value name
              ;  if "" then delete Entry
              ;
$var          ; Result:
              ;    $var=CHANGED  Value is written
              ;    $var=DELETED  Entry is deleted
              ;    $var=ADDED    Entry and Value are added
              ;    $var=SAME     Entry and Value already exist

Note:
- Error flag if file doesn't exist
- Error flag if file can't be opened

Example1:

Section
	${ConfigWrite} "C:\AUTOEXEC.BAT" "SET winbootdir=" "D:\WINDOWS" $R0
	;$R0=CHANGED
SectionEnd

Example2:

Section
	${ConfigWrite} "C:\apache\conf\httpd.conf" "Timeout " "30" $R0
	;$R0=SAME
SectionEnd

Example3:

Section
	${ConfigWrite} "C:\apache\conf\httpd.conf" "Timeout " "" $R0
	;$R0=DELETED
SectionEnd

E.2.12 ConfigWriteS

E.2.13 FileRecode

  • Recode text file from DOS to Windows format and vice-versa.

Syntax:

${FileRecode} "[File]" "[Format]"
"[File]"        ;
                ;
"[Format]"      ; OemToChar   -from DOS to Windows
                ; CharToOem   -from Windows to DOS

Note:
- Error flag if file doesn't exist
- Error flag if syntax error

Example:

Section
	${FileRecode} "C:\SCANDISK.LOG" "CharToOem"
SectionEnd

E.2.14 TrimNewLines

  • Trim newlines in a string.

Syntax:

${TrimNewLines} "[string]" $var
"[string]"    ; Input string
$var          ; Result: String without '$\r' and '$\n' at the end

Example:

Section
	${TrimNewLines} "Text line$\r$\n" $R0
	; $R0="Text line"
SectionEnd

Previous | Contents | Next