Registriert seit: 29. Mai 2002
Ort: Frankfurt
8.252 Beiträge
Delphi 2006 Professional
|
Re: Für jeden Tag eine Datei erzeugen
12. Jun 2006, 06:44
Hai ihr,
ich habe einfach auch mal etwas zusammen getippt
Eventuell ist das ja auch ein Lösungsansatz?
Delphi-Quellcode:
uses
Math;
const
myDateFormat = 'yyyy-mm-dd';
myTimeFormat = 'HH:mm';
function LogFileName(const ABaseName: string; ADateTime: TDateTime): string;
begin
Result := ABaseName + '_' + FormatDateTime(myDateFormat, ADateTime) + '.txt';
end;
procedure WriteToLog(const aLogBase, aLogText: string; aLastLog: TDateTime;
aStrings: TStrings = NIL);
var
LogName: string;
Mode: Cardinal;
LogFile: TFileStream;
LogLine: string;
LogTime: TDateTime;
begin
LogTime := now;
LogLine := FormatDateTime(Format('%s %s', [myDateFormat, myTimeFormat]), LogTime) +
' ' + aLogText;
LogName := LogFileName(aLogBase, LogTime);
ForceDirectories(ExtractFilePath(LogName));
Mode := IfThen(FileExists(LogName), fmOpenWrite, fmCreate);
LogFile := TFileStream.Create(LogName, Mode);
try
LogFile.Position := LogFile.Size;
LogFile.Write(LogLine[1], Length(LogLine));
LogFile.Write(sLineBreak, 2);
finally
LogFile.Free;
end;
if Assigned(aStrings) then
begin
if Trunc(LogTime) > Trunc(aLastLog) then
begin
aStrings.Clear;
end;
aStrings.Add(LogLine);
end;
aLastLog := LogTime;
end;
// Demo Aufruf
procedure TDemoForm.btn_testClick(Sender: TObject);
var
LastLog : TDateTime;
begin
WriteToLog('c:\logs\log_', Edit1.Text, LastLog, ListBox1.Items);
end;
Stephan B.
|
|
Zitat
|