Archive: A simple installation script


A simple installation script
  Hello all,

I am a newcomer. In order to learn I want to develop my first installer application.

1. Define the installation directory \InstallationDir
2. Unzip the contents of the installation file to the \Temp
3. Rename a file in \InstallationDir like this
File1.exe as File1-mm-dd-yy.exe where mm-dd-yy is the "Date Modified" date given in File Explorer
4. Copy File1.exe from the \Temp directory to \InstallationDir

I would appreciate if help me to create a script to this. Thank you very much.


Look up these in the manual:
1. InstallDir
2. File
3. Rename and GetTime
4. CopyFiles

You really should do a bit of research.

Stu


A Simple Installation Script
  Hello Stu,

Thank you for your help. I searched a lot. But I am new to this syntax and the logic of programming. Anyhow, I wrote the following script. But I ma getting an error message that the "Error: command StrCpy not valid outside Section or Function". What I am trying to do here is to rename an existing file (FileToInstall) in the installation directory and copy it to this directory. The name of the old file after the renaming would be FileToInstall-MM-DD-YY.exe where the DD, MM, YY come form File Explorer "Date Modified" field. The "GetTime" function should find these parameters. And StrCopy should create the new file name, and "MoveFile" macro do the renaming.

Thank you.

Cem

----------------------------------------

!include 'FileFunc.nsh'
!insertmacro Locate

Var /GLOBAL NewFileName
Var /GLOBAL FileToInstall
Var /GLOBAL switch_overwrite
StrCpy $switch_overwrite 0
!include 'MoveFileFolder.nsh'

# define the name of the installer
outfile "TMI.exe"

# define the file to install
FileToInstall "MyFile.exe"

# define the directory to install to
installDir c:\Dir\Dir2\Bin

# find out when the earlier version was last modified
${GetTime} FileToInstall "M" $var1 $var2 $var3 $var4 $var5 $var6 $var7

# setup new file name
StrCpy $NewFileName FileToInstall "-" $var1 "-" $var2 "-" $var3 ".exe"

# rename oldfile to newfile
!insertmacro MoveFile "$INSTDIR\FileToInstall" "$INSTDIR\$NewFileName"

# default section
section

# define the output path for this file
setOutPath $INSTDIR

file FileToInstall

sectionEnd


Originally posted by Cem
"Error: command StrCpy not valid outside Section or Function"
I would think that this error is extremely self-explanatory. The command is not valid outside of a section or function. So, move it into a section. (In your case, move it into THE section.) The same applies to the GetTime and MoveFile macros.

Also there's no such command as FileToInstall and no such macro called MoveFile. You have to use what is provided. You can't just make things up.

Stu


Hello Stu,

FileToInstall is a user variable. MoveFile is a user supplied function from the NSIS site which I placed into the Include directory of NSIS. At the moment I am getting an error from the GetTime function, so I want to find out the value of the $FileToInstall variable which should be hold the name of the file, and after the function returns the value of $var1 which is DD of the file. If I get the parameters, then I will create a new name for the file name to rename the existing file.

I also wante dto DumpSate plugin to debug the values of the variables but I am getting an error message as soon as DumpState is defined.

# Dumpstate::debug

!include 'FileFunc.nsh'
!insertmacro Locate
#!include 'MoveFileFolder.nsh'

Var /GLOBAL NewFileName
Var /GLOBAL FileToInstall
Var /GLOBAL switch_overwrite

# define the name of the installer
outfile "TMI.exe"

# define the directory to install to
installDir c:\Dir1\Dir2\Bin

# default section
section

# define the output path for this file
setOutPath $INSTDIR

StrCpy $switch_overwrite 0

# define the file to install
StrCpy $FileToInstall "File1.exe" # here I define $FileToInstall

MessageBox MB_OK "$FileToInstall" # here I want to see the value of $FileToInstall var.

# find out when the earlier version of $FileToInstall was last modified
${GetTime} FileToInstall "M" $var1 $var2 $var3 $var4 $var5 $var6 $var7

MessageBox MB_OK $var1

# setup new file name
StrCpy $NewFileName $FileToInstall "-" $var1 "-" $var2 "-" $var3 ".exe"

# rename oldfile to newfile
# !insertmacro MoveFile "$INSTDIR\$FileToInstall" "$INSTDIR\$NewFileName"

# file $FileToInstall

sectionEnd


You can use Rename to move files. You cannot use variables to define files to install. Variables are for run time use, and the File instruction (among others) is a compile time instruction. If you want constants, use !define.

Perhaps it will help if you describe further what you are trying to do with GetTime. Are you renaming an existing file on the user's system to a name consisting of its modified date?

Edit: To use plug-ins (such as Dumpstate), put the DLL file in the Plugins folder. Dumpstate will be no use to you anyway as it doesn't show custom variables (Var) but only default ones ($0-$9, $R0-$R9).

Stu


A Simple Installation Script
  Hello Stu,

I want to copy File1.exe to the installation directory. But before doing that I want to rename the existing (old version) of this file in the installation directory to something like File1-12-26-10.exe where 12-26-10 come from the File Explorer's Date Modified field. After the file is renamed, I can then copy File1.exe to this directory so that the original file is not overwritten. This way the user can go back to the older version of the program.

If DumpState does not work what else can I use to debug all the variables in the script? MessageBox does as I coded does not seem to work.


Hey, Cem

if I understood you right you can try this. If you modify it a little, it should work for what you need to do.

!include "FileFunc.nsh"



>Name "My File"
>OutFile "TMI.exe"
>InstallDir "Somewhere\on\your\comp"

>ShowInstDetails show

Section Install
>;set out the path and place the file there
SetOutPath"$INSTDIR" #or any other Dir you want to start from
File "this_is_the_file_you_want_to_rename.ext" #you should have it in your your folder on compile

>;get the time when the file was last modified
${GetTime} "path\to\file.ext" "M" $1 $2 $3 $4 $5 $6 $7
Pop$1
Pop$2
Pop$3

>; rename the file
Rename"path\to\old\file.ext" "path\to\new\file$1-$2-$3.ext" #quotes are just in case you have spaces in the path

>;After renaming it, copy the file$1-$2-$3.ext to whereever you want
CopyFiles "path\to\file$1-$2-$3.ext" "New\path"

>SectionEnd
>
Hope it helps. It's simple and you can read it all in the manual. Very easy to find.

A Simple Installation Script
  Hello Vankaa,

Thank you very much. I will study this soon and let you know. Well appreciated.


RE:A Simple Installation Script
  Hello Vanka,

I tried the script you sent me. Here is what I want to with this script:

1. There is a file in the $InstDir called TM.exe. This is the old file, created on 8/31/2010.
2. The new TM.exe is created on 9/10/2010 and this is in a temp directory, hopefully compressed in TMI.exe.
3. Before copying this new file into $InstDir, I want to rename the TM.exe in $InstDir to TM-8-31-2010.exe.
4. Then copy the new TM.exe to $Instdir.

When I run the script below, this is what happens:

1. The installation file created by this script does not include TMI.exe.
2. The TM.exe in $InstDir is successfully renamed to TM-8-31-2010.exe.
3. The new file TM.exe is not copied to $InstDir.
4. I commented out the copy commands since the File command should unpack the new TM.exe and copy that to $InstDir, shouldn't it?
5. I moved the File operation after the Rename operation so that the old TM.exe in the $InstDir is not overwritten before it is renamed.
5. What is the use for the Name command?

Thank you for your help.

---------------------------------

!include "FileFunc.nsh"

Name "My File"
OutFile "TMI.exe"
InstallDir "C:\DirToInstall\bin"

ShowInstDetails show

Section Install

;get the time when the file was last modified
${GetTime} "C:\DirToInstall\bin"\TM.exe" "M" $1 $2 $3 $4 $5 $6 $7
Pop $1
Pop $2
Pop $3

; rename the file
Rename "C:\DirToInstall\bin\TM.exe" "C:\DirToInstall\bin\TM-$2-$1-$3.exe" #quotes are just in case you have spaces in the path

;set out the path and place the file there
SetOutPath $INSTDIR #or any other Dir you want to start from
File TM.exe

