Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Erase a temporary file 1

Status
Not open for further replies.

psalyer

Technical User
Jun 20, 2001
30
US
Hi all,
I used the code
tmpfile = SYS(3)
SELECT * FROM CSL WHERE REQ_ID=MREQ ORDER BY REQ_LINE INTO DBF (tmpfile)

Now, how do I dele this temporary.dbf after I am done with it. I don't want to use into cursor myqueryname because it is creates a read only file.
 
Hi Chris

I tried that and I get the error "operator/operand type mismatch".

Pete
 
try
Delete file (tmpfile+".dbf")
David W. Grewe
Dave@internationalbid.com
ICQ VFP ActiveList #46145644
 
psalyer

Apologies - post in haste and repent forever

lcName = ALLT(STR(EVAL(tmpfile))) + [.dbf]
DELE FILE (lcName)

Chris :)
 
Hi

DELETE FILE tmpfile+".DBF"
DELETE FILE tmpfile+".FPT"

This will do the job. ramani :-9
(Subramanian.G)
FoxAcc
ramani_g@yahoo.com
LET KNOW IF THIS HELPED. ENOUGH EXPERTS ARE HERE TO HELP YOU OUT! BEST OF LUCK :)
 
Hi all,
that worked to delete the tmpfile. Now I need to check to see if the tmpfile is in use before deleting it. I used the code
if sys(3)=tmpfile
DELETE FILE tmpfile+".DBF"
endif
the problem I have is that if there isn't a tmpfile I get the error "variable "tmpfile" not found.
 
Hi psalyer,
Try using this,

IF FILE(tmpfile+".DBF")
IF USED(tmpfile)
USE &tmpfile
ENDIF
DELE FILE tmpfile+".DBF"
ENDIF


Hope it works!
 
Hi rajeevnandanmishra

When I use that code and the tmpfile is in use it works great and deletes the tmpfile. If the tmpfile does not exit I still get the error message "variable "tmpfile" not found.
If the tmp file does not exist, I need it to continue to execute the code without trying to delete anything.
Thanks Pete
 
Hi psalyer,
Write this code at whatever point you want to delete the temp file.
I had attached one more function here,
Try it. It should work!

IF UPPER(TYPE('tmpfile'))<>'U' AND FILE(tmpfile+&quot;.DBF&quot;)
IF USED(tmpfile)
USE &tmpfile
ENDIF
DELE FILE tmpfile+&quot;.DBF&quot;
ENDIF


 
Hi Pete Salyer,

1. If you are using a form, the best way will be to use a form level property.. Assume you are using two temp files.. create a form level property.. cTemp1 and cTemp2.
Then in the init event of the form.. put the code..
ThisForm.cTemp1 = SYS(3)
ThisForm.cTemp2 = SYS(3)
Beware the SYS(3) function is very tricky. In very fast machines, it used to give duplicate values. To avoid that, I always tend to add a small catcher code like this ...
DO WHILE ThisForm.cTemp1 = ThisForm.cTemp2
ThisForm.cTemp2 = SYS(3)
ENDDO

Now you can use this cTemp1 or cTemp2 at appropriate places
with reference to ThisForm.cTemp1 etc.

Then to delete the temp files, in the forms DESTROY EVENT put the code...

CLOSE ALL
IF !EMPTY(ThisForm.cTemp1)
DELETE FILE (ThisForm.cTemp1)+&quot;.DBF&quot;
DELETE FILE (ThisForm.cTemp1)+&quot;.FPT&quot;
DELETE FILE (ThisForm.cTemp1)+&quot;.CDX&quot;
DELETE FILE (ThisForm.cTemp1)+&quot;.IDX&quot;
ENDIF
IF !EMPTY(ThisForm.cTemp2)
DELETE FILE (ThisForm.cTemp2)+&quot;.DBF&quot;
DELETE FILE (ThisForm.cTemp2)+&quot;.FPT&quot;
DELETE FILE (ThisForm.cTemp2)+&quot;.CDX&quot;
DELETE FILE (ThisForm.cTemp2)+&quot;.IDX&quot;
ENDIF

And repeat it as many time as the cTemp1/2/3/4 etc is craeted. Note (1) that even if the file is not existing, the above code will not create any error. Note (2) that when the SQL SELECT is creating to DBF cTemp1, the file gets initialised by SQL and so all the records get zaped to start with.

2. If you are using codes to do the job, then you can initialise the cTemp1, cTemp2 etc in the beginning of the PRG file for this instance
PRIVATE cTemp1, cTemp2
cTemp1 = SYS(3)
cTemp2 = SYS(3)
DO WHILE cTemp2 = cTemp1
cTemp2 = SYS(3)
ENDDO

and then do rest.

