NSIS可用头文件

校睿宝培训机构ERP,学员课时管理软件
实验预约管理软件,实验室管理软件,大学、高校实验预约平台
网站日志分析软件,网站访问日志在线分析
哲涛服务器监控软件,IT运维管理软件,CPU、内存、磁盘监控软件

E.1 File Functions Header

E.1.1 Introduction

Include header:

!include "FileFunc.nsh"

Call functions:

Section Install
     ${GetFileExt} "C:\My Downloads\Index.html" $R0
     ; $R0="html"
SectionEnd
Section un.Install
     ${GetParent} "C:\My Downloads\Index.html" $R0
     ; $R0="C:\My Downloads"
SectionEnd

E.1.2 Locate

  • Find files, directories and empty directories with mask and size options.

Syntax:

${Locate} "[Path]" "[Options]" "Function"
"[Path]"      ; Disk or Directory
              ;
"[Options]"   ; /L=[FD|F|D|DE|FDE]
              ;     /L=FD    - Locate Files and Directories (default)
              ;     /L=F     - Locate Files only
              ;     /L=D     - Locate Directories only
              ;     /L=DE    - Locate Empty Directories only
              ;     /L=FDE   - Locate Files and Empty Directories
              ; /M=[mask]
              ;     /M=*.*         - Locate all (default)
              ;     /M=*.doc       - Locate Work.doc, 1.doc ...
              ;     /M=Pho*        - Locate PHOTOS, phone.txt ...
              ;     /M=win???.exe  - Locate winamp.exe, winver.exe ...
              ;     /M=winamp.exe  - Locate winamp.exe only
              ; /S=No:No
              ;     /S=      - Don't locate file size (faster) (default)
              ;     /S=0:0B  - Locate only files of 0 Bytes exactly
              ;     /S=5:9K  - Locate only files of 5 to 9 Kilobytes
              ;     /S=:10M  - Locate only files of 10 Megabyte or less
              ;     /S=1G    - Locate only files of 1 Gigabyte or more
              ; /G=[1|0]
              ;     /G=1     - Locate with subdirectories (default)
              ;     /G=0     - Locate without subdirectories
              ; /B=[0|1]
              ;     /B=0     - Banner isn't used (default)
              ;     /B=1     - Banner is used. Callback when function
              ;                start to search in new directory
"Function"    ; Callback function when found

Function "Function"
	; $R9    "path\name"
	; $R8    "path"
	; $R7    "name"
	; $R6    "size"  ($R6="" if directory, $R6="0" if file with /S=)

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

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

Note: 
- Error flag if disk or directory isn't exist 
- Error flag if syntax error 
- See also Locate plugin

Example (Find one file):

Section
	${Locate} "C:\ftp" "/L=F /M=RPC DCOM.rar /S=1K" "Example1"
	; 'RPC DCOM.rar' file in 'C:\ftp' with size 1 Kb or more

	IfErrors 0 +2
	MessageBox MB_OK "Error" IDOK +2
	MessageBox MB_OK "$$R0=$R0"
SectionEnd

Function Example1
	StrCpy $R0 $R9
	; $R0="C:\ftp\files\RPC DCOM.rar"

	MessageBox MB_YESNO '$R0$\n$\nFind next?' IDYES +2
	StrCpy $0 StopLocate

	Push $0
FunctionEnd

Example (Write results to a text file):

Section
	GetTempFileName $R0
	FileOpen $R1 $R0 w
	${Locate} "C:\ftp" "/S=:2M /G=0" "Example2"
	; folders and all files with size 2 Mb or less
	; don't scan subdirectories
	FileClose $R1

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

Function Example2
	StrCmp $R6 '' 0 +3
	FileWrite $R1 "Directory=$R9$\r$\n"
	goto +2
	FileWrite $R1 "File=$R9  Size=$R6 Mb$\r$\n"

	Push $0
FunctionEnd

Example (Write results to an INI file):

Section
	GetTempFileName $R0
	${Locate} "C:\ftp" "/L=F /S=0K" "Example3"
	; all files in 'C:\ftp' with size detect in Kb

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

Function Example3
	WriteINIStr $R0 "$R8" "$R7" "$R6 Kb"

	Push $0
FunctionEnd

Example (Delete empty directories):

Section
	StrCpy $R2 0
	StrCpy $R3 0

	loop:
	StrCpy $R1 0
	${Locate} "C:\ftp" "/L=DE" "Example4"
	IntOp $R3 $R3 + 1
	IntOp $R2 $R2 + $R1
	StrCmp $R0 StopLocate +2
	StrCmp $R1 0 0 loop

	IfErrors 0 +2
	MessageBox MB_OK 'error' IDOK +2
	MessageBox MB_OK '$R2 directories were removed$\n$R3 loops'
SectionEnd

Function Example4
	MessageBox MB_YESNOCANCEL 'Delete empty "$R9"?' IDNO end IDCANCEL cancel
	RMDir $R9
	IntOp $R1 $R1 + 1
	goto end

	cancel:
	StrCpy $R0 StopLocate

	end:
	Push $R0
FunctionEnd

Example (Move all files into one folder):

Section
	StrCpy $R0 "C:\ftp"   ;Directory move from
	StrCpy $R1 "C:\ftp2"  ;Directory move into

	StrCpy $R2 0
	StrCpy $R3 0
	${Locate} "$R0" "/L=F" "Example5"

	IfErrors 0 +2
	MessageBox MB_OK 'error' IDOK +4
	StrCmp $R3 0 0 +2
	MessageBox MB_OK '$R2 files were moved' IDOK +2
	MessageBox MB_OK '$R2 files were moved$\n$R3 files were NOT moved'
SectionEnd

Function Example5
	StrCmp $R8 $R1 +6
	IfFileExists '$R1\$R7' +4
	Rename $R9 '$R1\$R7'
	IntOp $R2 $R2 + 1
	goto +2
	IntOp $R3 $R3 + 1

	Push $0
FunctionEnd

Example (Copy files with log):

Section
	StrCpy $R0 "C:\ftp"   ;Directory copy from
	StrCpy $R1 "C:\ftp2"  ;Directory copy into
	StrLen $R2 $R0

	GetTempFileName $0
	FileOpen $R3 $0 w
	${Locate} "$R0" "/L=FDE" "Example6"
	FileClose $R3

	IfErrors 0 +2
	MessageBox MB_OK 'error'

	Exec '"notepad.exe" "$0"'     ;view log
SectionEnd

