Friday, February 8, 2013

Inno Setup: Creating a scheduled Log Rotator

Setting up a rotation mechanism for log files on a Windows machine can be an interesting problem. This could be real easy on Linux using CRON but on Windows you may find the a tool called waRmZip (download from sourceforge or look at http://winadmin.forret.com/) very useful. It works very well and you can set up a scheduler job to fire it at a predetermined time. To do this kind of thing in Inno Setup, use the code below:

function GetDoLogRotateTaskAddParams(Default: String):String;
begin
  // This will use the Operation System SYSTEM account for running the task
  result := '/Create /RL HIGHEST /F /TN "MODULE Log Rotate" /SC DAILY /ST 23:59 /RU "" /TR "' + ExpandConstant('{sys}\cscript.exe \"{#InstallHomeDir}\Tools\waRmZip.wsf\" C:\Logs /gt:1MB /ma:1 /md:\"C:\Logs\Backup-$DAY\" /q');
  if Length(LogDeleteTime) > 0 then begin
    result := result + ' /da:'+LogDeleteTime;
  end;
  result := result + '"';  
end;

This assumes that the waRmZip is installed using the following command:

Source: waRmZip.wsf; DestDir: {#InstallHomeDir}\Tools;

A Task scheduler can be created using the following "Run" command in Inno Setup:

Filename: "schtasks.exe"; Parameters: "{code:GetDoLogRotateTaskAddParams}"; Description: " Log Rotate Task"; Flags: runhidden; Check: GetDoLogRotateTask;

No comments: