Beranda > Delphi > How to get safe unique name

How to get safe unique name

The First function (GetUniqueStr) creates an unique string by very safe way. There is no chance to get two identical strings in your life. The Seconds function (GetUniqueFName) returns unique long filename. Last function returns date and time string when unique string was created.

{somerwhere on startup}
Randomize;
...
Uses SysUtils;
function GetUniqueStr : string;
var 
  TimeMS : comp;
begin
  TimeMS := TimeStampToMSecs(DateTimeToTimeStamp(now));
  Result := FormatFloat('0000000000000000', TimeMS )
            + FormatFloat('0000', int(10000*Random))
            + IntToHex(round(65535*Random),4)
            + FormatFloat('0000', int(10000*Random));
end;
 
function GetUniqueFName(Path, Suffix : string): string;
begin  
  if Path[length(Path)] <> '\' then
    Path := Path + '\';
 
  if Suffix[1] <> '.' then 
    Suffix := '.' + Suffix;
 
  Result := Path + GetUniqueStr + Suffix;
  // If you are maniac you can remove brackets...
  // but I think that it's obsolete and when Path will
  // be wrong the function will crash...
 
  {
  if FileExists(Result) then
     Result := GetUniqueFName(Path, Suffix);
  } 
end;
 
function DTstrFromUniqe(UniqueStr : string) : string;
var 
  TimeMS : comp;
  Stamp  : TTimeStamp;
begin
  TimeMS := StrToFloat(copy(UniqueStr, 1, 16));
  TimeSt := MSecsToTimeStamp(TimeMS);
  Result := DateTimeToStr(TimeStampToDateTime(Stamp));
end;
Iklan
Kategori:Delphi
  1. Belum ada komentar.
  1. No trackbacks yet.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: