NSIS - Adding other type of files - nsis

I have a NSIS generated-installer, however, I would like to add other files to the installer at compile time. For example, I want to add a batch file to the installer, so when the .exe file is ran, I can call that batch file within the .exe without needing to copy the actual batch file to the output location. This means the batch file will actually exist within the installer.
I hope this makes sense and thanks.

A batch file has to be extracted somewhere before it can be executed. $PluginsDir is a good choice because it is deleted when the installer ends.
Section
SetOutPath $Instdir
File myfiles\*
InitPluginsDir
SetOutPath $PluginsDir
File file.bat
nsExec::Exec '"cmd.exe" /c if 1==1 "$PluginsDir\file.bat"'
Pop $0 ; Exit code
SetOutPath $Temp ; Don't hold lock on pluginsdir
SectionEnd

Related

Generate installation logs

I am using special logging build to generate installation logs.
I have observed the logs are not generating when called another installer from installer script.
For ex -
ExecWait '"$INSTDIR\installer1.exe" /S _?=$INSTDIR'
The log is generating for main installer but not for installer1.exe
The installer1.exe contains lots of components and I need to print the logs for the same. I have tried enabling logset on in the installer1 script but no luck.
Tried using dumplog but it doesn't work with silent installation.
Any help would be appreciated!
Sample code from Main Installer script --
InstallDir "C:\MyFolder"
Name "${PRODUCT_NAME_VERSION}"
OutFile "${OUT_FILE}"
Section "Test"
SetOutPath $INSTDIR
LogSet on
ExecWait '"$EXEDIR\Packages\installer1.exe" /S /INST=$INSTDIR' $0
SectionEnd
Sample code from sub-installer script ---
InstallDir "C:\MyFolder"
Section "-Demo"
SetOutPath $INSTDIR
LogSet on
LogText "Print something"
SetOutPath $INSTDIR\ExternalFolder\Demo
File /nonfatal /a /r $INSTDIR\ExternalFolder\Demo\Test
ExecWait '"$INSTDIR\ExternalFolder\Demo\Test\TestSetup.exe" /silent '
SectionEnd
The sub-installer (installer1.exe) is pre-compiled and kept the exe in $EXEDIR\Packages\installer1.exe The patch is valid.
_?= is special syntax that is only supported by NSIS uninstallers, installers use /D=.
ExecWait '"$InstDir\installer.exe" /S /D=$InstDir'
Logging of course has to be enabled in this sub-installer as well.
/D= overrides the InstallDir attributes, forcing $InstDir to the specified path before .onInit is executed.
InstallDir $INSTDIR does not make sense, use something like InstallDir "$ProgramFiles\MyApp"

How to delete the startup file itself test.exe?

After running the .exe file, I need to delete the file so that it no longer exists.
I have a script that I create a file to run, but how to delete it after launch?
Outfile test.exe
Requestexecutionlevel user
Setcompressor LZMA
Page instfiles
Section
;Not a good idea to hardcode the destination like this,
;normally you would use a directory page so the user can choose
SetOutPath "c:\SaveToThisFolder"
;Take thefile.txt from the system you compile on and output it on the end users system
;into the directory specified by the SetOutPath call
File "thefile.txt"
Sectionend
How to delete file test.exe after run?
It is hard for a Windows process to delete itself and NSIS does not really support it but you can try the SelfDel plug-in

How do I execute the app right after installation ... with arguments?

My installation EXE should unzip itself in a temp folder, then execute an app therein (with passing args), return and delete the temp folder again.
How do I write the nsi script?
It sounds to me like you are trying to create a portable application. Portable applications are always better when the original author adds support for it because it can handle registry and other configuration files correctly.
If you still want to create a launcher application you can do something like this:
OutFile "MyLauncher.exe"
RequestExecutionLevel User
SilentInstall Silent
SetCompressor LZMA
!include FileFunc.nsh
!insertmacro GetParameters
Section
${GetParameters} $1
InitPluginsDir
SetOutPath $PluginsDir
File "c:\myfiles\MyApp.exe"
File /r "c:\myfiles\otherfiles\*.*" ; If you need to include other files required by the application
ExecWait '"$PluginsDir\MyApp.exe" /param1 "pa ra m2" /param3 $1' $0 ; $1 contains the parameters passed to your launcher, remove it if you don't want to pass those arguments
SetErrorLevel $0
SetOutPath $Temp ; Don't lock $PluginsDir so it can be deleted automatically by the installer
SectionEnd
Answering my own question.
The required nsi script skeleton should look like this:
# The name of the installer (arbitrary)
Name "hello"
# The name of the installation file
OutFile "hello.exe"
# where put the installation - other options would be $TEMP, etc.
InstallDir $DESKTOP
RequestExecutionLevel user # no Windows UAC popup please!
SilentInstall silent # completely silent install
SetCompressor /SOLID /FINAL lzma # max compression for inst. file
# The stuff to install
Section ""
SetOutPath $INSTDIR # where to install (overwritable by user!)
File /r D:\...\... # where the install material lives
SectionEnd
# this function auto-runs after installation is fine
Function .onInstSuccess
# parameter are passed through via $CMDLINE
ExecWait '"$OUTDIR\hello.dist\hello.exe" $CMDLINE'
RMDir /r "$OUTDIR\hello.dist" # remove install folder again
FunctionEnd

How to pack files into exe and extract it at specified folder with NSIS?

I wrote a NSIS-script that creates an exe wich creates a new folder and copies files into the folder. Is there a way to include the files into the exe?
Name "First Installer"
OutFile "firstinstaller.exe"
InstallDir C:\dev\NSIS\Scripts\FirstInstaller
Section "move test.exe"
CreateDirectory $INSTDIR\test
SetOutPath $INSTDIR\test
CopyFiles $EXEDIR\test.txt $EXEDIR\test
SectionEnd
I now want to include the test.txt into the exe! Now you only have to start the exe and the test.txt is extracted from the exe into the created folder!
Name "First Installer"
OutFile "firstinstaller.exe"
InstallDir D:\dev\FirstInstaller
Section "move test.exe"
;CreateDirectory $INSTDIR\test
SetOutPath $INSTDIR\test ; extract exe content at this path, you can also specify other path
File /r "D:\dev\test.txt" ;used to include file in exe
SectionEnd

IfFileExists command run 3 command lines

How do I the result of IfFileExists command run three lines and not only the first.
In the code below is the result of IfFileExists is TemWSConfig, must perform the 3 lines to the NaoTemWSConfig command.
Presently the line 2 and 3 after TemWSConfig, always run
IfFileExists "$INSTDIR\IntegradorWS.exe.Config" TemWSConfig NaoTemWSConfig
TemWSConfig:
File "..\IntegradorWS\bin\x86\Release\AppInstalado.config"
Rename "$INSTDIR\IntegradorWS.exe.Config" "$INSTDIR\Antigo_IntegradorWS.exe.Config"
Rename "$INSTDIR\AppInstalado.config" "$INSTDIR\IntegradorWS.exe.Config"
NaoTemWSConfig:
File "..\IntegradorWS\bin\x86\Release\IntegradorWS.exe.Config"
NSIS does not skip around like this, you can verify that by switching out the problematic instructions:
Section
; Fake the IntegradorWS.exe.Config file for this example:
StrCpy $INSTDIR $temp
File "/oname=$INSTDIR\IntegradorWS.exe.Config" "${__FILE__}"
IfFileExists "$INSTDIR\IntegradorWS.exe.Config" TemWSConfig NaoTemWSConfig
TemWSConfig:
DetailPrint "TemWSConfig:1"
DetailPrint "TemWSConfig:2"
DetailPrint "TemWSConfig:3"
NaoTemWSConfig:
DetailPrint "NaoTemWSConfig:1"
; Clean up
Delete "$temp\IntegradorWS.exe.Config"
SectionEnd
This means the problem must be with the File instruction. Make sure you have called SetOutPath so it knows where to extract. File extraction can be skipped if you have changed SetOverwrite etc.

Resources