AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Filename AutoInc name

Ein Thema von mohfa · begonnen am 17. Okt 2008 · letzter Beitrag vom 18. Okt 2008
Antwort Antwort
mohfa

Registriert seit: 11. Feb 2007
97 Beiträge
 
Delphi 7 Enterprise
 
#1

Filename AutoInc name

  Alt 17. Okt 2008, 21:01
Hi every body , my application needs to Create each time - after a specific event - a Log file , But what i want is that the new created Log file name will be like this : 00001.log // then 1 will be auto-inc each time the new log file is created .

So for the first execution my application will ensure that the 00001.log already exists , if this Log file (00001.log ) exists then it will create a new log file 00002.log and so on ( the 00001 will be auto-incremented like this 00002 or 00003 ...etc )otherwise it will create 00001.log .


as a summary : if 00001.log exits then Create a new file 00002.log , if 00002.log then a new with 00003.log ...etc . but if 00001.log doesn't exist then create 00001.log .



many thanks
  Mit Zitat antworten Zitat
Benutzerbild von MSSSSM
MSSSSM

Registriert seit: 18. Apr 2008
223 Beiträge
 
Delphi 7 Professional
 
#2

Re: Filename AutoInc name

  Alt 17. Okt 2008, 21:25
Hello,

Delphi-Quellcode:
var log,logs:Textfile;
lastincstr,s:string;
begin
  AssignFile(logs,ExtractFilePath(ParamStr(0))+'log.last');
  if (not FileExists(ExtractFilePath(ParamStr(0))+'log.last'))
  begin
    Rewrite(logs);
    Write(logs,'1');
    Reset(logs);
  end
  else ReSet(logs);
  Read(logs,lastincstr);
  s:=lastincstr;
  lastincstr:=FOrmat('%.6d',[strtointdef(lastincstr,1)]);
  AssignFile(log,ExtractFilePath(ParamStr(0))+lastincstr+'.log');
  //Write Log
  CloseFile(log);
  Rewrite(logs);
  Write(logs,inttostr(strtointdef(s,0)+1));
end;
untested.

Marius
Marius
  Mit Zitat antworten Zitat
mohfa

Registriert seit: 11. Feb 2007
97 Beiträge
 
Delphi 7 Enterprise
 
#3

Re: Filename AutoInc name

  Alt 17. Okt 2008, 21:59
Thank you MSSSSM , It works .
But if i delete the 00001.log and the last log number in the Log.last is 5 it continue to create 00006.log

The application must ensure the exact last log number .


Again many thanks
  Mit Zitat antworten Zitat
Benutzerbild von MSSSSM
MSSSSM

Registriert seit: 18. Apr 2008
223 Beiträge
 
Delphi 7 Professional
 
#4

Re: Filename AutoInc name

  Alt 18. Okt 2008, 21:18
spontaneously ( ):
FindFirst + FindNext with TSearchRec;

Sorry, no more time for you .
Marius
  Mit Zitat antworten Zitat
Apollonius

Registriert seit: 16. Apr 2007
2.325 Beiträge
 
Turbo Delphi für Win32
 
#5

Re: Filename AutoInc name

  Alt 18. Okt 2008, 21:25
By using FileExists or FindFirst/FindNext you create a race condition if another application tries to open the file at the same time. If you want to do this properly, you should use CreateFile with CREATE_NEW and check for the return value.
Wer erweist der Welt einen Dienst und findet ein gutes Synonym für "Pointer"?
"An interface pointer is a pointer to a pointer. This pointer points to an array of pointers, each of which points to an interface function."
  Mit Zitat antworten Zitat
Cyf

Registriert seit: 30. Mai 2008
407 Beiträge
 
Lazarus
 
#6

Re: Filename AutoInc name

  Alt 18. Okt 2008, 21:43
Something like this should work, but if you have a large number of log files, it may be an ineffective way to iterate, also you have to check for the maximal value of Cardinal.

Delphi-Quellcode:
procedure CreateLog;
var i: Cardinal;
begin
  i:= 1;
  while FileExists(IntToStr(i)+'.log') do //use format for left-hand zeros
  begin
    inc(i);
  end;
  //CreateFile ...
end;
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 14:49 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz