E.2 Text Functions Header

E.2.1 Introduction

Include header:

  1. !include "TextFunc.nsh"

Call functions:

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

E.2.2 LineFind

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

Syntax:

  1. ${LineFind} "[File1]" "[File2|/NUL]" "[LineNumbers]" "Function"
  1. "[File1]" ; Input text file
  2. ;
  3. "[File2|/NUL]" ; [File2]
  4. ; Output text file
  5. ; If empty then File2=File1
  6. ; [/NUL]
  7. ; No output text file (only read File1)
  8. ;
  9. "[LineNumbers]" ; [No|-No|No:No|{No}|{-No}|{No:No}]
  10. ; 1:-1 all lines to change (default)
  11. ; 2 second line from start
  12. ; -3 third line from end
  13. ; 5:9 range of lines from 5 to 9
  14. ; {2} only second line from start to output
  15. ; {-3} only third line from end to output
  16. ; {5:9} only range of lines from 5 to 9 to output
  17. ;
  18. "Function" ; Callback function for specified lines
  19.  
  20. Function "Function"
  21. ; $R9 current line
  22. ; $R8 current line number
  23. ; $R7 current line negative number
  24. ; $R6 current range of lines
  25. ; $R5 handle of a file opened to read
  26. ; $R4 handle of a file opened to write ($R4="" if "/NUL")
  27.  
  28. ; you can use any string functions
  29. ; $R0-$R3 are not used (save data in them).
  30. ; ...
  31.  
  32. Push $var ; If $var="StopLineFind" Then exit from function
  33. ; If $var="SkipWrite" Then skip current line (ignored if "/NUL")
  34. 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):

  1. Section
  2. ${LineFind} "C:\a.log" "C:\a-edited.log" "3:-1" "Example1"
  3. IfErrors 0 +2
  4. MessageBox MB_OK "Error"
  5. SectionEnd
  6.  
  7. Function Example1
  8. ${TrimNewLines} '$R9' $R9
  9. StrCpy $R9 $R9 '' 2
  10. StrCpy $R9 '$R9$\r$\n'
  11. ;start from 3 line and delete first two symbols
  12.  
  13. Push $0
  14. FunctionEnd

Example2 (show changed lines):

  1. Section
  2. ${LineFind} "C:\a.log" "a.log" "{5:12 15 -6:-5 -1}" "Example2"
  3. IfErrors 0 +2
  4. MessageBox MB_OK "Error"
  5. SectionEnd
  6.  
  7. Function Example2
  8. ${TrimNewLines} '$R9' $R9
  9. StrCpy $R9 "$R9 ~Changed line ($R8)~$\r$\n"
  10.  
  11. Push $0
  12. FunctionEnd

Example3 (delete lines):

  1. Section
  2. ${LineFind} "C:\a.log" "\logs\a.log" "2:3 10:-5 -3:-2" "Example3"
  3. IfErrors 0 +2
  4. MessageBox MB_OK "Error"
  5. SectionEnd
  6.  
  7. Function Example3
  8. StrCpy $0 SkipWrite
  9.  
  10. Push $0
  11. FunctionEnd

Example4 (insert lines):

  1. Section
  2. ${LineFind} "C:\a.log" "" "10" "Example4
  3. IfErrors 0 +2
  4. MessageBox MB_OK "Error"
  5. SectionEnd
  6.  
  7. Function Example4
  8. FileWrite $R4 "---First Line---$\r$\n"
  9. FileWrite $R4 "---Second Line ...---$\r$\n"
  10.  
  11. Push $0
  12. FunctionEnd

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

  1. !include "WordFunc.nsh"
  2.  
  3. Section
  4. StrCpy $R0 0
  5. ${LineFind} "C:\a.log" "C:\logs\a.log" "1:-1" "Example5"
  6. IfErrors 0 +2
  7. MessageBox MB_OK "Error" IDOK +2
  8. MessageBox MB_OK "Changed lines=$R0"
  9. SectionEnd
  10.  
  11. Function Example5
  12. StrCpy $1 $R9
  13.  
  14. ${WordReplace} '$R9' ' ' '_' '+*' $R9
  15.  
  16. StrCmp $1 $R9 +2
  17. IntOp $R0 $R0 + 1
  18. ;$R0 count of changed lines
  19.  
  20. Push $0
  21. FunctionEnd

