Unable to delete file .dbf

Topics: User Forum
Jul 4, 2014 at 3:25 PM
Hi friends,
i've a new problem with AVFP. I made a web application that use a dbf file. Now, i need to delete a dbf with one of buttons on my page. I used this code, in vfp syntax :
    IF FILE(oProp.DataPath+"mytable")
        IF USED("amytable")             &&alias
            SELECT amytable
            USE
        ENDIF
        IF USED(oProp.DataPath+"mytable")
            USE oProp.DataPath+"mytable"
        ENDIF
        DELETE FILE(oProp.DataPath+"mytable"+idTav)
    ENDIF
All is ok in the execution but the file are not delete.. I think that is "IIS service" that lock deleting. How can i unlock it for remove the file?
Developer
Jul 5, 2014 at 2:13 PM
I use SQL Server database .. but try the following

If table is part of dbc

DROP TABLE real_table_name
or
REMOVE TABLE Table_Name DELETE

If table is free table

Erase dbfname


Also as a side note
   IF USED("amytable")             &&alias
        SELECT amytable
        USE
    ENDIF
can be written as

use in select("amytable")
Jul 7, 2014 at 10:22 AM
Thanks for your reply. I've tried but nothing to do... If i remove the dbf file from explorer, i get a message that says: "Cannot delete the file because it is opened from w3wp.exe".
Coordinator
Jul 8, 2014 at 10:08 AM
Use the Win32 API deletefile to delete files in avfp. Should work smoothly.
Jul 11, 2014 at 2:42 PM
claudefox wrote:
Use the Win32 API deletefile to delete files in avfp. Should work smoothly.
Unfortunately it doesn't work... :(
Coordinator
Jul 11, 2014 at 5:29 PM
What do you mean it doesn't work? Can you give me the example you tried.

Unless I'm remembering wrong, this was the workaround for VFP's deletefile not working in a mtdll...
Coordinator
Jul 13, 2014 at 11:40 AM
Edited Jul 13, 2014 at 12:03 PM
Did you try this:
DECLARE DeleteFile IN WIN32API STRING

DeleteFile(yourfilename)
It would be nice if you told us what error message you get with this or if it actually does work..
Jul 14, 2014 at 8:19 AM
claudefox wrote:
Did you try this:
DECLARE DeleteFile IN WIN32API STRING

DeleteFile(yourfilename)
It would be nice if you told us what error message you get with this or if it actually does work..
I use this code:
DECLARE INTEGER DeleteFile IN kernel32;
    STRING lpFileName
DeleteFile( oProp.DataPath+"amytable.dbf" )
This don't work because the dbf file is in use from IIS :( Now i will try your code. Thanks
Jul 14, 2014 at 9:06 AM
I tried your code, but i can't resolve my problem. This is all my code:
<%      
    DECLARE DeleteFile IN WIN32API STRING
    
    IF FILE(oProp.DataPath+"amytable.dbf")
        IF USED(oProp.DataPath+"amytable.dbf")
            USE oProp.DataPath+"amytable.dbf"
        ENDIF
        
        DeleteFile( oProp.DataPath+"amytable.dbf" )
    ENDIF
%>
The code are run correctly but the file isn't deleted from your directory. If i try to remove manually the file from explorer i get this error message: "Could not complete the operation because the file is open in w3wp.exe".

I don't understand... i use "USE" command for close the file but this isn't released from IIS.

Thanks for your help!
Coordinator
Jul 14, 2014 at 9:17 PM
Edited Jul 14, 2014 at 9:23 PM
Ah, I see.
I have successfully closed all tables using CLOSE DATA in main.prg. THat allowed me to to replace tables at GoDaddy without having to stop the web server. You might want to try this to see if you can issue DeleteFile with that file actually closed.
From a previous message:
" * now we'll return the HTML output to the browser
close data
RETURN lcHTMLout
In main.prg, this works for me like a charm every time! I replaced customer.dbf/cdx several times at GoDaddy.

Now if the site is very,very busy, then it's likely to get used again by the next request coming in. If you're in charge of IIS, then you just have to stop the Web Application in IIS manager...
"
Jul 15, 2014 at 8:57 AM
Edited Jul 15, 2014 at 8:59 AM
I'm sorry but i don't understand how i can use this... I put "close data" before of "RETURN lcHTMLout" but this is located in "function debug" and i don't use this function. I've tried equally but it doesn't work...

I explain you my situation:
  • i've a delete.avfp page that contains the code that i wrote previously.
  • my main.prg is the original with some added function that i call in others webpages.
If i run delete.avfp with parameters, the table is deleted but if i run delete.avfp with parameters after i do a select statements (always on this table), the table isn't deleted.

I don't think that site is very very busy because i do only 1 request at times (select, update or delete). I don't want to restart IIS service/site because this project is for multi-users and i don't want that another user have a error message due to IIS restart.

N.B. I tried to use "close data" also before "deletefile" command without any results...

N.B. 2: In the code i wrote previously i wrote directly the file name instead of parameters and variables.
Coordinator
Jul 15, 2014 at 6:05 PM
Edited Jul 15, 2014 at 6:07 PM
No, the code I'm referring to is at the bottom of the main.prg main line of code at the top, not in any individual function. If you put code in the main part of main.prg, it's guaranteed to run on every request (either before or after depending on where you place it).

When I look at the main part of main.prg I see the following at the bottom of it (not in the function section):

.
.
.
ENDCASE
*  end mainline
oProp.RunningPrg=[main.prg]

*!* *!*    COMMENT OUT FOR PRODUCTION
lcHTMLout = DebugDump(lcHTMLout) && DEBUG Dump Routine            *!*   *!*    COMMENT OUT FOR PRODUCTION
*!* *!*    COMMENT OUT FOR PRODUCTION

*!* oAA=null       && Clear class and program from memory
*!* CLEAR CLASS ('schedbizobj')  && Clear class and program from memory
*!* CLEAR PROGRAM ('prg\utiltest2.prg')    && Clear class and program from memory
*RELEASE PROCEDURE 'c:\avfp5.61Demo\prg\utiltest'   && Clear class and program from memory
  • CLOSE DATA && optionally close tables after each hit
* now we'll return the HTML output to the browser
RETURN lcHTMLout
Marked as answer by mr_ocean on 7/16/2014 at 3:31 AM
Jul 16, 2014 at 11:32 AM
It works! Thank you so much!