Function Example6
	StrCpy $1 $R8 '' $R2

	StrCmp $R6 '' 0 +3
	CreateDirectory '$R1$1\$R7'
	goto end
	CreateDirectory '$R1$1'
	CopyFiles /SILENT $R9 '$R1$1'

	IfFileExists '$R1$1\$R7' 0 +3
	FileWrite $R3 "-old:$R9  -new:$R1$1\$R7  -success$\r$\n"
	goto +2
	FileWrite $R3 "-old:$R9  -new:$R1$1\$R7  -failed$\r$\n"

	end:
	Push $0
FunctionEnd

Example (Recreate directory structure):

Section
	StrCpy $R0 "C:\ftp"     ;Directory structure from
	StrCpy $R1 "C:\ftp2"    ;Directory structure into
	StrLen $R2 $R0

	${Locate} "$R0" "/L=D" "Example7"

	IfErrors 0 +2
	MessageBox MB_OK 'error'
SectionEnd

Function Example7
	StrCpy $1 $R9 '' $R2
	CreateDirectory '$R1$1'

	Push $0
FunctionEnd

Example (Locate with banner - NxS plugin required):

Section
	nxs::Show /NOUNLOAD `$(^Name) Setup` /top `Setup searching something$\r$\nPlease wait... If you can..` /h 1 /can 1 /end
	${Locate} "C:\WINDOWS" "/L=F /M=*.inf /B=1" "Example8"
	nxs::Destroy
SectionEnd

Function Example8
	StrCmp $R0 $R8 abortcheck
	StrCpy $R0 $R8
	nxs::Update /NOUNLOAD /sub "$R8" /pos 78 /end

	abortcheck:
	nxs::HasUserAborted /NOUNLOAD
	Pop $0
	StrCmp $0 1 0 +2
	StrCpy $0 StopLocate

	StrCmp $R9 '' end
	;...

	end:
	Push $0
FunctionEnd

E.1.3 GetSize

  • Find the size of a file, files mask or directory.
  • Find the sum of the files, directories and subdirectories.

Syntax:

${GetSize} "[Path]" "[Options]" $var1 $var2 $var3
"[Path]"      ; Disk or Directory
              ;
"[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
              ;     /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
              ;
$var1         ; Result1: Size
$var2         ; Result2: Sum of files
$var3         ; Result3: Sum of directories

Note: 
- Error flag if disk or directory isn't exist 
- Error flag if syntax error 
- See also Locate plugin

Example (1):

Section
	; Find file size "C:\WINDOWS\Explorer.exe" in kilobytes

	${GetSize} "C:\WINDOWS" "/M=Explorer.exe /S=0K /G=0" $0 $1 $2
	; $0="220" Kb
	; $1="1"   files
	; $2=""    directories

	IfErrors 0 +2
	MessageBox MB_OK "Error"
SectionEnd

Example (2):

Section
	; Find folder size "C:\Installs\Reanimator\Drivers" in megabytes

	${GetSize} "C:\Installs\Reanimator\Drivers" "/S=0M" $0 $1 $2
	; $0="132" Mb
	; $1="555" files
	; $2="55"  directories

	IfErrors 0 +2
	MessageBox MB_OK "Error"
SectionEnd

Example (3):

Section
	; Find sum of files and folders "C:\WINDOWS" (no subfolders)

	${GetSize} "C:\WINDOWS" "/G=0" $0 $1 $2
	; $0=""    size
	; $1="253" files
	; $2="46"  directories

	IfErrors 0 +2
	MessageBox MB_OK "Error"
SectionEnd

E.1.4 DriveSpace

  • Get total, occupied or free space of the drive.

Syntax:

${DriveSpace} "[Drive]" "[Options]" $var
"[Drive]"     ; Disk to check
              ;     
"[Options]"   ; /D=[T|O|F]
              ;     /D=T  - Total space (default)
              ;     /D=O  - Occupied space
              ;     /D=F  - Free space
              ; /S=
              ;     /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 disk isn't exist or not ready 
- Error flag if syntax error

Example:

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

E.1.5 GetDrives

  • Find all available drives in the system.

Syntax:

${GetDrives} "[Option]" "Function"
"[Option]"      ; [FDD+HDD+CDROM+NET+RAM]
                ;   FDD    Floppy Disk Drives
                ;   HDD    Hard Disk Drives 
                ;   CDROM  CD-ROM Drives
                ;   NET    Network Drives
                ;   RAM    RAM Disk Drives
                ;
                ; [ALL]
                ;   Find all drives by letter (default)
                ;
"Function"      ; Callback function when found

Function "Function"
	; $9    "drive letter"  (a:\ c:\ ...)
	; $8    "drive type"    (FDD HDD ...)

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

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

Example1:

Section
	${GetDrives} "FDD+CDROM" "Example1"
SectionEnd

Function Example1
	MessageBox MB_OK "$9  ($8 Drive)"

	Push $0
FunctionEnd

Example2:

Section
	${GetDrives} "ALL" "Example2"
SectionEnd

Function Example2
	MessageBox MB_OK "$9  ($8 Drive)"

	Push $0
FunctionEnd

Example3 (Get type of drive):

Section
	StrCpy $R0 "D:\"      ;Drive letter
	StrCpy $R1 "invalid"

	${GetDrives} "ALL" "Example3"

	MessageBox MB_OK "Type of drive $R0 is $R1"
SectionEnd

Function Example3
	StrCmp $9 $R0 0 +3
	StrCpy $R1 $8
	StrCpy $0 StopGetDrives

	Push $0
FunctionEnd

E.1.6 GetTime

  • Get local or system time.
  • Get file time (access, creation and modification).

Syntax:

${GetTime} "[File]" "[Option]" $var1 $var2 $var3 $var4 $var5 $var6 $var7
"[File]"        ; Ignored if "L" or "LS"
                ;
"[Option]"      ; [Options]
                ;   L   Local time
                ;   A   last Access file time
                ;   C   Creation file time
                ;   M   Modification file time
                ;   LS  System time (UTC)
                ;   AS  last Access file time (UTC)
                ;   CS  Creation file time (UTC)
                ;   MS  Modification file time (UTC)
                ;
$var1           ; Result1: day
$var2           ; Result2: month
$var3           ; Result3: year
$var4           ; Result4: day of week name
$var5           ; Result5: hour
$var6           ; Result6: minute
$var7           ; Result7: seconds

Note: 
- Error flag if file isn't exist 
- Error flag if syntax error 
- See also Time plugin

Example (Get local time):

Section
	${GetTime} "" "L" $0 $1 $2 $3 $4 $5 $6
	; $0="01"      day
	; $1="04"      month
	; $2="2005"    year
	; $3="Friday"  day of week name
	; $4="16"      hour
	; $5="05"      minute
	; $6="50"      seconds

	MessageBox MB_OK 'Date=$0/$1/$2 ($3)$\nTime=$4:$5:$6'
SectionEnd

Example (Get file time):

Section
	${GetTime} "$WINDIR\Explorer.exe" "C" $0 $1 $2 $3 $4 $5 $6
	; $0="12"       day
	; $1="10"       month
	; $2="2004"     year
	; $3="Tuesday"  day of week name
	; $4="2"        hour
	; $5="32"       minute
	; $6="03"       seconds

	IfErrors 0 +2
	MessageBox MB_OK "Error" IDOK +2
	MessageBox MB_OK 'Date=$0/$1/$2 ($3)$\nTime=$4:$5:$6'
SectionEnd

Example (Get system time):

Section
	${GetTime} "" "LS" $0 $1 $2 $3 $4 $5 $6
	; $0="01"      day
	; $1="04"      month
	; $2="2005"    year
	; $3="Friday"  day of week name
	; $4="11"      hour
	; $5="05"      minute
	; $6="50"      seconds

	MessageBox MB_OK 'Date=$0/$1/$2 ($3)$\nTime=$4:$5:$6'
SectionEnd

Example (Convert time to 12-hour format AM/PM):

Section
	${GetTime} "" "L" $0 $1 $2 $3 $4 $5 $6

	StrCmp $4 0 0 +3
	StrCpy $4 12
	goto +3
	StrCmp $4 12 +5
	IntCmp $4 12 0 0 +3
	StrCpy $7 AM
	goto +3
	IntOp $4 $4 - 12
	StrCpy $7 PM

	MessageBox MB_OK 'Date=$0/$1/$2 ($3)$\nTime=$4:$5:$6 $7'
SectionEnd

E.1.7 GetFileAttributes

  • Get attributes of file or directory.

Syntax:

${GetFileAttributes} "[File]" "[Attributes]" $var
"[File]"          ; File or directory
                  ;
"[Attributes]"    ; "ALL"  (default)
                  ;  -all attributes of file combined with "|" to output
                  ;
                  ; "READONLY|HIDDEN|SYSTEM|DIRECTORY|ARCHIVE|
                  ; DEVICE|NORMAL|TEMPORARY|SPARSE_FILE|REPARSE_POINT|
                  ; COMPRESSED|OFFLINE|NOT_CONTENT_INDEXED|ENCRYPTED"
                  ;  -file must have specified attributes
                  ;
$var              ; Result:
                  ;    $var=attr1|attr2|... (if used "ALL")
                  ;    $var=1   file has specified attributes
                  ;    $var=0   file has no specified attributes

Note: 
- Error flag if file doesn't exist

Example1:

Section
	${GetFileAttributes} "C:\MSDOS.SYS" "ALL" $R0
	; $R0=READONLY|HIDDEN|SYSTEM|ARCHIVE
SectionEnd

Example2:

Section
	${GetFileAttributes} "C:\MSDOS.SYS" "SYSTEM|HIDDEN" $R0
	; $R0=1
SectionEnd

Example3:

Section
	${GetFileAttributes} "C:\MSDOS.SYS" "NORMAL" $R0
	; $R0=0
SectionEnd

E.1.8 GetFileVersion

  • Get version information from executable file.

Syntax:

${GetFileVersion} "[Executable]" $var
"[Executable]"      ; Executable file (*.exe *.dll ...)
$var                ; Result: Version number

Note: 
- Error flag if file doesn't exist 
- Error flag if file doesn't contain version information

Example:

Section
	${GetFileVersion} "C:\ftp\program.exe" $R0
	; $R0="1.1.0.12"
SectionEnd

E.1.9 GetExeName

  • Get installer filename (with valid case for Windows 98/Me).

Syntax:

${GetExeName} $var

Example:

Section
	${GetExeName} $R0
	; $R0="C:\ftp\program.exe"
SectionEnd

E.1.10 GetExePath

  • Get installer pathname ($EXEDIR with valid case for Windows 98/Me).

Syntax:

${GetExePath} $var

Example:

Section
	${GetExePath} $R0
	; $R0="C:\ftp"
SectionEnd

E.1.11 GetParameters

  • Get command line parameters.

Syntax:

${GetParameters} $var

Example:

Section
	${GetParameters} $R0
	; $R0="[parameters]"
SectionEnd

E.1.12 GetOptions

  • Get options from command line parameters.

Syntax:

${GetOptions} "[Parameters]" "[Option]" $var
"[Parameters]"     ; command line parameters
                   ;
"[Option]"         ; option name
                   ;
$var               ; Result: option string

Note: 
- Error flag if option not found 
- First option symbol it is delimiter

Example1:

Section
	${GetOptions} "/S /T" "/T"  $R0

	IfErrors 0 +2
	MessageBox MB_OK "Not found" IDOK +2
	MessageBox MB_OK "Found"
SectionEnd

Example2:

Section
	${GetOptions} "-INSTDIR=C:\Program Files\Common Files -SILENT=yes" "-INSTDIR="  $R0
	;$R0=C:\Program Files\Common Files
SectionEnd

Example3:

Section
	${GetOptions} '/SILENT=yes /INSTDIR="C:/Program Files/Common Files" /ADMIN=password' "/INSTDIR="  $R0
	;$R0=C:/Program Files/Common Files
SectionEnd

Example4:

Section
	${GetOptions} `-SILENT=yes -INSTDIR='"C:/Program Files/Common Files"' -ADMIN=password` "-INSTDIR="  $R0
	;$R0="C:/Program Files/Common Files"
SectionEnd

E.1.13 GetOptionsS

E.1.14 GetRoot

  • Get root directory.

Syntax:

${GetRoot} "[FullPath]" $var

Example1:

Section
	${GetRoot} "C:\Program Files\NSIS" $R0
	; $R0="C:"
SectionEnd

Example2:

Section
	${GetRoot} "\\SuperPimp\NSIS\Source\exehead\Ui.c" $R0
	; $R0="\\SuperPimp\NSIS"
SectionEnd

E.1.15 GetParent

  • Get parent directory.

Syntax:

${GetParent} "[PathString]" $var

Example:

Section
	${GetParent} "C:\Program Files\Winamp\uninstwa.exe" $R0
	; $R0="C:\Program Files\Winamp"
SectionEnd

E.1.16 GetFileName

  • Get last part from directory path.

Syntax:

