Inno Setup:在Code部分中递归复制文件夹,子文件夹和文件

时间:2015-10-28 13:08:34

标签: inno-setup pascalscript

有没有办法在代码部分中浏览和递归复制/移动目录的所有文件和子目录? (PrepareToInstall

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

Files部分将在以后需要时执行。

1 个答案:

答案 0 :(得分:9)

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

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;

添加您需要的任何过滤。了解如何过滤...

有关使用示例,请参阅我对问题的回答:

相关问题