;After renaming it, copy the file$1-$2-$3.ext to whereever you want
; CopyFiles "C:\Biopticon\TumorManager\bin\TumorManager-$1-$2-$3.exe" "C:\Biopticon\TumorManager\bin\"

SectionEnd


hello again,
I think you should explain it a little better so that I can help you better. By what I understand
1. You want your installer to place two files:
A - in "C:\DirToInstall\bin" with created date 8/31/2010 and name TM.exe (or you assume that the file is already present in the "C:\DirToInstall\bin" location)
B - in "anotherFolder" with created date 9/10/2010 and name TM.exe aslo
then you want to rename file A to TM8-31-2010.exe and after that move file B to the "C:\DirToInstall\bin" location.

2. Name - is the installer name, usually shown on welcome page.
3. OutFile - is the .exe file that comes out after compiling your script. you do not need to include TMI.exe, this is the file you run.

About the code now: what you can also do is first rename the TM.exe(A) file and then just do


SetOutPath "INSTDIR"         

>File "TM.exe"
Since this will unpack the new TX.exe(B) file straight to the install directory ( this you can do assuming the file you rename already exists in the install directory as I said in 1. above)

or you can just do what the script originally does, but lose the last "\" like this
After renaming it, copy the file$1-$2-$3.ext to whereever you want

CopyFiles "C:\Biopticon\TumorManager\bin\TumorManager-$1-$2-$3.exe" "C:\Biopticon\TumorManager\bin"
hope it helps. Let me know, if I miss understood something.

Re:The Simple Installer Script
  Hello Vanka,


Before the installation, installation directory has the following file:

Name Size Date Modified
TM.exe 113,876 KB 8-31-2010

After the installation

Name Size Date Modified
TM.exe 124,456 KB 9-10-2010
TM-8-31-2010.exe 113,876 KB 8-31-2010

So the installer will
1. First rename the existing TM.exe in the installation directory to TM-8-31-2010.exe
2. Install the new TM.exe from TMI.exe to Installation directory

Where the new TM.exe will be part of TMI.exe installation file which the user will run. So the user runs TMI.exe installation (Setup file which has the new TM.exe in compressed form). The script renames the old TM.exe in the installation directory to something else and copies the uncompressed TM.exe to the installation directory.


Re:A simple Installer
  Hello Vankaa,

Your script worked after I changed File $InstDir to File "$InstDir".

But I still do not get TMI.exe to include TM.exe as compressed file. so the I can send a single TMI.exe as installer file. The TM.exe is in the directory but compilation of teh script doe not create a TMI.exe file including TM.exe.

Thank you.

!include "FileFunc.nsh"

Name "My File"
OutFile "TMI.exe"
InstallDir "C:\InsToDir\bin"

ShowInstDetails show

Section Install

;get the time when the file was last modified
${GetTime} "C:\InsToDir\bin\TM.exe" "M" $1 $2 $3 $4 $5 $6 $7
Pop $1
Pop $2
Pop $3

; rename the file
Rename C:\InsToDir\bin\TM.exe" "C:\InsToDir\bin\TM-$2-$1-$3.exe" #quotes are just in case you have spaces in the path

;set out the path and place the file there
SetOutPath "$INSTDIR" #or any other Dir you want to start from
File TM.exe

SectionEnd


Cem, the File command does two things:
1) During compilation, it causes the referenced file (in your case, TM.exe) to be compressed into the installer exe.
2) During runtime, it extracts the compressed file to $OUTPATH.

So, if your script can compile, then your TM.exe will be compressed into TMI.exe. There's no way around it. If the compression fails, the compilation also fails. So if compilation succeeds, your file is definitely compressed, no doubt about it. What makes you think that the file isn't compressed?


You missing "". File "TM.exe" should do it. Make sure that you open and close your quotes in your script.

MSG is very right it should be all good. Now that I checked it works right.


Hello Afrow, Vankaa and MSG,

Thank you very much. Now I know much better about NSIS than few days ago.

Soon I will post questions about running scripts to modify MS SQLServer databases. But I need to study the examples.

Have a nice weekend.

Cem