Example6 (line string to cut or delete):

  1. Section
  2. ${LineFind} "\a.log" "C:\logs\a.log" "" "Example6"
  3. IfErrors 0 +2
  4. MessageBox MB_OK "Error" IDOK +2
  5. MessageBox MB_OK "Processed lines=$R1:$R2"
  6. SectionEnd
  7.  
  8. Function Example6
  9. ;(Cut lines from a line to another line (also including that line))
  10. StrCmp $R0 finish stop
  11. StrCmp $R0 start finish
  12. StrCmp $R9 'Start Line$\r$\n' 0 skip
  13. StrCpy $R0 start
  14. StrCpy $R1 $R8
  15. goto code
  16. finish:
  17. StrCmp $R9 'Finish Line$\r$\n' 0 code
  18. StrCpy $R0 finish
  19. StrCpy $R2 $R8
  20. goto code
  21. skip:
  22. StrCpy $0 SkipWrite
  23. goto output
  24. stop:
  25. StrCpy $0 StopLineFind
  26. goto output
  27.  
  28. ;;(Delete lines from a line to another line (also including that line))
  29. ; StrCmp $R0 finish code
  30. ; StrCmp $R0 start finish
  31. ; StrCmp $R9 'Start Line$\r$\n' 0 code
  32. ; StrCpy $R0 start
  33. ; StrCpy $R1 $R8
  34. ; goto skip
  35. ; finish:
  36. ; StrCmp $R9 'Finish Line$\r$\n' 0 skip
  37. ; StrCpy $R0 finish
  38. ; StrCpy $R2 $R8
  39. ; skip:
  40. ; StrCpy $0 SkipWrite
  41. ; goto output
  42.  
  43. code:
  44. ;...
  45.  
  46. output:
  47. Push $0
  48. FunctionEnd

Example7 (read lines):

  1. Section
  2. ${LineFind} "C:\a.log" "/NUL" "1:-1" "Example7"
  3. IfErrors 0 +2
  4. MessageBox MB_OK "Error"
  5. SectionEnd
  6.  
  7. Function Example7
  8. MessageBox MB_OKCANCEL '$$R9 "Line"=[$R9]$\n$$R8 "#" =[$R8]' IDOK +2
  9. StrCpy $0 StopLineFind
  10.  
  11. Push $0
  12. FunctionEnd

E.2.3 LineRead

  • Get line in file specified with number.

Syntax:

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

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

Example:

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

E.2.4 FileReadFromEnd

  • Read text file from end line by line.

Syntax:

  1. ${FileReadFromEnd} "[File]" "Function"
  1. "[File]" ; Input text file
  2. "Function" ; Callback function
  3.  
  4. Function "Function"
  5. ; $9 current line
  6. ; $8 current line number
  7. ; $7 current line negative number
  8.  
  9. ; $R0-$R9 are not used (save data in them).
  10. ; ...
  11.  
  12. Push $var ; If $var="StopFileReadFromEnd" Then exit from function
  13. FunctionEnd

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

Example1:

  1. Section
  2. ${FileReadFromEnd} "C:\a.log" "Example1"
  3.  
  4. IfErrors 0 +2
  5. MessageBox MB_OK "Error"
  6. SectionEnd
  7.  
  8. Function Example1
  9. MessageBox MB_OKCANCEL '"Line"=[$9]$\n "#"=[$8]$\n "-#"=[$7]' IDOK +2
  10. StrCpy $0 StopFileReadFromEnd
  11.  
  12. Push $0
  13. FunctionEnd

Example2 (Reverse text file):

  1. Section
  2. GetTempFileName $R0
  3. FileOpen $R1 $R0 w
  4. ${FileReadFromEnd} "C:\a.log" "Example2"
  5. FileClose $R1
  6.  
  7. IfErrors 0 +2
  8. MessageBox MB_OK "Error" IDOK +2
  9. Exec '"notepad.exe" "$R0"'
  10. SectionEnd
  11.  
  12. Function Example2
  13. StrCmp $7 -1 0 +5
  14. StrCpy $1 $9 1 -1
  15. StrCmp $1 '$\n' +3
  16. StrCmp $1 '$\r' +2
  17. StrCpy $9 '$9$\r$\n'
  18.  
  19. FileWrite $R1 "$9"
  20.  
  21. Push $0
  22. FunctionEnd

E.2.5 LineSum

  • Get sum of lines in text file.

Syntax:

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

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

Example:

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

E.2.6 FileJoin

  • Join two files in one (File1 + File2 = File3).

Syntax:

  1. ${FileJoin} "[File1]" "[File2]" "[File3]"
  1. "[File1]" ; Input File1
  2. "[File2]" ; Input File2
  3. "[File3]" ; Output File3
  4. ; 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):

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

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

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

E.2.7 TextCompare

  • Compare two text files.

Syntax:

  1. ${TextCompare} "[File1]" "[File2]" "[Option]" "Function"
  1. "[File1]" ; File1 Compare these lines
  2. "[File2]" ; File2 Compare with these lines
  3. "[Options]" ; (line-by-line):
  4. ; FastDiff Compare line N (File1) with line N (File2)
  5. ; Call function if Different lines found
  6. ; FastEqual Compare line N (File1) with line N (File2)
  7. ; Call function if Equal lines found
  8. ; (line number independent):
  9. ; SlowDiff Compare line N (File1) with all lines (File2)
  10. ; Call function if line N (File1) Different
  11. ; SlowEqual Compare line N (File1) with all lines (File2)
  12. ; Call function if line N (File1) Equal
  13. "Function" ; Callback function
  14.  
  15. Function "Function"
  16. ; $9 "Line File1"
  17. ; $8 "Line number"
  18. ; $7 "Line File2" (empty if SlowDiff)
  19. ; $6 "Line number" (empty if SlowDiff)
  20.  
  21. ; $R0-$R9 are not used (save data in them).
  22. ; ...
  23.  
  24. Push $var ; If $var="StopTextCompare" Then exit from function
  25. FunctionEnd

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

Example (Different or Equal):

  1. Section
  2. StrCpy $R0 ''
  3. ${TextCompare} "C:\1.txt" "C:\2.txt" "FastDiff" "Example1"
  4. IfErrors 0 +2
  5. MessageBox MB_OK "Error" IDOK +4
  6.  
  7. StrCmp $R0 NotEqual 0 +2
  8. MessageBox MB_OK "Files differ" IDOK +2
  9. MessageBox MB_OK "Files identical"
  10. SectionEnd
  11.  
  12. Function Example1
  13. StrCpy $R0 NotEqual
  14. StrCpy $0 StopTextCompare
  15.  
  16. Push $0
  17. FunctionEnd

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

  1. Section
  2. StrCpy $R0 'Text1.txt'
  3. StrCpy $R1 'Text2.txt'
  4.  
  5. GetTempFileName $R2
  6. FileOpen $R3 $R2 w
  7. FileWrite $R3 "$R0 | $R1$\r$\n"
  8. ${TextCompare} "$R0" "$R1" "FastDiff" "Example2"
  9. IfErrors 0 +2
  10. MessageBox MB_OK "Error" IDOK +2
  11.  
  12. Exec "notepad.exe $R2"
  13. FunctionEnd
  14.  
  15. Function Example2
  16. FileWrite $R3 '$8=$9'
  17. FileWrite $R3 '$6=$7$\r$\n'
  18.  
  19. Push $0
  20. FunctionEnd

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

  1. Section
  2. StrCpy $R0 'Text1.txt'
  3. StrCpy $R1 'Text2.txt'
  4.  
  5. GetTempFileName $R2
  6. FileOpen $R3 $R2 w
  7. FileWrite $R3 "$R0 | $R1$\r$\n"
  8. ${TextCompare} "$R0" "$R1" "FastEqual" "Example3"
  9. IfErrors 0 +2
  10. MessageBox MB_OK "Error" IDOK +2
  11.  
  12. Exec "notepad.exe $R2"
  13. FunctionEnd
  14.  
  15. Function Example3
  16. FileWrite $R3 '$8|$6=$9'
  17.  
  18. Push $0
  19. FunctionEnd