${GetFileName} "[PathString]" $var

Example:

Section
	${GetFileName} "C:\Program Files\Winamp\uninstwa.exe" $R0
	; $R0="uninstwa.exe"
SectionEnd

E.1.17 GetBaseName

  • Get file name without extension.

Syntax:

${GetBaseName} "[FileString]" $var

Example:

Section
	${GetBaseName} "C:\ftp\program.exe" $R0
	; $R0="program"
SectionEnd

E.1.18 GetFileExt

  • Get extension of file.

Syntax:

${GetFileExt} "[FileString]" $var

Example:

Section
	${GetFileExt} "C:\ftp\program.exe" $R0
	; $R0="exe"
SectionEnd

E.1.19 BannerTrimPath

  • Trim string path for banner.

Syntax:

${BannerTrimPath} "[PathString]" "[Option]" $var
"[PathString]"    ;
                  ;
"[Option]"        ; [Length][A|B|C|D]
                  ;
                  ; Length  -Maximum string length
                  ;   A     -Trim center path (default)
                  ;           (C:\root\...\third path) 
                  ;           If A mode not possible Then will be used B mode
                  ;   B     -Trim right path
                  ;           (C:\root\second path\...)
                  ;           If B mode not possible Then will be used C mode
                  ;   C     -Trim right string
                  ;           (C:\root\second path\third p...)
                  ;   D     -Trim right string + filename
                  ;           (C:\root\second p...\third path)
                  ;           If D mode not possible Then will be used C mode
                  ;
$var              ; Result:  Trimmed path

Example:

Section
	${BannerTrimPath} "C:\Server\Documents\Terminal\license.htm" "35A" $R0
	;$R0=C:\Server\...\Terminal\license.htm
SectionEnd

Example (Banner plugin):

!include "WinMessages.nsh"
!include "FileFunc.nsh"

Section
	Banner::show "Starting..."
	Banner::getWindow
	Pop $R1
	${Locate} "$WINDIR" "/L=F /M=*.* /B=1" "LocateCallback"
	Banner::destroy
SectionEnd

Function LocateCallback
	StrCmp $R0 $R8 code
	StrCpy $R0 $R8
	${BannerTrimPath} "$R8" "38B" $R8
	GetDlgItem $1 $R1 1030
	SendMessage $1 ${WM_SETTEXT} 0 "STR:$R8"

	code:
	StrCmp $R9 '' end
	;...

	end:
	Push $0
FunctionEnd

Example (NxS plugin):

!include "FileFunc.nsh"

Section
	nxs::Show /NOUNLOAD `$(^Name) Setup`\
	  /top `Setup searching something$\nPlease wait$\nIf you can...`\
	  /h 1 /can 1 /end
	${Locate} "$WINDIR" "/L=F /M=*.* /B=1" "LocateCallback"
	nxs::Destroy
SectionEnd

Function LocateCallback
	StrCmp $R0 $R8 abortcheck
	StrCpy $R0 $R8
	${BannerTrimPath} "$R8" "55A" $R8
	nxs::Update /NOUNLOAD /sub "$R8" /pos 78 /end

	abortcheck:
	nxs::HasUserAborted /NOUNLOAD
	Pop $0
	StrCmp $0 1 0 +2
	StrCpy $0 StopLocate

	StrCmp $R9 '' end
	;...

	end:
	Push $0
FunctionEnd

E.1.20 DirState

  • Check directory full, empty or not exist.

Syntax:

${DirState} "[path]" $var
"[path]"      ; Directory
$var          ; Result:
              ;    $var=0  (empty)
              ;    $var=1  (full)
              ;    $var=-1 (directory not found)

Example:

Section
	${DirState} "$TEMP" $R0
	; $R0="1"  directory is full
SectionEnd

E.1.21 RefreshShellIcons

  • After changing file associations, you can call this function to refresh the shell immediately.

Syntax:

${RefreshShellIcons}

Example:

Section
	WriteRegStr HKCR "Winamp.File\DefaultIcon" "" "$PROGRAMFILES\Winamp\WINAMP.EXE,2"

	${RefreshShellIcons}
SectionEnd

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

E.3 Word Functions Header

E.3.1 Introduction

Include header:

!include "WordFunc.nsh"

Call functions:

Section Install
	${WordFind} "A--H---S" "-" "+2" $R0
	; $R0="H"
SectionEnd
Section un.Install
	${WordReplace} "A--H---S" "-" "x" "+3*" $R0
	; $R0="A--HxS"
SectionEnd

E.3.2 WordFind

  • Multi-features string function.
Strings:
"[word+1][delimiter][word+2][delimiter][word+3]..."
"[delimiter][word+1][delimiter][word+2][delimiter]..."
"[delimiter][delimiter][word+1][delimiter][delimiter][delimiter]..."
"...[word-3][delimiter][word-2][delimiter][word-1]"
"...[delimiter][word-2][delimiter][word-1][delimiter]"
"...[delimiter][delimiter][word-1][delimiter][delimiter][delimiter]"

Syntax:

${WordFind} "[string]" "[delimiter]" "[E][options]" $var
"[string]"         ;[string]
                   ;  input string
"[delimiter]"      ;[delimiter]
                   ;  one or several symbols
"[E][options]"     ;[options]
                   ;  +number   : word number from start
                   ;  -number   : word number from end
                   ;  +number}  : delimiter number from start
                   ;              all space after this
                   ;              delimiter to output
                   ;  +number{  : delimiter number from start
                   ;              all space before this
                   ;              delimiter to output
                   ;  +number}} : word number from start
                   ;              all space after this word
                   ;              to output
                   ;  +number{{ : word number from start
                   ;              all space before this word
                   ;              to output
                   ;  +number{} : word number from start
                   ;              all space before and after
                   ;              this word (word exclude)
                   ;  +number*} : word number from start
                   ;              all space after this
                   ;              word to output with word
                   ;  +number{* : word number from start
                   ;              all space before this
                   ;              word to output with word
                   ;  #         : sum of words to output
                   ;  *         : sum of delimiters to output
                   ;  /word     : number of word to output
                   ;
                   ;[E]
                   ;  with errorlevel output
                   ;  IfErrors:
                   ;     $var=1  delimiter not found
                   ;     $var=2  no such word number
                   ;     $var=3  syntax error (Use: +1,-1},#,*,/word,...)
                   ;[]
                   ;  no errorlevel output (default)
                   ;  If some errors found then (result=input string)
                   ;
$var               ;output (result)

Note: 
- Accepted numbers 1,01,001,...

Example (Find word by number):

Section
	${WordFind} "C:\io.sys C:\Program Files C:\WINDOWS" " C:\" "-02" $R0
	; $R0="Program Files"
SectionEnd

Example (Delimiter exclude):

Section
	${WordFind} "C:\io.sys C:\logo.sys C:\WINDOWS" "sys" "-2}" $R0
	; $R0=" C:\logo.sys C:\WINDOWS"
SectionEnd

Example (Sum of words):

Section
	${WordFind} "C:\io.sys C:\logo.sys C:\WINDOWS" " C:\" "#" $R0
	; $R0="3"
SectionEnd

Example (Sum of delimiters):

Section
	${WordFind} "C:\io.sys C:\logo.sys C:\WINDOWS" "sys" "*" $R0
	; $R0="2"
SectionEnd

Example (Find word number):

Section
	${WordFind} "C:\io.sys C:\Program Files C:\WINDOWS" " " "/Files" $R0
	; $R0="3"
SectionEnd

Example ( }} ):

