How can I delete files in a directory, exclude some files in that derectory under [UninstallDelete]

I am trying to write a script in InnoSetup using Ant example.

    <delete includeemptydirs="true">
        <fileset dir="${term.path}/configuration" excludes="*.rtf,*.property,accounts/**/*,log/*,**/*.p12"/>
    </delete>

      

But I don't understand how I can delete files in a directory excluding files by name pattern:

*.rtf,*.property,accounts/**/*,log/*,**/*.p12

      

". In the help file, I did not find parameter exclusion in Inno Setup [InstallDelete] sectoin.

+3


source to share


1 answer


I solved this problem by adding the "[Code]" section to my inno script. I am sure my problem can be solved in a more compact and better way.



[Code]

//Procedure checks whether the file extension 
//specified values or not. In the case of non-compliance,
//the file is deleted
procedure CompareAndRemove(const Path: String); 
begin 
    if (ExtractFileExt(Path) <> '.rtf') 
        and (ExtractFileExt(Path) <> '.p12')
        and (ExtractFileExt(Path) <> '.property')
        then  DelayDeleteFile(Path, 2);    
end; 

// Procedure compare Path of folder with given paths 
procedure CompareImportantFolders(const PathToCompare: String; const Path: String );
begin 
     if  (PathToCompare <> Path+'.') 
        and (PathToCompare <> Path+'..')
     then  DelTree(PathToCompare, True, True, True);
end;                       


// Procedure check a folder to important files inside
function isImportantFilesExist(Path: String): Boolean;
var
  FindRec: TFindRec;
begin
  if FindFirst(ExpandConstant(Path + '*'), FindRec) then
  begin 
    try
      repeat
        // If just file
      if FindRec.Attributes and FILE_ATTRIBUTE_DIRECTORY = 0         
        then
         begin 
             if ExtractFileExt(Path+FindRec.Name) = '.p12'
             then  
             Result := true;
         end;
      until not FindNext(FindRec);
    finally
      FindClose(FindRec);
    end; 
    end; 
   end;   


//Procedure runs on folder first level and deletes all files exclude
// files with special ext. (look at procedure "CompareAndRemove")
procedure CleanDirOutOfFiles(const Path: String);
var  
  FindRec: TFindRec;
begin
  if FindFirst(ExpandConstant(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;




// Procedure runs in folder and delete all folders include 
// itself
procedure CleanDirOutOfFolders(const Path: String);
var
  FilesFound: Integer;
  DirFound: Integer;
  FindRec: TFindRec;
begin //(1)
  if FindFirst(Path + '*', FindRec) then
  begin //(2)
    try
      repeat
        // If found file - Directory
      if FindRec.Attributes and FILE_ATTRIBUTE_DIRECTORY = 16         
        then
         begin    
          CompareImportantFolders(Path+FindRec.Name, Path)
         end; 
      until not FindNext(FindRec);
    finally
      FindClose(FindRec);
    end;
    end;
   end;

// Procedure clean folder out of unimportant 
// files and directories
procedure CleanDir(const Path: String);
var
  FilesFound: Integer;
  DirFound: Integer;
  FindRec: TFindRec;
begin //(1)
  FilesFound := 0;
  DirFound  := 0;
  if FindFirst(ExpandConstant(Path + '*'), FindRec) then
  begin //(2)
    try
      repeat
        // If found file - file
      if FindRec.Attributes and FILE_ATTRIBUTE_DIRECTORY = 0         
        then 
         begin
        CompareAndRemove(Path+FindRec.Name);
         end
      // If found file - directory
       else 
           begin
          if  (Path+FindRec.Name <> Path+'.') 
          and (Path+FindRec.Name <> Path+'..')
          and (Path+FindRec.Name <> Path+'log')
          and (Path+FindRec.Name <> Path+'accounts')
          then 
              begin  
                  CleanDirOutOfFolders(Path+FindRec.Name+'\'); 
                  CleanDirOutOfFiles(Path+FindRec.Name+'\');
                      if not isImportantFilesExist(Path+FindRec.Name+'\')
                      then
                          begin
                            DelTree(Path+FindRec.Name, True, True, True);
                          end;
              end;
          end;                   
      until not FindNext(FindRec);
    finally
      FindClose(FindRec);
    end;
    end;
   end;

      

+1


source







All Articles