#-- Replace file content function--# Function AdvReplaceInFile Exch $0 ;file to replace in Exch Exch $1 ;number to replace after Exch Exch 2 Exch $2 ;replace and onwards Exch 2 Exch 3 Exch $3 ;replace with Exch 3 Exch 4 Exch $4 ;to replace Exch 4 Push $5 ;minus count Push $6 ;universal Push $7 ;end string Push $8 ;left string Push $9 ;right string Push $R0 ;file1 Push $R1 ;file2 Push $R2 ;read Push $R3 ;universal Push $R4 ;count (onwards) Push $R5 ;count (after) Push $R6 ;temp file name GetTempFileName $R6 FileOpen $R1 $0 r ;file to search in FileOpen $R0 $R6 w ;temp file StrLen $R3 $4 StrCpy $R4 -1 StrCpy $R5 -1 loop_read: ClearErrors FileRead $R1 $R2 ;read line IfErrors exit StrCpy $5 0 StrCpy $7 $R2 loop_filter: IntOp $5 $5 - 1 StrCpy $6 $7 $R3 $5 ;search StrCmp $6 "" file_write1 StrCmp $6 $4 0 loop_filter StrCpy $8 $7 $5 ;left part IntOp $6 $5 + $R3 IntCmp $6 0 is0 not0 is0: StrCpy $9 "" Goto done not0: StrCpy $9 $7 "" $6 ;right part done: StrCpy $7 $8$3$9 ;re-join IntOp $R4 $R4 + 1 StrCmp $2 all loop_filter StrCmp $R4 $2 0 file_write2 IntOp $R4 $R4 - 1 IntOp $R5 $R5 + 1 StrCmp $1 all loop_filter StrCmp $R5 $1 0 file_write1 IntOp $R5 $R5 - 1 Goto file_write2 file_write1: FileWrite $R0 $7 ;write modified line Goto loop_read file_write2: FileWrite $R0 $R2 ;write unmodified line Goto loop_read exit: FileClose $R0 FileClose $R1 SetDetailsPrint none Delete $0 Rename $R6 $0 Delete $R6 SetDetailsPrint both Pop $R6 Pop $R5 Pop $R4 Pop $R3 Pop $R2 Pop $R1 Pop $R0 Pop $9 Pop $8 Pop $7 Pop $6 Pop $5 Pop $0 Pop $1 Pop $2 Pop $3 Pop $4 FunctionEnd
It is used as follows:
#-- Replace startup path --# Push C:\www\tools #text to be replaced Push $INSTDIR #replace with Push all #replace all occurrences Push all #replace all occurrences Push $INSTDIR\memcached\uninstall.bat #file to replace in Call AdvReplaceInFile
Modify the ini file
WriteINIStr $INSTDIR\php-7.0.15-nts-Win32-VC14-x64\php.ini PHP upload_tmp_dir "$INSTDIR\tmp\php-7.0.15-nts-Win32-VC14-x64"
replace string content
import header file
!include "WordFunc.nsh"
${WordReplace} $INSTDIR "\" "/" "+" $R0
Packaging web environment sample code
; The script was generated using the HM VNISEdit script editor wizard ; Installer initial definition constants !define PRODUCT_NAME "One-click construction of web environment" !define PRODUCT_VERSION "1.0" !define PRODUCT_PUBLISHER "Ningbo Yusheng Information Technology Co., Ltd." !define PRODUCT_UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}" !define PRODUCT_UNINST_ROOT_KEY "HKLM" SetCompressor lzma ; ------ MUI modern interface definition (1.67+ compatible) ------ !include "MUI.nsh" !include "WordFunc.nsh" ; MUI predefined constants !define MUI_ABORTWARNING !define MUI_ICON "${NSISDIR}\Contrib\Graphics\Icons\modern-install.ico" !define MUI_UNICON "${NSISDIR}\Contrib\Graphics\Icons\modern-uninstall.ico" ; welcome page !insertmacro MUI_PAGE_WELCOME ; License Agreement page !insertmacro MUI_PAGE_LICENSE "授权.txt" ; component selection page !insertmacro MUI_PAGE_COMPONENTS ; install directory selection page !insertmacro MUI_PAGE_DIRECTORY ; installation process page !insertmacro MUI_PAGE_INSTFILES ; Installation complete page !insertmacro MUI_PAGE_FINISH ; install uninstall process page !insertmacro MUI_UNPAGE_INSTFILES ; Language settings included in the installation interface !insertmacro MUI_LANGUAGE "SimpChinese" ; install prerelease files !insertmacro MUI_RESERVEFILE_INSTALLOPTIONS ;------ MUI end of modern interface definition------ Name "${PRODUCT_NAME} ${PRODUCT_VERSION}" OutFile "Setup.exe" InstallDir "$PROGRAMFILES\websoft" ShowInstDetails show ShowUnInstDetails show Section "Main Program (Required)" SEC01 SetOutPath "$INSTDIR\tmp" SetOverwrite ifnewer File /r "D:\profile\deployment\installanywhere\src\tmp\*.*" SetOutPath "$INSTDIR" File "..\src\Instructions.txt" SectionEnd Section "nginx" SEC02 SetOutPath "$INSTDIR\nginx" File /r "..\src\nginx\*.*" #-- Replace startup path --# Push C:/www/tools #text to be replaced Push $INSTDIR #replace with Push all #replace all occurrences Push all #replace all occurrences Push $INSTDIR\nginx\start_nginx.bat #file to replace in Call AdvReplaceInFile SectionEnd Section "Apache" SEC03 SetOutPath "$INSTDIR\Apache24" File /r "..\src\Apache24\*.*" #-- Replace startup path --# ${WordReplace} $INSTDIR "\" "/" "+" $R0 Push C:/www/tools #text to be replaced Push $R0 #replace with Push all #replace all occurrences Push all #replace all occurrences Push $INSTDIR\Apache24\conf\httpd.conf #file to replace in Call AdvReplaceInFile #-- Replace startup path --# Push C:\www\tools #text to be replaced Push $INSTDIR #replace with Push all #replace all occurrences Push all #replace all occurrences Push $INSTDIR\Apache24\conf\httpd.conf #file to replace in Call AdvReplaceInFile #-- Replace startup path --# Push C:\www\tools #text to be replaced Push $INSTDIR #replace with Push all #replace all occurrences Push all #replace all occurrences Push $INSTDIR\Apache24\install.bat #file to replace in Call AdvReplaceInFile #-- Replace startup path --# Push C:\www\tools #text to be replaced Push $INSTDIR #replace with Push all #replace all occurrences Push all #replace all occurrences Push $INSTDIR\Apache24\uninstall.bat #file to replace in Call AdvReplaceInFile SectionEnd Section "php5.6.29" SEC04 SetOutPath "$INSTDIR\php-5.6.29-nts-Win32-VC11-x64" File /r "..\src\php-5.6.29-nts-Win32-VC11-x64\*.*" #-- Modify the php.ini file--# WriteINIStr $INSTDIR\php-5.6.29-nts-Win32-VC11-x64\php.ini PHP upload_tmp_dir "$INSTDIR\tmp\php-5.6.29-nts-Win32-VC11-x64" WriteINIStr $INSTDIR\php-5.6.29-nts-Win32-VC11-x64\php.ini PHP sys_temp_dir "$INSTDIR\tmp\php-5.6.29-nts-Win32-VC11-x64" WriteINIStr $INSTDIR\php-5.6.29-nts-Win32-VC11-x64\php.ini Session session.save_path "$INSTDIR\tmp\php-5.6.29-nts-Win32-VC11-x64" WriteINIStr $INSTDIR\php-5.6.29-nts-Win32-VC11-x64\php.ini soap soap.wsdl_cache_dir "$INSTDIR\tmp\php-5.6.29-nts-Win32-VC11-x64" SectionEnd Section "php7.0.15" SEC05 SetOutPath "$INSTDIR\php-7.0.15-nts-Win32-VC14-x64" File /r "..\src\php-7.0.15-nts-Win32-VC14-x64\*.*" WriteINIStr $INSTDIR\php-7.0.15-nts-Win32-VC14-x64\php.ini PHP upload_tmp_dir "$INSTDIR\tmp\php-7.0.15-nts-Win32-VC14-x64" WriteINIStr $INSTDIR\php-7.0.15-nts-Win32-VC14-x64\php.ini PHP sys_temp_dir "$INSTDIR\tmp\php-7.0.15-nts-Win32-VC14-x64" WriteINIStr $INSTDIR\php-7.0.15-nts-Win32-VC14-x64\php.ini Session session.save_path "$INSTDIR\tmp\php-7.0.15-nts-Win32-VC14-x64" WriteINIStr $INSTDIR\php-7.0.15-nts-Win32-VC14-x64\php.ini soap soap.wsdl_cache_dir "$INSTDIR\tmp\php-7.0.15-nts-Win32-VC14-x64" SectionEnd Section "php7.1.1" SEC06 SetOutPath "$INSTDIR\php-7.1.1-nts-Win32-VC14-x64" File /r "..\src\php-7.1.1-nts-Win32-VC14-x64\*.*" WriteINIStr $INSTDIR\php-7.1.1-nts-Win32-VC14-x64\php.ini PHP upload_tmp_dir "$INSTDIR\tmp\php-7.1.1-nts-Win32-VC14-x64" WriteINIStr $INSTDIR\php-7.1.1-nts-Win32-VC14-x64\php.ini PHP sys_temp_dir "$INSTDIR\tmp\php-7.1.1-nts-Win32-VC14-x64" WriteINIStr $INSTDIR\php-7.1.1-nts-Win32-VC14-x64\php.ini Session session.save_path "$INSTDIR\tmp\php-7.1.1-nts-Win32-VC14-x64" WriteINIStr $INSTDIR\php-7.1.1-nts-Win32-VC14-x64\php.ini soap soap.wsdl_cache_dir "$INSTDIR\tmp\php-7.1.1-nts-Win32-VC14-x64" SectionEnd Section "memcached" SEC07 SetOutPath "$INSTDIR\memcached" File /r "..\src\memcached\*.*" #-- Replace startup path --# Push C:\www\tools #text to be replaced Push $INSTDIR #replace with Push all #replace all occurrences Push all #replace all occurrences Push $INSTDIR\memcached\install.bat #file to replace in Call AdvReplaceInFile #-- Replace startup path --# Push C:\www\tools #text to be replaced Push $INSTDIR #replace with Push all #replace all occurrences Push all #replace all occurrences Push $INSTDIR\memcached\uninstall.bat #file to replace in Call AdvReplaceInFile SectionEnd Section "orcle Client" SEC08 SetOutPath "$INSTDIR\oracle_client_10_2" File /r "..\src\oracle_client_10_2\*.*" #-- path --# ReadRegStr $0 HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" "Path" WriteRegExpandStr HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" "Path" "$0;$INSTDIR\oracle_client_10_2" SectionEnd Section "tools" SEC09 SetOutPath "$INSTDIR\tools" File /r "..\src\tools\*.*" SectionEnd Section -Post WriteUninstaller "$INSTDIR\uninst.exe" WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayName" "$(^Name)" WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "UninstallString" "$INSTDIR\uninst.exe" WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayVersion" "${PRODUCT_VERSION}" WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "Publisher" "${PRODUCT_PUBLISHER}" SectionEnd #-- According to NSIS script editing rules, all Function sections must be written after Section sections to avoid unpredictable problems with the installer. --# ; section component description !insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN !insertmacro MUI_DESCRIPTION_TEXT ${SEC01} "Required for main program" !insertmacro MUI_DESCRIPTION_TEXT ${SEC02} "" !insertmacro MUI_DESCRIPTION_TEXT ${SEC03} "" !insertmacro MUI_DESCRIPTION_TEXT ${SEC04} "" !insertmacro MUI_DESCRIPTION_TEXT ${SEC05} "" !insertmacro MUI_DESCRIPTION_TEXT ${SEC06} "" !insertmacro MUI_DESCRIPTION_TEXT ${SEC07} "" !insertmacro MUI_DESCRIPTION_TEXT ${SEC08} "" !insertmacro MUI_DESCRIPTION_TEXT ${SEC09} "" !insertmacro MUI_FUNCTION_DESCRIPTION_END /****************************** *The following is the uninstall part of the installer* ******************************/ Section Uninstall SetOutPath '$INSTDIR' IfFileExists $INSTDIR\nginx\stop_nginx.bat 0 +2 ExecWait '$INSTDIR\nginx\stop_nginx.bat' IfFileExists $INSTDIR\Apache24\uninstall.bat 0 +2 ExecWait '$INSTDIR\Apache24\uninstall.bat' IfFileExists $INSTDIR\memcached\uninstall.bat 0 +2 ExecWait '$INSTDIR\memcached\uninstall.bat' Delete "$INSTDIR\uninst.exe" Delete "$INSTDIR\Instructions.txt" RMDir /r "$INSTDIR\tmp" RMDir /r "$INSTDIR\php-7.1.1-nts-Win32-VC14-x64" RMDir /r "$INSTDIR\php-7.0.15-nts-Win32-VC14-x64" RMDir /r "$INSTDIR\php-5.6.29-nts-Win32-VC11-x64" RMDir /r "$INSTDIR\oracle_client_10_2" RMDir /r "$INSTDIR\nginx" RMDir /r "$INSTDIR\memcached" RMDir /r "$INSTDIR\Apache24" RMDir /r "$INSTDIR\tools" RMDir "$INSTDIR" DeleteRegKey ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" ReadRegStr $R0 HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" "Path" ${WordReplace} $R0 ";$INSTDIR\oracle_client_10_2" "" "+" $R1 ;MessageBox MB_OK|MB_USERICON '$R0 - $INSTDIR - $R1 ' WriteRegExpandStr HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" "Path" "$R1" SetAutoClose true SectionEnd #-- According to NSIS script editing rules, all Function sections must be written after Section sections to avoid unpredictable problems with the installer. --# Function un.onInit MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 "Are you sure you want to completely remove $(^Name) and all its components?" IDYES +2 Abort FunctionEnd Function un.onUninstSuccess HideWindow MessageBox MB_ICONINFORMATION|MB_OK "$(^Name) was successfully removed from your computer." FunctionEnd #-- Replace file content function--# Function AdvReplaceInFile Exch $0 ;file to replace in Exch Exch $1 ;number to replace after Exch Exch 2 Exch $2 ;replace and onwards Exch 2 Exch 3 Exch $3 ;replace with Exch 3 Exch 4 Exch $4 ;to replace Exch 4 Push $5 ;minus count Push $6 ;universal Push $7 ;end string Push $8 ;left string Push $9 ;right string Push $R0 ;file1 Push $R1 ;file2 Push $R2 ;read Push $R3 ;universal Push $R4 ;count (onwards) Push $R5 ;count (after) Push $R6 ;temp file name GetTempFileName $R6 FileOpen $R1 $0 r ;file to search in FileOpen $R0 $R6 w ;temp file StrLen $R3 $4 StrCpy $R4 -1 StrCpy $R5 -1 loop_read: ClearErrors FileRead $R1 $R2 ;read line IfErrors exit StrCpy $5 0 StrCpy $7 $R2 loop_filter: IntOp $5 $5 - 1 StrCpy $6 $7 $R3 $5 ;search StrCmp $6 "" file_write1 StrCmp $6 $4 0 loop_filter StrCpy $8 $7 $5 ;left part IntOp $6 $5 + $R3 IntCmp $6 0 is0 not0 is0: StrCpy $9 "" Goto done not0: StrCpy $9 $7 "" $6 ;right part done: StrCpy $7 $8$3$9 ;re-join IntOp $R4 $R4 + 1 StrCmp $2 all loop_filter StrCmp $R4 $2 0 file_write2 IntOp $R4 $R4 - 1 IntOp $R5 $R5 + 1 StrCmp $1 all loop_filter StrCmp $R5 $1 0 file_write1 IntOp $R5 $R5 - 1 Goto file_write2 file_write1: FileWrite $R0 $7 ;write modified line Goto loop_read file_write2: FileWrite $R0 $R2 ;write unmodified line Goto loop_read exit: FileClose $R0 FileClose $R1 SetDetailsPrint none Delete $0 Rename $R6 $0 Delete $R6 SetDetailsPrint both Pop $R6 Pop $R5 Pop $R4 Pop $R3 Pop $R2 Pop $R1 Pop $R0 Pop $9 Pop $8 Pop $7 Pop $6 Pop $5 Pop $0 Pop $1 Pop $2 Pop $3 Pop $4 FunctionEnd
references
Replacing characters in a file in NSIS
http://www.cnblogs.com/zdxster/archive/2011/04/13/2015496.html