4

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?

Dan
  • 224
  • 2
  • 11
  • By which pattern do you want to search that folder ? Also, *first folder* is pretty wide term. First in which case ? – TLama Jul 16 '14 at 14:58
  • I guess it doesn't really matter - I expect there to be only one folder – Dan Jul 16 '14 at 15:16

1 Answers1

5

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;
TLama
  • 75,147
  • 17
  • 214
  • 392