Example (Compare all lines - Different):

  1. Section
  2. StrCpy $R0 'Text1.txt'
  3. StrCpy $R1 'Text2.txt'
  4.  
  5. GetTempFileName $R2
  6. FileOpen $R3 $R2 w
  7. FileWrite $R3 "$R0 | $R1$\r$\n"
  8. ${TextCompare} "$R0" "$R1" "SlowDiff" "Example4"
  9. IfErrors 0 +2
  10. MessageBox MB_OK "Error" IDOK end
  11.  
  12. FileWrite $R3 "$\r$\n$R1 | $R0$\r$\n"
  13. ${TextCompare} "$R1" "$R0" "SlowDiff" "Example4"
  14. FileClose $R3
  15. IfErrors 0 +2
  16. MessageBox MB_OK "Error" IDOK end
  17.  
  18. Exec "notepad.exe $R2"
  19.  
  20. end:
  21. FunctionEnd
  22.  
  23. Function Example4
  24. FileWrite $R3 '$8=$9'
  25.  
  26. Push $0
  27. FunctionEnd

Example (Compare all lines - Equal):

  1. Section
  2. StrCpy $R0 'Text1.txt'
  3. StrCpy $R1 'Text2.txt'
  4.  
  5. GetTempFileName $R2
  6. FileOpen $R3 $R2 w
  7. FileWrite $R3 "$R0 | $R1$\r$\n"
  8. ${TextCompare} "$R0" "$R1" "SlowEqual" "Example5"
  9. IfErrors 0 +2
  10. MessageBox MB_OK "Error" IDOK +2
  11.  
  12. Exec "notepad.exe $R2"
  13. FunctionEnd
  14.  
  15. Function Example5
  16. FileWrite $R3 '$8|$6=$9'
  17.  
  18. Push $0
  19. FunctionEnd

Example (Show variables):

  1. Section
  2. ${TextCompare} "C:\1.txt" "C:\2.txt" "FastDiff" "Example6"
  3.  
  4. IfErrors 0 +2
  5. MessageBox MB_OK "Error"
  6. SectionEnd
  7.  
  8. Function Example6
  9. MessageBox MB_OKCANCEL '\
  10. $$9 "Line File1" =[$9]$\n\
  11. $$8 "Line #" =[$8]$\n\
  12. $$7 "Line File2" =[$7]$\n\
  13. $$6 "Line #" =[$6]'\
  14. IDOK +2
  15. StrCpy $0 StopTextCompare
  16.  
  17. Push $0
  18. FunctionEnd

E.2.8 TextCompareS

E.2.9 ConfigRead

  • Read value from entry name in config file.

Syntax:

  1. ${ConfigRead} "[File]" "[Entry]" $var
  1. "[File]" ; config file
  2. ;
  3. "[Entry]" ; entry name
  4. ;
  5. $var ; Result: Value

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

Example1:

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

Example2:

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

E.2.10 ConfigReadS

E.2.11 ConfigWrite

  • Write value from entry name in config file.

Syntax:

  1. ${ConfigWrite} "[File]" "[Entry]" "[Value]" $var
  1. "[File]" ; config file
  2. ;
  3. "[Entry]" ; entry name
  4. ;
  5. "[Value]" ; value name
  6. ; if "" then delete Entry
  7. ;
  8. $var ; Result:
  9. ; $var=CHANGED Value is written
  10. ; $var=DELETED Entry is deleted
  11. ; $var=ADDED Entry and Value are added
  12. ; $var=SAME Entry and Value already exist

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

Example1:

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

Example2:

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

Example3:

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

E.2.12 ConfigWriteS

E.2.13 FileRecode

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

Syntax:

  1. ${FileRecode} "[File]" "[Format]"
  1. "[File]" ;
  2. ;
  3. "[Format]" ; OemToChar -from DOS to Windows
  4. ; CharToOem -from Windows to DOS

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

Example:

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

E.2.14 TrimNewLines

  • Trim newlines in a string.

Syntax:

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

Example:

  1. Section
  2. ${TrimNewLines} "Text line$\r$\n" $R0
  3. ; $R0="Text line"
  4. SectionEnd