Page 1 of 1

Create a unique filename

Posted: Sat Jun 23, 2018 10:44 pm
by unisoft
Hello,

Any one has a small function to create a unique filename ? similar to sys(3)

Regards

Re: Create a unique filename

Posted: Sun Jun 24, 2018 1:42 am
by Koverhage
Do you mean a function like this ?

Code: Select all

FUNCTION GetTempFileName_a(cPfad)
Local nDll, cRetVal := space(260), nRetVal := 0, nUnique := 0, cPrefix := "TFX"
nDll := DllLoad( "KERNEL32.DLL")
IF nDll != 0
   nRetVal := DllCall( nDll, DLL_STDCALL, "GetTempFileNameA", ;
                       cPfad, @cPrefix, nUnique, @cRetVal )
   DllUnload( nDll )
ENDIF
RETURN substr(cRetVal, 1, At(Chr(0),cRetVal)-1 )

Re: Create a unique filename

Posted: Sun Jun 24, 2018 2:15 am
by Eugene Lutsenko
I would use data from a computer timer like this to form a unique file name: mFileName = ALLTRIM(STR((DOY(DATE())-1)*86400+SECONDS()))

Re: Create a unique filename

Posted: Sun Jun 24, 2018 4:10 am
by Tom
TempFile() will create a filename unique for the directory you use as a parameter. If you want to create a filename which is unique for the whole world, use UuidToChar(UuidCreate()). This will create a unique string (also very handy for unique IDs without the need of counter files and stuff like this) which can be used as a filename.

Re: Create a unique filename

Posted: Sun Jun 24, 2018 5:54 am
by Victorio
I see, this is many handy functions, I use seconds and random() function ,
str(seconds())+randomint() like this

pomdbf1s:=cestatlac+"P"+padl(alltrim(str(int(seconds()) )),8,"0")+alltrim(str(RandomInt(1000,9999)))+".DBF"

Re: Create a unique filename

Posted: Thu Jun 28, 2018 12:16 am
by unisoft
Thank you All.

Re: Create a unique filename

Posted: Thu Jun 28, 2018 8:17 am
by rdonnay
pomdbf1s:=cestatlac+"P"+padl(alltrim(str(int(seconds()) )),8,"0")+alltrim(str(RandomInt(1000,9999)))+".DBF"
I use a similar function, but I start with today's date.
This will insure that there can be no duplicate.