Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pascal Scripting: Check if dest directory is empty and only print yes'/no warning if so

Tags:

inno-setup

I have written a setup. In this setup nothing happens, because I only concentrated on one area: The " wpSelectDir", where the user can choose a directory the setup should be installed in.

Now my code snippet should check, if ANYTHING exist in the chosen directory (any other folders, files, etc.). If so, the user gets warned if he still wants to continue, because everything in this directory will be removed then. If the user only created a new empty folder he should not get a warning, because nothing will be lost.

I have the code snippet already finished excepting the check if the directory is empty (I replaced it with "if 1=1 then".

Please just have a look:

[Setup]
AppName=Testprogramm
AppVerName=Example
AppPublisher=Exxample
DefaultDirName={pf}\C
DefaultGroupName=C
Compression=lzma
SolidCompression=yes

[Code]
function NextButtonClick(CurPageID: Integer): Boolean;
begin
   if CurPageID = wpSelectDir then // if user is clicked the NEXT button ON the select directory window; if1 begins here;
   begin
   if 1=1 then // if the directory is not empty; thx 4 help stackoverflow
   begin // warning with yes and no
    if MsgBox('The file contains data. This data will be removed permanently by continuing the setup?', mbConfirmation, MB_YESNO) = IDYES then //if 3 begins here
    begin
      Result := True;
    end
    else
    begin
      Result := False;
    end;
    end; // if2 ends here
  end // not CurPageID but any other begins here
  else
  begin
      Result := True;
  end; 
end;

I have already tried to use functions like "if FileExists( ...", but there I can not say " . " for any file. Also I was not successful using WizardDirValue and its properties.

I would really appreciate if someone could help me or give me a hint.

Thanks a lot, Regards C.

like image 846
user3197760 Avatar asked Jan 15 '14 16:01

user3197760


1 Answers

Use FindFirst/FindNext.

Example:

function isEmptyDir(dirName: String): Boolean;
var
  FindRec: TFindRec;
  FileCount: Integer;
begin
  Result := False;
  if FindFirst(dirName+'\*', FindRec) then begin
    try
      repeat
        if (FindRec.Name <> '.') and (FindRec.Name <> '..') then begin
          FileCount := 1;
          break;
        end;
      until not FindNext(FindRec);
    finally
      FindClose(FindRec);
      if FileCount = 0 then Result := True;
    end;
  end;
end;

Note: This function also returns False if directory doesn't exists

like image 141
anotheruser Avatar answered Nov 10 '22 02:11

anotheruser