Creating unique temporary files
Publicado: Dom Ene 11, 2009 12:12 am
Does anyone have suggestions for the best way to create unique temporary
files? I am working on a networked application and need to create temporary
databases for reports. Because several clients may attempt to run these
reports at the same time, the temporary files need to have unique names.
I tried to use code like the following that worked in Clipper:
FUNCTION UniqFile
LOCAL I, FHandle
LOCAL cFileName := ""
I := 0
DO WHILE .T.
cFileName := 'TMP'+leadchar(STR(I,5,0),'0')+'.'+cExtension
IF ! FILE(cFileName) // if file doesn't already exist
// have to actually create a file in order to reserve its name
// the parameter 0 signifies file is created with normal attributes
(read/write)
IF FHandle := FCREATE(cFileName,0) < 0
msgStop("Unable to create temporary file.")
ENDIF
EXIT
ELSE
I := I + 1
ENDIF
ENDDO
RETURN cFileName
However, I find that on a workstation running Vista, I get a DOS 32 error
whenever attempting to overwrite a file created in this manner. For
instance, if I create a temporary file named TMP00000.TMP, assign it to the
memvar cFile1 and then try the statement CREATE &cFile1, I get a DOS ERROR
32. DOS Error 32 is a "sharing violation". My existing uniqfile function has
to actually create the file in order to reserve that unique filename for
future use -- it cannot just give me a potential file name to use later with
a CREATE statement because I don't know if that filename will still be
unique at a future time later in the code execution. FCREATE has no problem
creating the file, but even though it has created it with the "Normal"
attribute (READ/WRITE), xHarbour apparently can't overwrite it in Vista. I'm
thinking Vista must handle file creation and manipulation differently than
XP because my app never had a problem with this code on XP workstations.
Any suggestions would be appreciated.
Thanks in advance,
Gary
files? I am working on a networked application and need to create temporary
databases for reports. Because several clients may attempt to run these
reports at the same time, the temporary files need to have unique names.
I tried to use code like the following that worked in Clipper:
FUNCTION UniqFile
LOCAL I, FHandle
LOCAL cFileName := ""
I := 0
DO WHILE .T.
cFileName := 'TMP'+leadchar(STR(I,5,0),'0')+'.'+cExtension
IF ! FILE(cFileName) // if file doesn't already exist
// have to actually create a file in order to reserve its name
// the parameter 0 signifies file is created with normal attributes
(read/write)
IF FHandle := FCREATE(cFileName,0) < 0
msgStop("Unable to create temporary file.")
ENDIF
EXIT
ELSE
I := I + 1
ENDIF
ENDDO
RETURN cFileName
However, I find that on a workstation running Vista, I get a DOS 32 error
whenever attempting to overwrite a file created in this manner. For
instance, if I create a temporary file named TMP00000.TMP, assign it to the
memvar cFile1 and then try the statement CREATE &cFile1, I get a DOS ERROR
32. DOS Error 32 is a "sharing violation". My existing uniqfile function has
to actually create the file in order to reserve that unique filename for
future use -- it cannot just give me a potential file name to use later with
a CREATE statement because I don't know if that filename will still be
unique at a future time later in the code execution. FCREATE has no problem
creating the file, but even though it has created it with the "Normal"
attribute (READ/WRITE), xHarbour apparently can't overwrite it in Vista. I'm
thinking Vista must handle file creation and manipulation differently than
XP because my app never had a problem with this code on XP workstations.
Any suggestions would be appreciated.
Thanks in advance,
Gary