Inno Setup find subfolder

2019-06-24 06:40发布

问题:

Is there anyway to get all (or just the first) subfolder in a directory? I'm trying to install my files into a subdirectory that has a dynamic name. It is not one of the constants available with Inno Setup. Is there anyway to find this subdirectory name?

回答1:

Well, to get name of a first found subfolder of a certain folder, no matter which one it is, you may use the following function:

[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program

[Code]
function TryGetFirstSubfolder(const Path: string; out Folder: string): Boolean;
var
  S: string;
  FindRec: TFindRec;
begin
  Result := False;
  if FindFirst(ExpandConstant(AddBackslash(Path) + '*'), FindRec) then
  try
    repeat
      if (FindRec.Attributes and FILE_ATTRIBUTE_DIRECTORY <> 0) and
        (FindRec.Name <> '.') and (FindRec.Name <> '..') then
      begin
        Result := True;
        Folder := AddBackslash(Path) + FindRec.Name;
        Exit;
      end;
    until
      not FindNext(FindRec);
  finally
    FindClose(FindRec);
  end;
end;

procedure InitializeWizard;
var
  S: string;
begin  
  if TryGetFirstSubfolder('C:\Folder', S) then
    MsgBox('The first found subfolder is: ' + S, mbInformation, MB_OK);
end;