cTempFile() -> bad alias

cTempFile() -> bad alias

Postby Gale FORd » Wed May 31, 2006 3:59 pm

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
Gale FORd
 
Posts: 663
Joined: Mon Dec 05, 2005 11:22 pm
Location: Houston

Postby James Bott » Wed May 31, 2006 4:41 pm

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
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Postby Gale FORd » Wed May 31, 2006 6:12 pm

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
Gale FORd
 
Posts: 663
Joined: Mon Dec 05, 2005 11:22 pm
Location: Houston

Postby James Bott » Wed May 31, 2006 6:27 pm

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  RUN
* 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
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Postby dbzap » Thu Jun 01, 2006 5:17 pm

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  RUN
//-------------------------------------
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
User avatar
dbzap
 
Posts: 189
Joined: Mon Nov 07, 2005 7:36 pm
Location: Chile


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 32 guests