Return a unique file file name - function?? 
Author Message
 Return a unique file file name - function??

How can I return a unique file name in clipper.  I have been programming in
foxpro off and on and I'm used to creating temporary tables using a
built-in function in foxpro called sys(3), which will return a unique file
name based on the date & time, such as "77401223"  I can then add a _ to
the beginning of the name, check if that table exists, if not, run my
iterations for that table, and then delete it.

How can I do the same thing in Clipper using dbcreate.  Obviously in a
multi-user environment i want to be concerned about hardcoding a file name.
 I'm sure there must be something out there that will do this for me and/or
someone has asked this question before. . .??

Thanks in advance!



-Rick



Mon, 18 Oct 1999 03:00:00 GMT  
 Return a unique file file name - function??


Quote:
>How can I return a unique file name in clipper.  I have been programming in
>foxpro off and on and I'm used to creating temporary tables using a
>built-in function in foxpro called sys(3), which will return a unique file
>name based on the date & time, such as "77401223"  I can then add a _ to
>the beginning of the name, check if that table exists, if not, run my
>iterations for that table, and then delete it.

>How can I do the same thing in Clipper using dbcreate.  Obviously in a
>multi-user environment i want to be concerned about hardcoding a file name.
> I'm sure there must be something out there that will do this for me and/or
>someone has asked this question before. . .??

>Thanks in advance!



>-Rick

Rick,

        Here is another idea I borrowed from somewhere, one day a long
time ago ...  With this one you can control the file's naming "style"
or just let it do it's own thing.  

        Get the unique name

                eg, UniqueFname( '.dbf', <path>, left( NetName(), 4 ) )

then dbcreate(), do whatever, close & delete.  

        You can substitute '.<any_three_letters>' for '.dbf' and still use
the file as a DBF so that is some more scope for "uniqueness".  Maybe
'.' + right( alltrim( NetName() ), 3 ) could be the extension??

[paste]

#include 'dons.ch'

