`

NSIS可用头文件

    博客分类:
  • nsis
阅读更多

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[B|K|M|G]

              ;     /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[B|K|M|G]

              ;     /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=[B|K|M|G]

              ;     /S=B  - size in Bytes (default)

              ;     /S=K  - size in Kilobytes

              ;     /S=M  - size in Megabytes

              ;     /S=G  - size in Gigabytes

              ;

$var          ; Result: Size

Note: 

- Error flag if 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

 

Same as GetOptions, but case sensitive.

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

 

Same as TextCompare, but case sensitive.

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

 

Same as ConfigRead, but case sensitive.

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

 

Same as ConfigWrite, but case sensitive.

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

 

Same as WordFind, but case sensitive.

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

 

Same as WordFind2X, but case sensitive.

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

 

Same as WordFind3X, but case sensitive.

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

 

Same as WordReplace, but case sensitive.

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

 

Same as WordInsert, but case sensitive.

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

 

Same as StrFilter, but case sensitive.

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

 

文章出处:http://www.zhetao.com/content124

 

分享到:
评论

相关推荐

    NSIS仅卸载安装的文件 UninstallLog.nsh

    Nsis的头文件,支持卸载安装的文件,不会卸载其他文件,避免整个磁盘被删掉的尴尬情况 https://nsis.sourceforge.io/Advanced_Uninstall_Log_NSIS_Header

    NSIS+Duilib自定义安装程序

    1. **环境准备**:首先,你需要下载并安装NSIS编译器(makensis.exe),以及Duilib的相关库和头文件。确保编译器和库文件路径已添加到系统环境变量中。 2. **编写NSIS脚本**:使用NSIS的脚本语言(称为NSIS ...

    NSIS-Unicode.7z NSIS

    3. 头文件(`.h`)和库文件(`.lib`):这些可能包含了NSIS的API和其他功能的定义,供脚本引用。 4. 图标、许可证文件、帮助文档等资源:安装程序中通常会包含这些元素,以便为用户提供更友好的交互体验。 5. 可能还...

    NSIS所需空间大小使用.zip

    标题 "NSIS所需空间大小使用.zip" 暗示了这个压缩包包含了关于如何在NSIS(Nullsoft Scriptable Install System)中计算安装程序所需的磁盘空间的信息。NSIS是一款开源的Windows安装制作系统,允许开发者创建自定义...

    nsis打包脚本

    NSIS(Nullsoft Scriptable Install System)是一款强大的开源安装制作工具,主要用于在Windows平台上创建安装程序。它允许开发者使用简单的脚本语言(NSIS Scripting Language)来编写自定义的安装、卸载脚本,从而...

    c++开发NSIS插件示例

    include`引入插件API头文件。 **2. C++与NSIS插件的结合** C++是一种强大且灵活的编程语言,适合用于实现复杂的逻辑和操作。在开发NSIS插件时,你可以利用C++的面向对象特性来组织代码。以下是一些关键点: - **...

    仿 百度云安装包 NSIS打包源码

    Include 编译需要的头文件,请自己复制Include下所有文件到NSIS目录下的Include如:C:\Program Files\NSIS\Include Plugins 编译需要的DLL插件,请自己复制Plugins下所有文件到NSIS目录下的Plugins如:C:\Program ...

    nsis支持库FindProcDLL.dll

    通常,这可以通过在NSIS脚本中包含`FindProcDLL.nsh`头文件来完成,这个头文件定义了如何使用插件的函数。 确保将FindProcDLL.dll文件复制到你的安装程序的运行目录或者系统路径下,这样在运行时才能找到并正确调用...

    nsis+winCodeSign+nsis-resources-3.4.1.zip

    《NSIS + WinCodeSign + NSIS Resources:创建安全可执行安装程序的综合指南》 在软件开发领域,创建一个可执行的安装程序是至关重要的一步,它使得用户能够便捷地安装和卸载你的应用程序。NSIS (Nullsoft ...

    NSIS-中文帮助文档

    NSIS-中文帮助文档 NSIS 用户手册 新闻、信息、支持、例子、指南等可以到 http://nsis.sf.net 查看。 快速链接: FAQ - 常见问题列表 NSIS Wiki - 例子、函数、指南、插件、软件等等 Forum - 发表你的问题或进行 ...

    nsis-3.0.1.4.7z文件.zip

    这个文件以.7z格式压缩,可能包含NSIS的源代码、编译器、头文件、库和其他相关资源,用于构建自定义的Windows安装程序。 描述中的“electron打包exe必备”表明NSIS在这个场景下是与Electron相关的。Electron是一个...

    nsis的killer插件

    include`指令引入插件的头文件,然后使用`Function`和`Call`命令来调用插件的特定函数。例如,如果`killer.dll`提供了` KillerFunction`函数,你可以在脚本中这样使用: ```nsis !include Killer.nsh Function ....

    NSIS使用教程档.zip_nsis

    NSIS(Nullsoft Scriptable Install System)是一款强大的、开源的Windows平台下的安装制作系统,它允许用户使用简单的脚本语言创建具有专业级别的安装程序。这个“NSIS使用教程档.zip_nsis”压缩包包含了深入学习...

    解决NSIS病毒方法

    ### 解决NSIS病毒方法 #### 一、NSIS病毒简介与危害 NSIS(Nullsoft Scriptable Install System)是一种开源的Windows安装系统脚本语言,用于创建安装程序。然而,由于其灵活性和广泛的应用范围,NSIS也被黑客利用...

    NSIS安装包.zip

    NSIS(Nullsoft Scriptable Install System)是一款流行的开源安装制作系统,主要用于创建Windows平台上的安装、卸载程序。它的特点是可脚本化、灵活且功能强大,被广大开发者用于快速生成定制化的安装程序。在提供...

    NSIS美化控件

    总之,“NSIS美化控件”是提升NSIS安装程序用户体验的关键组成部分,通过结合使用Include中的头文件、Docs中的指南、Examples中的示例、Plugins中的功能插件,以及Contrib中的社区贡献,开发者可以打造出功能强大且...

    NSIS_2.46_Unicode版本

    NSIS(Nullsoft Scriptable Install System)是一款流行的开源安装制作工具,主要用于创建Windows平台上的安装程序。这个"NSIS_2.46_Unicode版本"是NSIS的一个特定构建,特别针对非中文环境下的安装问题进行了优化,...

    NSIS killer.dll Unicode Ansi

    【NSIS killer.dll Unicode Ansi】是一个专门针对NSIS(Nullsoft Scriptable Install System)程序的工具,它具有独特的功能,能够检测并终止32位和64位程序进程。NSIS是一款广泛使用的开源安装制作系统,允许开发者...

    NSIS卸载保留文件夹

    ### NSIS卸载保留文件夹知识点详解 #### 一、NSIS简介 NSIS (Nullsoft Scriptable Install System) 是一种开源的Windows安装系统,基于脚本语言编写,支持创建功能强大且高度定制化的安装程序。它允许开发者通过...

    NSIS+Duilib 自定义安装程序

    【NSIS+Duilib 自定义安装程序】 NSIS(Nullsoft Scriptable Install System)是一款流行的开源安装制作系统,它允许开发者使用脚本语言创建Windows平台上的安装程序。NSIS提供了丰富的功能,包括文件复制、注册表...

Global site tag (gtag.js) - Google Analytics