Inno Setup:在代码部分递归复制文件夹、子文件夹和文件

Inno Setup: copy folder, subfolders and files recursively in Code section

有什么方法可以在代码段内浏览并递归copy/move目录的所有文件和子目录吗? (PrepareToInstall)

我需要忽略特定目录,但是使用 xcopy 它会忽略所有目录 /default/,例如,我只需要忽略特定目录。

Files 部分稍后在需要时执行。

要以编程方式递归复制目录,请使用:

procedure DirectoryCopy(SourcePath, DestPath: string);
var
  FindRec: TFindRec;
  SourceFilePath: string;
  DestFilePath: string;
begin
  if FindFirst(SourcePath + '\*', FindRec) then
  begin
    try
      repeat
        if (FindRec.Name <> '.') and (FindRec.Name <> '..') then
        begin
          SourceFilePath := SourcePath + '\' + FindRec.Name;
          DestFilePath := DestPath + '\' + FindRec.Name;
          if FindRec.Attributes and FILE_ATTRIBUTE_DIRECTORY = 0 then
          begin
            if FileCopy(SourceFilePath, DestFilePath, False) then
            begin
              Log(Format('Copied %s to %s', [SourceFilePath, DestFilePath]));
            end
              else
            begin
              Log(Format('Failed to copy %s to %s', [
                SourceFilePath, DestFilePath]));
            end;
          end
            else
          begin
            if DirExists(DestFilePath) or CreateDir(DestFilePath) then
            begin
              Log(Format('Created %s', [DestFilePath]));
              DirectoryCopy(SourceFilePath, DestFilePath);
            end
              else
            begin
              Log(Format('Failed to create %s', [DestFilePath]));
            end;
          end;
        end;
      until not FindNext(FindRec);
    finally
      FindClose(FindRec);
    end;
  end
    else
  begin
    Log(Format('Failed to list %s', [SourcePath]));
  end;
end;

添加您需要的任何过滤。查看 ... 是如何过滤的。


请注意,该函数不会创建根 DestPath。如果您不知道它是否存在,请将其添加到代码的开头:

if DirExists(DestPath) or CreateDir(DestPath) then

(那么递归DirectoryCopy调用之前的类似代码就变得多余了)


使用示例见我的问题回答:

  • .