When the prg is RETURNing to called routine when the close tables are issued, follow it up the code...
IF !EMPTY(cTemp1)
DELETE FILE cTemp1+&quot;.DBF&quot;
DELETE FILE cTemp1+&quot;.FPT&quot;
DELETE FILE cTemp1+&quot;.CDX&quot;
DELETE FILE cTemp1+&quot;.IDX&quot;
ENDIF
IF !EMPTY(cTemp2)
DELETE FILE cTemp2+&quot;.DBF&quot;
DELETE FILE cTemp2+&quot;.FPT&quot;
DELETE FILE cTemp2+&quot;.CDX&quot;
DELETE FILE cTemp2+&quot;.IDX&quot;
ENDIF

This will work perfectly. I have used this type of coding in my applications. Hope this helps you.
ramani :-9
(Subramanian.G)
FoxAcc
ramani_g@yahoo.com
LET KNOW IF THIS HELPED. ENOUGH EXPERTS ARE HERE TO HELP YOU OUT! BEST OF LUCK :)
 
Thanks to all of you. I have it working perfect now.

Pete
 
ramani

Curious as to why:-

CLOSE ALL
IF !EMPTY(ThisForm.cTemp1)
DELETE FILE (ThisForm.cTemp1)+&quot;.DBF&quot;
DELETE FILE (ThisForm.cTemp1)+&quot;.FPT&quot;
DELETE FILE (ThisForm.cTemp1)+&quot;.CDX&quot;
DELETE FILE (ThisForm.cTemp1)+&quot;.IDX&quot;
ENDIF
IF !EMPTY(ThisForm.cTemp2)
DELETE FILE (ThisForm.cTemp2)+&quot;.DBF&quot;
DELETE FILE (ThisForm.cTemp2)+&quot;.FPT&quot;
DELETE FILE (ThisForm.cTemp2)+&quot;.CDX&quot;
DELETE FILE (ThisForm.cTemp2)+&quot;.IDX&quot;
ENDIF

couldn't be:-

CLOSE ALL
IF !EMPTY(ThisForm.cTemp1)
DELETE FILE (ThisForm.cTemp1)+&quot;.*&quot;
ENDIF
IF !EMPTY(ThisForm.cTemp2)
DELETE FILE (ThisForm.cTemp2)+&quot;.*&quot;
ENDIF

Chris :)
 
Here is a function I created and have been using for years to generate temporary file names. It returns a filename that does not exist in the form of TMPxxxx.dbf where xxxx is a number from 1000 - 9999. The neat thing about it is that everytime it is called it looks for files that begin with TMP and were created prior to today and then deletes them. That way I never have to worry about deleting temporary files. Of course, the one caveat is that it could erase a file that begins with TMP that was not created with this function. I am aware of this possibility and am careful never to name files beginning with TMP other than temporary files.

For what it is worth ...


function tmpdbf
local cValue, cFilename, nValue, aTmpsLen, dFiledate, dToday
dToday = date()

* This routine erases TMP????.* files which were created prior to today.

aDir(aTmps,&quot;TMP????.*&quot;)
if type(&quot;aTmps&quot;) <> &quot;U&quot;
aTmpsLen = aLen(aTmps,1)
for j = 1 to aTmpsLen
cFilename = aTmps[j,1]
dFiledate = aTmps[j,3]
if dFiledate < dToday
erase (cFilename)
endif
endfor
endif
nValue = 1000
do while .t.
nValue = nValue + 1
cValue = str(nValue,4)
cFilename = &quot;TMP&quot;+cValue+&quot;.DBF&quot;
if not file(cFilename)
exit
endif
enddo
return cFilename

Don
dond@csrinc.com

 
Hi Chris

We could use DELETE FILE with wild card &quot;.*&quot;. Atleast that is what VFP6 manual says, that, we can use wild card. But in practice, somewhere I noticed, That it did not function correctly and gave some error like &quot;File not found&quot;. But when used as I mentioned above, the error messages were not flashed.. Strange but true. That is the reason, I used it that way. Also FP2.6 does not allow use of wild card characters and the habit continued as well. But VFP does respond with wild card characters usage. These type of functions, generaly, I code in only my BaseForm subclassed from VFP Base Class. So these codes automaticlly fire in all my forms. I just initialise the value in the init event whereever required and that is it. The rest of action is taken over by the forms functionality. If there are temporary files, the destroy event deletes them, automatically.

Hope this answers your question. But wild cards can be used in VFP6. ramani :-9
(Subramanian.G)
FoxAcc
ramani_g@yahoo.com
LET KNOW IF THIS HELPED. ENOUGH EXPERTS ARE HERE TO HELP YOU OUT! BEST OF LUCK :)
 
Ramani

I was puzzled why you weren't coding the obvious way and thanks for the explanation.

I have often considered starting a news item thread where members could contribute such experiences, especially where the actual code works and the help file says it should not, or vice versa.

In certain circumstances, I seem to be able to successfully REMOVEOBJECT() where no object exists without any error!

Chris :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top