0

I am trying to write a script(am new to pascal-script and innosetup), Post installation it has to delete all the files in a directory excluding a specific file name.

[Code]

procedure CompareAndRemove(const Path: String); 
begin 
Log('checking file path  : ' + Path);
    if (ExtractFileExt(Path) <> 'setup-0.1.1.2.exe') 
        then  DelayDeleteFile(Path, 2);    
end; 

procedure CleanDirOutOfFiles();
var  
  Path, FilePath: string;
  FindRec: TFindRec;
begin
   Path := ExpandConstant('{{app}\{#test}\recurring}');
  if FindFirst(Path + '*', FindRec) then
  begin
    try
      repeat
        // if just File
      if FindRec.Attributes and FILE_ATTRIBUTE_DIRECTORY = 0         
        then
         begin
             CompareAndRemove(Path+FindRec.Name);
         end;
      until not FindNext(FindRec);
    finally
      FindClose(FindRec);
    end;
    end;
   end;

The code has to delete all the files in the recurring directory except setup-0.1.1.2.exe. so how can I do this. As if now it is not deleting anything.

    [Code]
procedure DelTreeExceptSavesDir(Path: string);
var
  FindRec: TFindRec;
  FilePath: string;
begin
  if FindFirst(Path + '\*', FindRec) then
  begin
    try
      repeat
        if (FindRec.Name <> '.') and (FindRec.Name <> '..') then
        begin
          FilePath := Path + '\' + FindRec.Name;
          if FindRec.Attributes and FILE_ATTRIBUTE_DIRECTORY = 0 then
          begin
            if DeleteFile(FilePath) then
            begin
              Log(Format('Deleted file %s', [FilePath]));
            end
              else
            begin
              Log(Format('Failed to delete file %s', [FilePath]));
            end;
          end
        end;
      until not FindNext(FindRec);
    finally
      FindClose(FindRec);
    end;
  end
    else
  begin
    Log(Format('Failed to list %s', [Path]));
  end;
end;


procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
var
  SavePath: string;
begin
  SavePath := ExpandConstant('{app}\{#test}\recurring');

  if CurUninstallStep = usUninstall then
  begin
      DelTreeExceptSavesDir(SavePath);
    end;
  end;

I have modified a bit Inno Setup - Delete whole application folder except for data subdirectory since i just want to delete files. Compiler is not complaining anything but I am not getting how to mention that specific file name here

Trang D
  • 333
  • 5
  • 16
  • 1
    1) `ExpandConstant('{{app}\{#test}\recurring}');` => `ExpandConstant('{app}\{#test}\recurring\');` 2) `ExtractFileExt` => `ExtractFileName` 3) Use `Log` function or debugger to see what's going on. – Martin Prikryl Nov 04 '19 at 16:35
  • Also see [Inno Setup - Delete whole application folder except for data subdirectory](https://stackoverflow.com/q/36491213/850848). – Martin Prikryl Nov 04 '19 at 16:35
  • @MartinPrikryl Have replaced these two things `ExpandConstant('{app}\{#test}\recurring\')` & `ExtractFileName`. Added Log's but I dont see any thing in console. It is not deleting anything.. Do I have to mention isspostinstall ? – Trang D Nov 04 '19 at 16:47
  • Show us your code including the `Log` calls and relevant log file. – Martin Prikryl Nov 04 '19 at 17:59

1 Answers1

1
function DeleteReqdFiles(FileNameToSkipDelete: String; WildCharPath: String): Boolean;
 var
  FindRec: TFindRec;   
  FilesFound: Integer;
  FilePath: String;
  ResultVal: Integer;
  Path: String;
begin
  FilesFound := 0
  Path:=ExpandConstant('{app}')
  ResultVal:=FilesFound
  if FindFirst(Path + '\*' + WildCharPath + '.*' , FindRec) then
  begin
   try
     repeat
        if (FindRec.Name <> '.') and (FindRec.Name <> '..') and (FindRec.Name <> FileNameToSkipDelete) then
        begin
         FilesFound := FilesFound + 1;
         ResultVal:=FilesFound
         FilePath := Path + '\' + FindRec.Name;
         DelTree(FilePath,False, True, False);
         end;
      until not FindNext(FindRec);
     finally
      FindClose(FindRec);
    end;     
  end
end;

Hi i guess i am a little late posting this. But i was also stuck with this same issue and i tried this way and its working.

FileNameToSkipDelete : Its the name of the file you want to skip

WildCharPath: In case you have a path C:\Program Files (x86)\Test\New and in this folder you have many files, some files are having few chars same like SW.SL.Test.dll,SW.SL.New.dll, SW.SL.KL.dll, SW.SL.JK.dll and there are other files like SP.SL.HH.dll, SP.SL.KJ.dll . Then i may be interested to delete a particular file that is SW.SL.KL.dll and want to search within that SW.SL then FileNameToSkipDelete will be SW.SL.KL.dll and WildCharPath will be SW.SL. So only the files within that matching WildCharPath will be searched.

I am trying to make it more generic by trying to input array of strings that can be skipped but for immediate requirement its now taking only one filename to be skipped.

Hope it solves your issue.

Jayashree Shetty
  • 1,695
  • 2
  • 11
  • 8