Arquivos - criando backups em cascata |
Top Previous Next |
|
// Faz backup em cascata deletando o ultimo. // Exemplo de uso: desejando fazer o backup de CLIENTES.DBF: // GerenciaBackup('C:\DADOS','CLIENTES.DBF', 20) -> o programa pega o CLIENTES.DBF // renomeia para CLIENTES.000 e caso este mesmo já exista ele renomeia para // CLIENTES.001, assim até 020, sendo este último deletado.
// Exemplo: GerenciaBackup('C:\temp1', 'Planilha.zip'); procedure GerenciaBackup(const Pasta, Arquivo: string; MaxBackups: Integer = 9); var P: string; I: Integer; begin P := IncludeTrailingBackslash(Pasta); // Gerencia o backup em cascata for I:= MaxBackups downto 0 do if FileExists(P + ChangeFileExt(Arquivo,'.' + Format('%.3d',[I]))) then if I = MaxBackups then DeleteFile(PChar(P + ChangeFileExt(Arquivo,'.' + Format('%.3d', [MaxBackups])))) else RenameFile(P + ChangeFileExt(Arquivo, '.' + Format('%.3d',[I ]) ), P + ChangeFileExt(Arquivo, '.' + Format('%.3d',[I+1]) )); // Renomeia o arquivo RenameFile(P + Arquivo, P + ChangeFileExt(Arquivo, '.000')); end; |