Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Filename AutoInc name (https://www.delphipraxis.net/122541-filename-autoinc-name.html)

mohfa 17. Okt 2008 21:01


Filename AutoInc name
 
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

MSSSSM 17. Okt 2008 21:25

Re: Filename AutoInc name
 
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

mohfa 17. Okt 2008 21:59

Re: Filename AutoInc name
 
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

MSSSSM 18. Okt 2008 21:18

Re: Filename AutoInc name
 
spontaneously ( :?: ):
FindFirst + FindNext with TSearchRec;

Sorry, no more time for you :( .

Apollonius 18. Okt 2008 21:25

Re: Filename AutoInc name
 
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.

Cyf 18. Okt 2008 21:43

Re: Filename AutoInc name
 
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;


Alle Zeitangaben in WEZ +1. Es ist jetzt 15:34 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