Search subdirectories for Inno Setup DestDir

假装没事ソ 提交于 2019-12-06 16:54:35
  1. To pass the path from InitializeWizard to StampTargetDir, use a global variable. Though I suggest you better use CurStepChanged(ssInstall) instead of InitializeWizard, unless you need the path earlier than during the actual installation.

  2. Use recursion in GetFirstMatchingSubfolder.

  3. The cleanest solution is to run GetFirstMatchingSubfolder multiple times with specific roots (i.e. twice, for Adobe and for Acrobat)

  4. Use Check parameter.

The code can be like:

[Files]
Source: "C:\mydir\stamp.pdf"; DestDir: "{code:GetStampsFolderPath}"; \
    Check: WasStampsFolderFound; Flags: ignoreversion

[Code]

const
  StampsFolderName = 'Stamps';

var
  StampsFolderPath: string;

function WasStampsFolderFound(): Boolean;
begin
  Result := (StampsFolderPath <> '');
end;

function GetStampsFolderPath(Params: string): string;
begin
  Result := StampsFolderPath;
end;

function GetFirstMatchingSubfolderRecursively(const Path: string; Name: string; out Folder: string): Boolean;
var
  FindRec: TFindRec;
  FolderPath: string;
begin
  Result := False;
  Log(Format('Searching in %s', [Path]));

  if FindFirst(AddBackslash(Path) + '*', FindRec) then
  try
    repeat
      if (FindRec.Attributes and FILE_ATTRIBUTE_DIRECTORY <> 0) then
      begin
        FolderPath := AddBackslash(Path) + FindRec.Name;
        if CompareText(FindRec.Name, Name) = 0 then
        begin
          Result := True;
          Folder := FolderPath;
          Log(Format('Match: %s', [Folder]));
        end
          else
        if (FindRec.Name <> '.') and (FindRec.Name <> '..') then
        begin
          Result := GetFirstMatchingSubfolderRecursively(FolderPath, Name, Folder);
        end;
      end;
    until Result or (not FindNext(FindRec));
  finally
    FindClose(FindRec);
  end;
end;

procedure CurStepChanged(CurStep: TSetupStep);
var
  RootPath: string;
begin
  Log(Format('CurStepChanged %d', [CurStep]));

  if CurStep = ssInstall then
  begin
    if IsTaskSelected('pf') then
    begin
      // this should be pf32 or pf64 specifically,
      // depending on where Adobe installs the applications
      RootPath := ExpandConstant('{pf}\');
    end
      else
    if IsTaskSelected('local') then
    begin
      RootPath := ExpandConstant('{localappdata}\');
    end;

    if RootPath = '' then
    begin
      Log(Format('No task selected, will not search for %s', [StampsFolderName]));
    end
      else
    begin
      Log(Format('Searching for %s folder under %s', [StampsFolderName, RootPath]));

      if GetFirstMatchingSubfolderRecursively(RootPath + 'Adobe', StampsFolderName, StampsFolderPath) or
         GetFirstMatchingSubfolderRecursively(RootPath + 'Acrobat', StampsFolderName, StampsFolderPath) then
      begin
        Log(Format('Found %s folder at %s', [StampsFolderName, StampsFolderPath]));
      end
        else
      begin
        Log(Format('%s folder not found anywhere', [StampsFolderName]));
      end;
    end;
  end;
end;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!