5

我只是想在编译开始之前将一个文件复制到指定目录。

背景:我需要创建一些 zip 文件。需要将要包含的文件之一复制到最终的 zip 位置。(我不能直接从原始位置压缩它,因为它会将原始路径保留在 zip 文件中。

我发现 preProcessor 操作非常适合这个。我尝试使用 windows 复制命令,但它不起作用。是否可以在 preCompiler 部分中使用 CODE 部分的函数?如何?还是有完全不同的方式来实现我的目标?

提前致谢。

4

2 回答 2

4

您可以使用 ISPPCopyFile()功能执行此操作。

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

请注意,无法使用[Code]ISPP 中的函数,但您可以使用本机 ISPP 函数和宏。Exec()使用命令调用copy也会失败,因为它是命令处理器 ( cmd.exe) 的功能,而不是应用程序本身。

于 2013-01-29T11:59:59.213 回答
2

尽管我已经建议对此类任务使用批处理文件,但我制作了一个示例脚本,它处理某个目录中的所有 *.tzs 脚本文件,并为每个文件尝试检查文件是否有已创建(或更具体,如果存在)。当然,这只是假设 *.tzs 脚本的基本用法,如帮助文件中所示。这里是:

#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
于 2013-01-30T08:37:34.063 回答