Include header:
!include "FileFunc.nsh"
Include function "GetFileExt" for install and "GetParent" for uninstall:
!insertmacro GetFileExt !insertmacro un.GetParent
Call functions:
Section Install
${GetFileExt} "C:\My Downloads\Index.html" $R0
; $R0="html"
SectionEnd
Section un.Install
${un.GetParent} "C:\My Downloads\Index.html" $R0
; $R0="C:\My Downloads"
SectionEnd
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
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 founded in 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 founded in 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
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
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
Syntax:
${GetSize} "[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
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
Syntax:
${GetTime} "[File]" "[Option]" $var1 $var2 $var3 $var4 $var5 $var6 $var7
"[File]" ; Ignored if "L"
;
"[Option]" ; [Options]
; L Local time
; A last Access file time
; C Creation file time
; M Modification file time
;
$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
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
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 isn'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
Syntax:
${GetFileVersion} "[Executable]" $var
"[Executable]" ; Executable file (*.exe *.dll ...) $var ; Result: Version number
Note:
-Error flag if file isn't exist
-Error flag if file isn't contain version information
Example:
Section
${GetFileVersion} "C:\ftp\program.exe" $R0
; $R0="1.1.0.12"
SectionEnd
Syntax:
${GetExeName} $var
Example:
Section
${GetExeName} $R0
; $R0="C:\ftp\program.exe"
SectionEnd
Syntax:
${GetExePath} $var
Example:
Section
${GetExePath} $R0
; $R0="C:\ftp"
SectionEnd
Syntax:
${GetParameters} $var
Example:
Section
${GetParameters} $R0
; $R0="[parameters]"
SectionEnd
Syntax:
${GetOptions} "[Parameters]" "[Option]" $var
"[Parameters]" ; command line parameters
;
"[Option]" ; option name
;
$var ; Result: option string
Note:
-First option symbol it is delimiter
Example1:
Section
${GetOptions} "/INSTDIR=Temp /SILENT=yes /ADMIN=qwerty" "/ADMIN=" $R0
;$R0=qwerty
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
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
Syntax:
${GetParent} "[PathString]" $var
Example:
Section
${GetParent} "C:\Program Files\Winamp\uninstwa.exe" $R0
; $R0="C:\Program Files\Winamp"
SectionEnd
Syntax:
${GetFileName} "[PathString]" $var
Example:
Section
${GetFileName} "C:\Program Files\Winamp\uninstwa.exe" $R0
; $R0="uninstwa.exe"
SectionEnd
Syntax:
${GetBaseName} "[FileString]" $var
Example:
Section
${GetBaseName} "C:\ftp\program.exe" $R0
; $R0="program"
SectionEnd
Syntax:
${GetFileExt} "[FileString]" $var
Example:
Section
${GetFileExt} "C:\ftp\program.exe" $R0
; $R0="exe"
SectionEnd
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"
!insertmacro Locate
Section
Banner::show /NOUNLOAD "Starting..."
Banner::getWindow /NOUNLOAD
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"
!insertmacro Locate
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
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
Syntax:
${RefreshShellIcons}
Example:
Section
WriteRegStr HKCR "Winamp.File\DefaultIcon" "" "$PROGRAMFILES\Winamp\WINAMP.EXE,2"
${RefreshShellIcons}
SectionEnd