Uninstall user files

From ISXKB

Jump to: navigation, search

This code was provided by Abel G. Perez
Sometimes your application creates files on the fly and you would like those files deleted when the application is uninstalled. Additionally, you would like to prompt the user if they would like the files deleted. This code will do just that with several example files and also with a developer-specified wildcard. The code was put together with the help of the Inno newsgroup and was tested with InnoSetup5. The code can also be used in other areas as a base to enumerate files that match a wildcard. Updated to deal with a multi-language setup.

[Languages]
Name: en;  MessagesFile: "compiler:Default.isl"
Name: deu; MessagesFile: "compiler:Languages\German.isl"
Name: es;  MessagesFile: "compiler:Languages\Spanish.isl"

[Code]
const
    NL = #13#10;    //carriage return and line feed

//*********************************************************************************
//gets language used in the setup. returns language code in lower case
//*********************************************************************************
function GetLangCode() : String;
begin
  Result := Lowercase( ExpandConstant('{language}') )
end;

//**************************\\\
//***uninstallation stuff***\\\
//**************************\\\

//confirm file deletion and then delete the file
function DeleteOneFile(sFullFilePath: String): Boolean;
var
  iAns: Integer;
  SucInDel: Boolean;
  sTxt: String;

begin
  //get language specific text strings
  case GetLangCode of
    'es','es2': //spanish
        sTxt := 'Este archivo de usuario todavía está presente y ya no es necesario por el programa.' +NL+ '¿Te gustaría borrar?'   ;
    'deu':       //german
        sTxt := 'Dieser Benutzer Datei noch vorhanden ist und wird nicht mehr von dem Programm benötigt werden.' +NL+ 'Möchten Sie es löschen?' ;
  else          //en = the default is english or other unknown language
        sTxt := 'This user file is still present and is no longer needed by the program.' +NL+ 'Would you like to delete it?'  ;
  end;

  iAns := MsgBox(sFullFilePath +NL+NL+ sTxt, mbConfirmation, MB_YESNO or MB_DEFBUTTON2);
  if iAns = IDYES then SucInDel := DeleteFile(sFullFilePath);
end;


//*********************************************************************************
//cycles through all files that match a spec and then
//asks the user if they would like to delete them.
//*********************************************************************************
procedure DeleteManyFiles(sPattern: String);
var
 PathD: String;
 FileD: String;
 ExistsD: Boolean;
 FindRec: TFindRec;

begin

 //test to see if a files matching a pattern still exists. if they do exist then we
 //ask the user if they want to delete each one
 PathD := AddBackslash(ExpandConstant('{app}'))
 if FindFirst(ExpandConstant(PathD + sPattern), FindRec) then begin
   try
     repeat
       //only check files and not directories
       if FindRec.Attributes and FILE_ATTRIBUTE_DIRECTORY = 0 then
         //we found one file that matches our wildcards
         begin

            //FindFirst returns the name of the file with no path so must account for that
            FileD := PathD + FindRec.Name
            ExistsD := FileExists(FileD);

            //ask to delete the found file
            if ExistsD = true then DeleteOneFile(FileD);
         end;
     until not FindNext(FindRec);
   finally
     FindClose(FindRec);
   end;
 end;
end;

//*********************************************************************************
//deletes (with prompt) three individual files.
//deletes (with prompt) all files that match a pattern
//*********************************************************************************
procedure DeleteUserFiles();
var
 FileA: String;
 FileB: String;
 FileC: String;
 ExistsA: Boolean;
 ExistsB: Boolean;
 ExistsC: Boolean;

begin
 //test to see if the INI file and LOG files still exist
 FileA := ExpandConstant('{app}\MyApp.ini');
 FileB := ExpandConstant('{app}\MyApp.log');
 FileC := ExpandConstant('{app}\Process.log');
 ExistsA := FileExists(FileA);
 ExistsB := FileExists(FileB);
 ExistsC := FileExists(FileC);

 //ask to delete the INI and LOG files
 if ExistsA = true then DeleteOneFile(FileA);
 if ExistsB = true then DeleteOneFile(FileB);
 if ExistsC = true then DeleteOneFile(FileC);


 //test to see if MDB files still exists. the MDB files are generated based on 
 //user ID so we cycle through all the existing databases in the app folder and
 //ask the user if they want to delete each one
 DeleteManyFiles('??????XX.mdb');
 DeleteManyFiles('??????YY.mdb');
 DeleteManyFiles('??????ZZ.mdb');
end;

//*********************************************************************************
//this is the main calling point when the uninstall begins. we do this because the
//folder will be automatically be deleted at the END of this step, IF the folder no
//longer contains any files
//*********************************************************************************
procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
begin
 case CurUninstallStep of
   usUninstall:
     begin
       DeleteUserFiles;
     end;
 end;
end;

Personal tools
Ads: