Page 1 of 1

cTempFile() -> bad alias

PostPosted: Wed May 31, 2006 3:59 pm
by Gale FORd
if you use cTempFile( cPath, 'DBF') to create temporary dbf file then the all numeric tempfile will cause an "illegal characters in alias" error when used.
I would use cNewFileName() but I need to create temp file using path.

Also I found that if you need a couple of temp files then you need to run cTempFile(), then create the actual file before you can get another temporary file with cTempFile().
This is especially true with cNewFileName()

I would like to change cTempFile so that it adds an alpha to the beginning of filename, or add path option to cNewFileName()


Something like:

function cTempFile( cPath, cExtension ) // returns a temporary filename

local cFileName

static cOldName

DEFAULT cPath := "", cExtension := ""

if ! "." $ cExtension .and. .not. empty( cExtension )
cExtension = "." + cExtension
endif
// Added 'A' to filename but we could pass a cRootName also
while File( cFileName := ( cPath + 'A'+StrTran( Time(), ":", "" ) + cExtension ) ) .or. ;
cFileName == cOldName
end

cOldName = cFileName

return cFileName

// and / or

//---------------------------------------------------------------------------//

function cNewFileName( cRootName, cExt, cPath )

local cFileName := cRootName + "1"
local nId := 1
static cOldName
DEFAULT cPath := ""

if ! "." $ cExtension .and. .not. empty( cExtension )
cExtension = "." + cExtension
endif
cFileName += cExtension

while File( cPath+cFileName ) .or. ;
cFileName == cOldName
cFileName = cRootName + ;
StrZero( ++nId, If( nId < 10, 1,;
If( nId < 100, 2, 3 ) ) ) + ;
cExt
end

cOldName = cFileName

return cFileName

PostPosted: Wed May 31, 2006 4:41 pm
by James Bott
Gale,

The use of the static cOldName is of limited value. It only enables you to create 2 filenames before you have to open them. If you want to open 3 or more you still have the same problem. I think it is best to open each file before getting a new filename.

I suppose you could force the cTempFile() to wait one second at the end. This way you couldn't get the same filename for another 24hrs.

Or, you could just wait one second between calls to cTempFile().

cFile1:= cTempFile(...)
waitSeconds(1)
cFile2:= cTempFile(...)

James

PostPosted: Wed May 31, 2006 6:12 pm
by Gale FORd
James,
Yes, the static was part of a mod I did in the past, but is not the real stopper.

After upgrading to latest xHarbour builder and FWH, I started getting alias errors. I remember a problem with clipper and alias names that were numeric.
So my real concern here is to return a filename that does not start with a number.

I would use cNewFileName() but it does not handle paths. So the mods to filename.prg could be made without the static but handle the issues concerning cTempFile and numeric filename and/or cNewFileName() and paths.

Thanks, Gale

PostPosted: Wed May 31, 2006 6:27 pm
by James Bott
Gale,

>So the mods to filename.prg could be made without the static but handle the issues concerning cTempFile and numeric filename and/or cNewFileName() and paths.<

Agreed.

I have been using my own tempfile() function for many years. It has leading characters in the name.

James

Code: Select all  Expand view
* Function   : TEMPFILE()
* Purpose    : To generate a unique name for a temporary file
*              (needed on networks so you don't overwite someone
*               else's temporary file).
* Comments   : Generates name, checks for existance of file with
*              generated name, if found generates a another name.
* Assumes    : file is to go into current directory
* Syntax     : TEMPFILE(expC1)
* Parameters : expC1 - extension of file (without the dot)
* Example    : findex=tempfile("ntx")
* Returns    : unique file name
* Author     : James Bott, CIS 71706,551
* History    : 09/27/93 Changed leading 3 chars from 000 to AAA
*              because Clip 5.2 gave "Illegal characters in alias" mess
*              10/05/93 Found that between hours of 00:00 & 01:00 was
*              getting spaces in name which also gave illegal char mess.
*              Fixed this.
*              10/7/2004 Added optional cDir

function tempFile(extension,cDir)
   local cFile
   default cDir:=".\"
   cDir:= if(right(cDir,1)!="\",cDir+"\",cDir)
   // loop until you find a name that doesn't exist
   do while .t.
      cFile:="AAA"+trim(str(seconds(),5,0))+"."+upper(extension)
      cFile:=strtran(cFile," ","0") // fix for hours between 00:00 & 01:00
      if .not. file( cDir + cFile )
         exit
      endif
   enddo
return cFile

PostPosted: Thu Jun 01, 2006 5:17 pm
by dbzap
Here is my litle contribution, same like James its come with my from years ago.
The diferencie....mmmm...create the file when found a file-name who doenst exists.

Code: Select all  Expand view
//-------------------------------------
Function TEMPFILE( lInicio, cDefa )
Local file_name, counter, extension
Local hFile
Static cDirectorio
DEFAULT lInicio := .F.            // inicializa dir temporal?
If lInicio
   cDirectorio := cDefa          // establece directorio temporal
   Return NIL                     // retornar sin crear
Else
   file_name := ''
   extension := 'DBF'

   For counter := 1 TO 9999
       file_name := 'TEMP' + PADL(counter, 4, '0') + '.' + extension
       If !File( file_name )
          If ( hFile := fCreate( cDirectorio+"\"+file_name ) ) <> -1
             FClose( hFile )
             EXIT
          EndIf
       EndIf
   Next

   Return Left( file_name, 8 )

EndIf
Return NIL