Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy a file in InnoSetup PreProcessor section?

Tags:

inno-setup

I simply want to copy one file to a specified directory before the compilation begins.

Background: Im need to create some zip files. One of the files, to be included needs to be copied to the final zip location. (I can not zip it directly from the original location because it would keep the original path in the zip file.

I found out, that the preProcessor action is perfect for this. I tried to use windows copy command but it does not work. Is it possible to use a function from CODE section within the preCompiler section? How? Or is there a completely different way to achieve my target?

Thanks in advance.

like image 482
Christian Rockrohr Avatar asked Jan 29 '13 11:01

Christian Rockrohr


Video Answer


2 Answers

You can do this using the ISPP CopyFile() function.

#expr CopyFile("C:\SourceFolder\FileName.exe", "C:\TargetFolder\FileName.exe")

Note that it is not possible to use [Code] functions from ISPP, but you can use the native ISPP functions and macros. Calling Exec() with the copy command also fails as it's a feature of the command processor (cmd.exe) rather than an application on its own.

like image 198
Deanna Avatar answered Oct 21 '22 06:10

Deanna


Although I've already suggested to use a batch file for this sort of task, I've made a sample script, which processes all the *.tzs script files in a certain directory and for each of them tries to check if the file have been created (or to be more spefic, if exists). This of course assumes just the very basic use of the *.tzs scripting like shown in the help file. Here it is:

#define FindHandle
#define FindResult
#define ScriptFile
#define ScriptLine
#define ScriptHandle
#define ArchiveFile
; unique identifier for the target archive file name property member
#define ArchiveToken ".Archive="
; path to directory, where all the *.tzs script files will be processed
#define ScriptPath "d:\Development\__StackOverflow\14579484\"
; tool, which will be used for the *.tzs file processing mentioned above
#define ScriptTool "c:\Program Files (x86)\TUGZip\TzScript.exe"

; This procedure tries to extract the name of the archive, to be created from
; the line currently read from the just processed *.tzs script file. It first
; tries to search the ArchiveToken constant value in trimmed, space-less line
; of script. If it's found, and the script line contains also two quotes, the 
; file name is stored to the ArchiveFile variable, by which is later checked 
; the file existence after the script execution is done
#sub ParseScriptLine  
  #define SrcPos = Pos("""", ScriptLine)
  #define EndPos = RPos("""", ScriptLine)
  #define ParsedStr = Trim(StringChange(ScriptLine, " ", ""))

  #if (Pos(ArchiveToken, ParsedStr) != 0) && (SrcPos != 0) && (EndPos != 0) && (SrcPos != EndPos)
    #expr ArchiveFile = Copy(ScriptLine, SrcPos + 1, EndPos - SrcPos - 1)
    #expr ArchiveFile = StringChange(ArchiveFile, "\\", "\")
    #if ArchiveFile != ""
      #pragma message "ExecuteScriptFile: ArchiveFile=" + ArchiveFile
      #expr ExecuteScriptFile      
    #endif
  #endif        
#endsub

; This procedure opens the currently processed *.tzs script file and starts to 
; read it line by line to get the file name of the archive to be created
#sub ReadScriptFile
  #pragma message "ReadScriptFile: ScriptFile=" + ScriptPath + ScriptFile
  #for {ScriptHandle = FileOpen(ScriptPath + ScriptFile); \
    ScriptHandle && !FileEof(ScriptHandle); ScriptLine = FileRead(ScriptHandle)} \
    ParseScriptLine
  #if ScriptHandle
    #expr FileClose(ScriptHandle)
  #endif
#endsub

; This procedure is used for the script execution itself, here the TzScript.exe
; tool is called passing the name of the currently processed script file. After
; this tool is done, it's checked if the expected archive file has been created 
#sub ExecuteScriptFile
  #if Exec(ScriptTool, ScriptPath + ScriptFile) == 0
    #if FileExists(ArchiveFile) == 0
      #error The expected archive was not found. Compilation will abort now!
    #endif
  #else
    #error An error occured at script tool starting. Compilation will abort now!
  #endif
#endsub

; This is the file iteration "callback"
#sub ProcessFoundFile
  #expr ScriptFile = FindGetFileName(FindHandle)
  #expr ReadScriptFile
#endsub

; This procedure is used to find all the *.tzs script files in folder, specified
; by the ScriptPath constant path. All the *.tzs files found there are executed 
; by the command line tool specified by the ScriptTool constant 
#for {FindHandle = FindResult = FindFirst(ScriptPath + "*.tzs", 0); \
  FindResult; FindResult = FindNext(FindHandle)} \
  ProcessFoundFile
#if FindHandle
  #expr FindClose(FindHandle)
#endif

[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program
OutputDir=userdocs:Inno Setup Examples Output
like image 45
TLama Avatar answered Oct 21 '22 06:10

TLama