上一页 | 目录 | 下一页

E.2 文本函数头文件

E.2.1 介绍

包含头文件:

!include "TextFunc.nsh"

调用函数:

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

语法:

${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

注意:
- 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.

例子1 (删除前两个符号):

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

例子2 (显示更改的行):

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

例子3 (删除行):

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

例子4 (插入行):

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

例子5 (在文件内使用更改计数进行替换 - 需要 "WordFunc.nsh"):

!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

例子6 (行字符串的剪切或删除):

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

例子7 (读取行):

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

语法:

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

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

例子:

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

E.2.4 FileReadFromEnd

语法:

${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

注意:
- Error flag if input file doesn't exist

例子1:

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

例子2 (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

语法:

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

注意:
- Error flag if input file doesn't exist

例子:

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

E.2.6 FileJoin

语法:

${FileJoin} "[File1]" "[File2]" "[File3]"
"[File1]"     ; Input File1
"[File2]"     ; Input File2
"[File3]"     ; Output File3
              ;  If [File3]="" Then add [File2] to [File1]

注意:
- Error flag if input files don't exist
- Error flag if output file path doesn't exist

例子1 (联接: a.log + b.log = Z.log):

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

例子2 (添加: a.log + b.log = a.log):

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

E.2.7 TextCompare

语法:

${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

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

例子 (不同或相等):

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

例子 (逐行比较 - 不同):

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

例子 (逐行比较 - 相等):

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

例子 (比较所有行 - 不同):

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

例子 (比较所有行 - 相等):

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

例子 (显示变量):

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

语法:

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

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

例子1:

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

例子2:

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

E.2.10 ConfigReadS

E.2.11 ConfigWrite

语法:

${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

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

例子1:

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

例子2:

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

例子3:

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

E.2.12 ConfigWriteS

E.2.13 FileRecode

语法:

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

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

例子:

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

E.2.14 TrimNewLines

语法:

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

例子:

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

上一页 | 目录 | 下一页