Section
	${WordFind} "C:\io.sys C:\logo.sys C:\WINDOWS" " " "+2}}" $R0
	; $R0=" C:\WINDOWS"
SectionEnd

Example ( {} ):

Section
	${WordFind} "C:\io.sys C:\logo.sys C:\WINDOWS" " " "+2{}" $R0
	; $R0="C:\io.sys C:\WINDOWS"
SectionEnd

Example ( *} ):

Section
	${WordFind} "C:\io.sys C:\logo.sys C:\WINDOWS" " " "+2*}" $R0
	; $R0="C:\logo.sys C:\WINDOWS"
SectionEnd

Example (Get parent directory):

Section
	StrCpy $R0 "C:\Program Files\NSIS\NSIS.chm"
;	           "C:\Program Files\NSIS\Include\"
;	           "C:\\Program Files\\NSIS\\NSIS.chm"

	${WordFind} "$R0" "\" "-2{*" $R0
	; $R0="C:\Program Files\NSIS"
	;     "C:\\Program Files\\NSIS"
SectionEnd

Example (Coordinates):

Section
	${WordFind} "C:\io.sys C:\logo.sys C:\WINDOWS" ":\lo" "E+1{" $R0
	; $R0="C:\io.sys C"
	IfErrors end

	StrLen $0 $R0             ; $0 = Start position of word (11)
	StrLen $1 ':\lo'          ; $1 = Word length (4)
	; StrCpy $R0 $R1 $1 $0    ; $R0 = :\lo

	end:
SectionEnd

Example (With errorlevel output):

Section
	${WordFind} "[string]" "[delimiter]" "E[options]" $R0

	IfErrors 0 end
	StrCmp $R0 1 0 +2       ; errorlevel 1?
	MessageBox MB_OK 'delimiter not found' IDOK end
	StrCmp $R0 2 0 +2       ; errorlevel 2?
	MessageBox MB_OK 'no such word number' IDOK end
	StrCmp $R0 3 0 +2       ; errorlevel 3?
	MessageBox MB_OK 'syntax error'

	end:
SectionEnd

Example (Without errorlevel output):

Section
	${WordFind} "C:\io.sys C:\logo.sys" "_" "+1" $R0

	; $R0="C:\io.sys C:\logo.sys" (error: delimiter "_" not found)
SectionEnd

Example (If found):

Section
	${WordFind} "C:\io.sys C:\logo.sys" ":\lo" "E+1{" $R0

	IfErrors notfound found
	found:
	MessageBox MB_OK 'Found' IDOK end
	notfound:
	MessageBox MB_OK 'Not found'

	end:
SectionEnd

Example (If found 2):

Section
	${WordFind} "C:\io.sys C:\logo.sys" ":\lo" "+1{" $R0

	StrCmp $R0 "C:\io.sys C:\logo.sys" notfound found        ; error?
	found:
	MessageBox MB_OK 'Found' IDOK end
	notfound:
	MessageBox MB_OK 'Not found'

	end:
SectionEnd

Example (To accept one word in string if delimiter not found):

Section
	StrCpy $0 'OneWord'
	StrCpy $1 1

	loop:
	${WordFind} "$0" " " "E+$1" $R0
	IfErrors 0 code
	StrCmp $1$R0 11 0 error
	StrCpy $R0 $0
	goto end

	code:
	; ...
	IntOp $1 $1 + 1
	goto loop

	error:
	StrCpy $1 ''
	StrCpy $R0 ''

	end:
	; $R0="OneWord"
SectionEnd

E.3.3 WordFindS

E.3.4 WordFind2X

  • Find word between two delimiters.
Strings:
"[delimiter1][word+1][delimiter2][delimiter1][word+2][delimiter2]..."
"[text][delimiter1][text][delimiter1][word+1][delimiter2][text]..."
"...[delimiter1][word-2][delimiter2][delimiter1][word-1][delimiter2]"
"...[text][delimiter1][text][delimiter1][word-1][delimiter2][text]"

Syntax:

${WordFind2X} "[string]" "[delimiter1]" "[delimiter2]" "[E][options]" $var
"[string]"         ;[string]
                   ;  input string
"[delimiter1]"     ;[delimiter1]
                   ;  first delimiter
"[delimiter2]"     ;[delimiter2]
                   ;  second delimiter
"[E][options]"     ;[options]
                   ;  +number   : word number from start
                   ;  -number   : word number from end
                   ;  +number}} : word number from start all space
                   ;              after this word to output
                   ;  +number{{ : word number from end all space
                   ;              before this word to output
                   ;  +number{} : word number from start
                   ;              all space before and after
                   ;              this word (word exclude)
                   ;  +number*} : word number from start
                   ;              all space after this
                   ;              word to output with word
                   ;  +number{* : word number from start
                   ;              all space before this
                   ;              word to output with word
                   ;  #         : sum of words to output
                   ;  /word     : number of word to output
                   ;
                   ;[E]
                   ;  with errorlevel output
                   ;  IfErrors:
                   ;     $var=1  no words found
                   ;     $var=2  no such word number
                   ;     $var=3  syntax error (Use: +1,-1,#)
                   ;[]
                   ;  no errorlevel output (default)
                   ;  If some errors found then (result=input string)
                   ;
$var               ;output (result)

Example (1):

Section
	${WordFind2X} "[C:\io.sys];[C:\logo.sys];[C:\WINDOWS]" "[C:\" "];" "+2" $R0
	; $R0="logo.sys"
SectionEnd

Example (2):

Section
	${WordFind2X} "C:\WINDOWS C:\io.sys C:\logo.sys" "\" "." "-1" $R0
	; $R0="logo"
SectionEnd

Example (3):

Section
	${WordFind2X} "C:\WINDOWS C:\io.sys C:\logo.sys" "\" "." "-1{{" $R0
	; $R0="C:\WINDOWS C:\io.sys C:"
SectionEnd

Example (4):

Section
	${WordFind2X} "C:\WINDOWS C:\io.sys C:\logo.sys" "\" "." "-1{}" $R0
	; $R0="C:\WINDOWS C:\io.sys C:sys"
SectionEnd

Example (5):

Section
	${WordFind2X} "C:\WINDOWS C:\io.sys C:\logo.sys" "\" "." "-1{*" $R0
	; $R0="C:\WINDOWS C:\io.sys C:\logo."
SectionEnd

Example (6):

Section
	${WordFind2X} "C:\WINDOWS C:\io.sys C:\logo.sys" "\" "." "/logo" $R0
	; $R0="2"
SectionEnd

Example (With errorlevel output):

Section
	${WordFind2X} "[io.sys];[C:\logo.sys]" "\" "];" "E+1" $R0
	; $R0="1" ("\...];" not found)

	IfErrors 0 noerrors
	MessageBox MB_OK 'Errorlevel=$R0' IDOK end

	noerrors:
	MessageBox MB_OK 'No errors'

	end:
SectionEnd

E.3.5 WordFind2XS

E.3.6 WordFind3X

  • Find a word that contains a string, between two delimiters.

Syntax:

${WordFind3X} "[string]" "[delimiter1]" "[center]" "[delimiter2]" "[E][options]" $var
"[string]"         ;[string]
                   ;  input string
"[delimiter1]"     ;[delimiter1]
                   ;  first delimiter
"[center]"         ;[center]
                   ;  center string
"[delimiter2]"     ;[delimiter2]
                   ;  second delimiter
"[E][options]"     ;[options]
                   ;  +number   : word number from start
                   ;  -number   : word number from end
                   ;  +number}} : word number from start all space
                   ;              after this word to output
                   ;  +number{{ : word number from end all space
                   ;              before this word to output
                   ;  +number{} : word number from start
                   ;              all space before and after
                   ;              this word (word exclude)
                   ;  +number*} : word number from start
                   ;              all space after this
                   ;              word to output with word
                   ;  +number{* : word number from start
                   ;              all space before this
                   ;              word to output with word
                   ;  #         : sum of words to output
                   ;  /word     : number of word to output
                   ;
                   ;[E]
                   ;  with errorlevel output
                   ;  IfErrors:
                   ;     $var=1  no words found
                   ;     $var=2  no such word number
                   ;     $var=3  syntax error (Use: +1,-1,#)
                   ;[]
                   ;  no errorlevel output (default)
                   ;  If some errors found then (result=input string)
                   ;
$var               ;output (result)

Example (1):

Section
	${WordFind3X} "[1.AAB];[2.BAA];[3.BBB];" "[" "AA" "];" "+1" $R0
	; $R0="1.AAB"
SectionEnd

Example (2):

Section
	${WordFind3X} "[1.AAB];[2.BAA];[3.BBB];" "[" "AA" "];" "-1" $R0
	; $R0="2.BAA"
SectionEnd

Example (3):

Section
	${WordFind3X} "[1.AAB];[2.BAA];[3.BBB];" "[" "AA" "];" "-1{{" $R0
	; $R0="[1.AAB];"
SectionEnd

Example (4):

Section
	${WordFind3X} "[1.AAB];[2.BAA];[3.BBB];" "[" "AA" "];" "-1{}" $R0
	; $R0="[1.AAB];[3.BBB];"
SectionEnd

Example (5):

Section
	${WordFind3X} "[1.AAB];[2.BAA];[3.BBB];" "[" "AA" "];" "-1{*" $R0
	; $R0="[1.AAB];[2.BAA];"
SectionEnd

Example (6):

Section
	${WordFind3X} "[1.AAB];[2.BAA];[3.BBB];" "[" "AA" "];" "/2.BAA" $R0
	; $R0="2"
SectionEnd

Example (With errorlevel output):

Section
	${WordFind3X} "[1.AAB];[2.BAA];[3.BBB];" "[" "XX" "];" "E+1" $R0
	; $R0="1" ("[...XX...];" not found)

	IfErrors 0 noerrors
	MessageBox MB_OK 'Errorlevel=$R0' IDOK end

	noerrors:
	MessageBox MB_OK 'No errors'

	end:
SectionEnd

E.3.7 WordFind3XS

E.3.8 WordReplace

  • Replace or delete word from string.

Syntax:

${WordReplace} "[string]" "[word1]" "[word2]" "[E][options]" $var
"[string]"         ;[string]
                   ;  input string
"[word1]"          ;[word1]
                   ;  word to replace or delete
"[word2]"          ;[word2]
                   ;  replace with (if empty delete)
"[E][options]"     ;[options]
                   ;  +number  : word number from start
                   ;  -number  : word number from end
                   ;  +number* : word number from start multiple-replace
                   ;  -number* : word number from end multiple-replace
                   ;  +        : replace all results
                   ;  +*       : multiple-replace all results
                   ;  {        : if exists replace all delimiters
                   ;               from left edge
                   ;  }        : if exists replace all delimiters
                   ;               from right edge
                   ;  {}       : if exists replace all delimiters
                   ;               from edges
                   ;  {*       : if exists multiple-replace all
                   ;               delimiters from left edge
                   ;  }*       : if exists multiple-replace all
                   ;               delimiters from right edge
                   ;  {}*      : if exists multiple-replace all
                   ;               delimiters from edges
                   ;
                   ;[E]
                   ;  with errorlevel output
                   ;  IfErrors:
                   ;     $var=1  word to replace not found
                   ;     $var=2  no such word number
                   ;     $var=3  syntax error (Use: +1,-1,+1*,-1*,+,+*,{},{}*)
                   ;[]
                   ;  no errorlevel output (default)
                   ;  If some errors found then (result=input string)
                   ;
$var               ;output (result)

Example (replace):

Section
	${WordReplace} "C:\io.sys C:\logo.sys C:\WINDOWS" "SYS" "bmp" "+2" $R0
	; $R0="C:\io.sys C:\logo.bmp C:\WINDOWS"
SectionEnd

Example (delete):

Section
	${WordReplace} "C:\io.sys C:\logo.sys C:\WINDOWS" "SYS" "" "+" $R0
	; $R0="C:\io. C:\logo. C:\WINDOWS"
SectionEnd

Example (multiple-replace 1):

Section
	${WordReplace} "C:\io.sys      C:\logo.sys   C:\WINDOWS" " " " " "+1*" $R0
	; +1* or +2* or +3* or +4* or +5* or +6*
	; $R0="C:\io.sys C:\logo.sys   C:\WINDOWS"
SectionEnd

Example (multiple-replace 2):

Section
	${WordReplace} "C:\io.sys C:\logo.sysSYSsys C:\WINDOWS" "sys" "bmp" "+*" $R0
	; $R0="C:\io.bmp C:\logo.bmp C:\WINDOWS"
SectionEnd

Example (multiple-replace 3):

Section
	${WordReplace} "sysSYSsysC:\io.sys C:\logo.sys C:\WINDOWSsysSYSsys" "sys" "|" "{}*" $R0
	; $R0="|C:\io.sys C:\logo.sys C:\WINDOWS|"
SectionEnd

Example (With errorlevel output):

Section
	${WordReplace} "C:\io.sys C:\logo.sys" "sys" "bmp" "E+3" $R0
	; $R0="2" (no such word number "+3")

	IfErrors 0 noerrors
	MessageBox MB_OK 'Errorlevel=$R0' IDOK end

	noerrors:
	MessageBox MB_OK 'No errors'

	end:
SectionEnd

E.3.9 WordReplaceS

E.3.10 WordAdd

  • Add words to string1 from string2 if not exist or delete words if exist.

Syntax:

${WordAdd} "[string1]" "[delimiter]" "[E][options]" $var
"[string1]"          ;[string1]
                     ;  string for addition or removing
"[delimiter]"        ;[delimiter]
                     ;  one or several symbols
"[E][options]"       ;[options]
                     ;  +string2 : words to add
                     ;  -string2 : words to delete
                     ;
                     ;[E]
                     ;  with errorlevel output
                     ;  IfErrors:
                     ;     $var=1  delimiter is empty
                     ;     $var=3  syntax error (use: +text,-text)
                     ;[]
                     ;  no errorlevel output (default)
                     ;  If some errors found then (result=input string)
                     ;
$var                 ;output (result)

Example (add):

Section
	${WordAdd} "C:\io.sys C:\WINDOWS" " " "+C:\WINDOWS C:\config.sys" $R0
	; $R0="C:\io.sys C:\WINDOWS C:\config.sys"
SectionEnd

Example (delete):

Section
	${WordAdd} "C:\io.sys C:\logo.sys C:\WINDOWS" " " "-C:\WINDOWS C:\config.sys C:\IO.SYS" $R0
	; $R0="C:\logo.sys"
SectionEnd

Example (add to one):

Section
	${WordAdd} "C:\io.sys" " " "+C:\WINDOWS C:\config.sys C:\IO.SYS" $R0
	; $R0="C:\io.sys C:\WINDOWS C:\config.sys"
SectionEnd

Example (delete one):

Section
	${WordAdd} "C:\io.sys C:\logo.sys C:\WINDOWS" " " "-C:\WINDOWS" $R0
	; $R0="C:\io.sys C:\logo.sys"
SectionEnd

Example (No new words found):

Section
	${WordAdd} "C:\io.sys C:\logo.sys" " " "+C:\logo.sys" $R0
	StrCmp $R0 "C:\io.sys C:\logo.sys" 0 +2
	MessageBox MB_OK "No new words found to add"
SectionEnd

Example (No words deleted):

Section
	${WordAdd} "C:\io.sys C:\logo.sys" " " "-C:\config.sys" $R0
	StrCmp $R0 "C:\io.sys C:\logo.sys" 0 +2
	MessageBox MB_OK "No words found to delete"
SectionEnd

Example (With errorlevel output):

Section
	${WordAdd} "C:\io.sys C:\logo.sys" "" "E-C:\logo.sys" $R0
	; $R0="1" (delimiter is empty "")

	IfErrors 0 noerrors
	MessageBox MB_OK 'Errorlevel=$R0' IDOK end

	noerrors:
	MessageBox MB_OK 'No errors'

	end:
SectionEnd

E.3.11 WordAddS

  • Same as WordAdd, but case sensitive.

E.3.12 WordInsert

  • Insert word in string.

Syntax:

${WordInsert} "[string]" "[delimiter]" "[word]" "[E][options]" $var
"[string]"          ;[string]
                    ;  input string
"[delimiter]"       ;[delimiter]
                    ;  one or several symbols
"[word]"            ;[word]
                    ;  word to insert
"[E][options]"      ;[options]
                    ;  +number  : word number from start
                    ;  -number  : word number from end
                    ;
                    ;[E]
                    ;  with errorlevel output
                    ;  IfErrors:
                    ;     $var=1  delimiter is empty
                    ;     $var=2  wrong word number
                    ;     $var=3  syntax error (Use: +1,-1)
                    ;[]
                    ;  no errorlevel output (default)
                    ;  If some errors found then (result=input string)
                    ;
$var                ;output (result)

Example (1):

Section
	${WordInsert} "C:\io.sys C:\WINDOWS" " " "C:\logo.sys" "-2" $R0
	; $R0="C:\io.sys C:\logo.sys C:\WINDOWS"
SectionEnd

Example (2):

Section
	${WordInsert} "C:\io.sys" " " "C:\WINDOWS" "+2" $R0
	; $R0="C:\io.sys C:\WINDOWS"
SectionEnd

Example (3):

Section
	${WordInsert} "" " " "C:\WINDOWS" "+1" $R0
	; $R0="C:\WINDOWS "
SectionEnd

Example (With errorlevel output):

Section
	${WordInsert} "C:\io.sys C:\logo.sys" " " "C:\logo.sys" "E+4" $R0
	; $R0="2" (wrong word number "+4")

	IfErrors 0 noerrors
	MessageBox MB_OK 'Errorlevel=$R0' IDOK end

	noerrors:
	MessageBox MB_OK 'No errors'

	end:
SectionEnd

E.3.13 WordInsertS

E.3.14 StrFilter

  • Convert string to uppercase or lowercase.
  • Set symbol filter.

Syntax:

${StrFilter} "[string]" "[options]" "[symbols1]" "[symbols2]" $var
"[string]"       ;[string]
                 ;  input string
                 ;
"[options]"      ;[+|-][1|2|3|12|23|31][eng|rus]
                 ;  +   : convert string to uppercase
                 ;  -   : convert string to lowercase
                 ;  1   : only Digits
                 ;  2   : only Letters
                 ;  3   : only Special
                 ;  12  : only Digits  + Letters
                 ;  23  : only Letters + Special
                 ;  31  : only Special + Digits
                 ;  eng : English symbols (default)
                 ;  rus : Russian symbols
                 ;
"[symbols1]"     ;[symbols1]
                 ;  symbols include (not changeable)
                 ;
"[symbols2]"     ;[symbols2]
                 ;  symbols exclude
                 ;
$var             ;output (result)

Note: 
- Error flag if syntax error 
- Same symbol to include & to exclude = to exclude

Example (UpperCase):

Section
	${StrFilter} "123abc 456DEF 7890|%#" "+" "" "" $R0
	; $R0="123ABC 456DEF 7890|%#"
SectionEnd

Example (LowerCase):

Section
	${StrFilter} "123abc 456DEF 7890|%#" "-" "ef" "" $R0
	; $R0="123abc 456dEF 7890|%#"
SectionEnd

Example (Filter1):

Section
	${StrFilter} "123abc 456DEF 7890|%#" "2" "|%" "" $R0
	; $R0="abcDEF|%"       ;only Letters + |%
SectionEnd

Example (Filter2):

Section
	${StrFilter} "123abc 456DEF 7890|%#" "13" "af" "4590" $R0
	; $R0="123a 6F 78|%#"  ;only Digits + Special + af - 4590
SectionEnd

Example (Filter3):

Section
	${StrFilter} "123abc 456DEF 7890|%#" "+12" "b" "def" $R0
	; $R0="123AbC4567890"  ;only Digits + Letters + b - def
SectionEnd

Example (Filter4):

Section
	${StrFilter} "123abcÀÁÂ 456DEFãäå 7890|%#" "+12rus" "ä" "ãå" $R0
	; $R0="123ÀÁÂ456ä7890"  ;only Digits + Letters + ä - ãå
SectionEnd

Example (English + Russian Letters):

Section
	${StrFilter} "123abcÀÁÂ 456DEFãäå 7890|%#" "2rus" "" "" $R0
	; $R0="ÀÁÂãäå"        ;only Russian Letters
	${StrFilter} "123abcÀÁÂ 456DEFãäå 7890|%#" "2" "$R0" "" $R0
	; $R0="abcÀÁÂDEFãäå"  ;only English + Russian Letters
SectionEnd

Example (Word Capitalize):

Section
	Push "_01-PERPETUOUS_DREAMER__-__THE_SOUND_OF_GOODBYE_(ORIG._MIX).MP3_"
	Call Capitalize
	Pop $R0
	; $R0="_01-Perpetuous_Dreamer__-__The_Sound_Of_Goodbye_(Orig._Mix).mp3_"

	${WordReplace} "$R0" "_" " " "+*" $R0
	; $R0=" 01-Perpetuous Dreamer - The Sound Of Goodbye (Orig. Mix).mp3 "

	${WordReplace} "$R0" " " "" "{}" $R0
	; $R0="01-Perpetuous Dreamer - The Sound Of Goodbye (Orig. Mix).mp3"
SectionEnd

Function Capitalize
	Exch $R0
	Push $0
	Push $1
	Push $2

	${StrFilter} '$R0' '-eng' '' '' $R0
	${StrFilter} '$R0' '-rus' '' '' $R0

	StrCpy $0 0

	loop:
	IntOp $0 $0 + 1
	StrCpy $1 $R0 1 $0
	StrCmp $1 '' end
	StrCmp $1 ' ' +5
	StrCmp $1 '_' +4
	StrCmp $1 '-' +3
	StrCmp $1 '(' +2
	StrCmp $1 '[' 0 loop
	IntOp $0 $0 + 1
	StrCpy $1 $R0 1 $0
	StrCmp $1 '' end

	${StrFilter} '$1' '+eng' '' '' $1
	${StrFilter} '$1' '+rus' '' '' $1

	StrCpy $2 $R0 $0
	IntOp $0 $0 + 1
	StrCpy $R0 $R0 '' $0
	IntOp $0 $0 - 2
	StrCpy $R0 '$2$1$R0'
	goto loop

	end:
	Pop $2
	Pop $1
	Pop $0
	Exch $R0
FunctionEnd

E.3.15 StrFilterS

E.3.16 VersionCompare

  • Compare version numbers.

Syntax:

${VersionCompare} "[Version1]" "[Version2]" $var
"[Version1]"        ; First version
"[Version2]"        ; Second version
$var                ; Result:
                    ;    $var=0  Versions are equal
                    ;    $var=1  Version1 is newer
                    ;    $var=2  Version2 is newer

Example:

Section
	${VersionCompare} "1.1.1.9" "1.1.1.01" $R0
	; $R0="1"
SectionEnd

E.3.17 VersionConvert

  • Convert version in the numerical format which can be compared.

Syntax:

${VersionConvert} "[Version]" "[CharList]" $var
"[Version]"         ; Version
                    ;
"[CharList]"        ; List of characters, which will be replaced by numbers
                    ; "abcdefghijklmnopqrstuvwxyz" (default)
                    ;
$var                ; Result: converted version

Note: 
- Converted letters are separated with dot 
- If character is non-digit and not in list then it will be converted to dot

Example1:

Section
	${VersionConvert} "9.0a" "" $R0
	; $R0="9.0.01"

	${VersionConvert} "9.0c" "" $R1
	; $R1="9.0.03"

	${VersionCompare} "$R0" "$R1" $R2
	; $R2="2"   version2 is newer
SectionEnd

Example2:

Section
	${VersionConvert} "0.15c-9m" "" $R0
	; $R0="0.15.03.9.13"

	${VersionConvert} "0.15c-1n" "" $R1
	; $R1="0.15.03.1.14"

	${VersionCompare} "$R0" "$R1" $R2
	; $R2="1"   version1 is newer
SectionEnd

Example3:

Section
	${VersionConvert} "0.15c+" "abcdefghijklmnopqrstuvwxyz+" $R0
	; $R0="0.15.0327"

	${VersionConvert} "0.15c" "abcdefghijklmnopqrstuvwxyz+" $R1
	; $R1="0.15.03"

	${VersionCompare} "$R0" "$R1" $R2
	; $R2="1"   version1 is newer
SectionEnd

校睿宝培训机构ERP,学员课时管理软件
实验预约管理软件,实验室管理软件,大学、高校实验预约平台
网站日志分析软件,网站访问日志在线分析
哲涛服务器监控软件,IT运维管理软件,CPU、内存、磁盘监控软件
点我咨询