/*------------------------------------------------------------------*/
function UniqueFname( cExtension, cPath, cPrefix )
   /*
      Generate a unique file name based on arguments or defaults
      Checks & creates in cPath or default directory if no cPath
   */
   local nCounter
   local cUniqName, cPict
   local cbName

   default cExtension  to  'U_Q'
   default cPath       to  set( _SET_DEFAULT )
   default cPrefix     to  'U'

   cPath   := cPath + '\'
   cPath   := strtran( cPath, '\\', '\' )
   cPrefix := cPrefix + '_'

   cExtension := '.' + cExtension
   cExtension := strtran( cExtension, '..', '.' )
   cPict      := replicate( '9', 8 - len( cPrefix ) )

   nCounter   := 9 * ( 10 ^ ( len( cPict ) - 1 ) )
   cbName     := { |n| cPath + cPrefix + TM( n, cPict ) + cExtension }
   cUniqName  := eval( cbName, nCounter )

   //  just keep checking until you find an unused file name    
   while file( cUniqName )
      cUniqName := eval( cbName, ++nCounter )
   endwhile

   return cUniqName

[end paste]

Hope this may be of some use to you.  I have been using this to create
unique files to hold terminal-specific information in a data directory
on a server.

Regards

Don Andersen
RAT Software
Database Programming, Accounting Solutions, PC Support
Phone +61 07 3881 2432
QUEENSLAND AUSTRALIA

"I never forget anything I remember"



Mon, 18 Oct 1999 03:00:00 GMT  
 Return a unique file file name - function??

Hey Thanks Don! That's exactly what I needed!  -Rick



Mon, 18 Oct 1999 03:00:00 GMT  
 Return a unique file file name - function??

Quote:


> >How can I return a unique file name in clipper.  I have been programming in
> >foxpro off and on and I'm used to creating temporary tables using a
> >built-in function in foxpro called sys(3), which will return a unique file
> >name based on the date & time, such as "77401223"  I can then add a _ to
> >the beginning of the name, check if that table exists, if not, run my
> >iterations for that table, and then delete it.

> >How can I do the same thing in Clipper using dbcreate.  Obviously in a
> >multi-user environment i want to be concerned about hardcoding a file name.
> > I'm sure there must be something out there that will do this for me and/or
> >someone has asked this question before. . .??

> >Thanks in advance!



> >-Rick
> Rick,

>         Here is another idea I borrowed from somewhere, one day a long
> time ago ...  With this one you can control the file's naming "style"
> or just let it do it's own thing.

>         Get the unique name

>                 eg, UniqueFname( '.dbf', <path>, left( NetName(), 4 ) )

> then dbcreate(), do whatever, close & delete.

>         You can substitute '.<any_three_letters>' for '.dbf' and still use
> the file as a DBF so that is some more scope for "uniqueness".  Maybe
> '.' + right( alltrim( NetName() ), 3 ) could be the extension??

> [paste]

> #include 'dons.ch'

> /*------------------------------------------------------------------*/
> function UniqueFname( cExtension, cPath, cPrefix )
>    /*
>       Generate a unique file name based on arguments or defaults
>       Checks & creates in cPath or default directory if no cPath
>    */
>    local nCounter
>    local cUniqName, cPict
>    local cbName

>    default cExtension  to  'U_Q'
>    default cPath       to  set( _SET_DEFAULT )
>    default cPrefix     to  'U'

>    cPath   := cPath + '\'
>    cPath   := strtran( cPath, '\\', '\' )
>    cPrefix := cPrefix + '_'

>    cExtension := '.' + cExtension
>    cExtension := strtran( cExtension, '..', '.' )
>    cPict      := replicate( '9', 8 - len( cPrefix ) )

>    nCounter   := 9 * ( 10 ^ ( len( cPict ) - 1 ) )
>    cbName     := { |n| cPath + cPrefix + TM( n, cPict ) + cExtension }
>    cUniqName  := eval( cbName, nCounter )

>    //  just keep checking until you find an unused file name
>    while file( cUniqName )
>       cUniqName := eval( cbName, ++nCounter )
>    endwhile

>    return cUniqName

> [end paste]

> Hope this may be of some use to you.  I have been using this to create
> unique files to hold terminal-specific information in a data directory
> on a server.

> Regards

> Don Andersen
> RAT Software
> Database Programming, Accounting Solutions, PC Support
> Phone +61 07 3881 2432
> QUEENSLAND AUSTRALIA

> "I never forget anything I remember"

Hello Don & Rick

I don't speak english very well (usely speak french) but i'll try
to tell you one possibility of problem with the function. (I think)

I'm a Foxpro & Clipper Programmer. I already developped an inventory
system in Foxpro. I had the same problem as you Rick.

My solution was really near yours Don. Except for one thing.

I just want to tell you what was my problem.

Just after the WHILE, when you find the correct unique name, if
another user at the same time is looking for a unique file you can
have problems.

For example : A user took an unique name via this fucntion but the
file is not yet created. Another user use the same fuction at the same
time.
He will receive the same name because this file is not already created.

I really got some problems with that. The solution was to create a
DUMMY.DBF file on the server. Each time i need to have an unique
name, i locked this file before the WHILE and i unlocked it just
after the creation of the file. This way every users have to
lock this file before taking the unique file name.

This way, you are sure that every user will wait one by one to
receive an unique file name. So you have to test for FLOCK()
before looking for the unique file.

I hope i'm right. I don't want to be the bad guy :)

Regards

Francois Belley



Tue, 19 Oct 1999 03:00:00 GMT  
 Return a unique file file name - function??


Quote:
>How can I return a unique file name in clipper.

Example:

ThisFile := tempname()

... do some stuff ...

ferase( thisfile )
ferase( thisfile + '.DBF' )
ferase( thisfile + '.CDX' )

etc...

------------------------------------------------------

static seed := -1

function TEMPNAME

local TEMP, Handle, RNUM := 0, x

for x := 1 to 1000
  do while RNUM < 100000000
    RNUM := RANDOMIZE(10000000000000)
  enddo
  TEMP := '$'+left(alltrim(str(RNUM)),7)
  RNUM := adir(TEMP+'.*')
  if RNUM = 0
    Handle := fcreate(TEMP,0)
    fclose(Handle)
    exit
  endif
next

if x > 999
  temp := '$NOTUNIQ'
  Handle := fcreate(TEMP,0)
  fclose(Handle)
endif

return TEMP

function RANDOMIZE( nLimit )
// returns a random number between 1 and nLimit, inclusive

return int( random() * nLimit ) + 1

function random()

local p := 335544319       // This is just a lucky prime number
local x := ( 2 ^ 31 ) - 1  // This is the distribution factor
local y := x / p
local z := x % p

if seed == -1
   seed := ( seconds() * day( date() ) * ;
             month( date() ) * year( date() ) ) % x
endif

seed := abs( p * ( seed % y ) - z * ( seed / y ) ) % x

return ( seed - 1 ) / ( x - 2 )

---


             WWW  http://www.iag.net/~philb/
        FTP Site  ftp://ftp.iag.net/pub/clipper



Wed, 20 Oct 1999 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Return a unique file file name - function??

2. DOS File Lookup - just the file name, not the path AND file name

3. Use of unique file names

4. CW15 : Freeware library - Unique file name (8 characters)

5. How to get unique names from file

6. Unique file names

7. Unique file name [newbie]

8. generating a unique file name in tcl

9. Parsing CSV file outputting desired information into multiple unique files

10. How can I know the .EXE file name, file date, file size

11. Functions to return data file information? -Reply

12. Functions to return data file information?

 

 
Powered by phpBB® Forum Software