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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DELETE FILE or DeleteFile()? 2

Status
Not open for further replies.

ChrisRChamberlain

Programmer
Mar 23, 2000
3,392
0
0
GB
In most situations, the native DELETE FILE will delete the named file(s).

But what if the files have not been deleted and subsequent events are dependant on their removal?

You then need to verify the existence of the file(s) with either FILE(), (which will fail under certain circumstances) or ADIR().

Enter the WinAPI call DeleteFile().

Disadvantage - will not accept wildcards, but you can programatically loop through the file names.

Advantage - the return value of the function will indicate the success or otherwise of the delete.

Syntax -

DECLARE INTEGER DeleteFile IN kernel32 ;
[tab]STRING lpFileName

lcFilename = [C:\My app\test.doc]
lnRetVal = DeleteFile(lcFilename + CHR(0))
IF lnRetVal = 0
[tab]MESSAGEBOX([Error - ] ;
[tab][tab]+ lcFileName ;
[tab][tab]+ [ - has not been deleted!])
ENDI
FAQ184-2483 - the answer to getting answered.​
Chris [pc2]
 
Thanks CHris

Many times I've been with an undeleted file using the VFP DELETE FILE. Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Plus, with the VFP DELETE FILE, you don't get wildcards, either, and it throws an error if it doesn't delete the file, which error can be difficult to trap in certain circumstances.

I've started using DeleteFile() recently, too.
 
Chris

This one is even worst, if the file does not exit you get a com status error:

oFS = CreateObject("Scripting.FileSystemObject")
oFS.DeleteFile("c:\somehtml.htm")
Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Chris...very interesting topic.

Why would a file not be deleted?

I'm thinking install from CD where file attrib are by default set to read only.

In your experience does it matter if the file is *not* read only that it cannot be deleted?

Is there anyway to programatically change a file's attributes from read-only to read-write?

Regards - Wayne
sig_jugler.gif
...all this and tap dancing too!
 
Sure you can change a file's attributes programmatically:
see faq184-3327
 
wgcs;

Got it! Thanx for sharing your knowledge with me and the group.

Regards - Wayne
sig_jugler.gif
...all this and tap dancing too!
 
Thanks for the Star!
 
Why not just use the native ERASE function in VFP? It takes wildcards, and is not subject to path sensitivity like DELETE FILE is.

Hope that helps!

-- Ed
-- Get great VFP support in a new forum filled with Microsoft MVPs!
--
 
Both DELETE FILE and ERASE accept wildcards and the syntax appears to be identical, although the wording is different in the help file.

ERASE has an advantage in that it does not generate an error if the specified file does not exist.

What both lack is a return value determining the success of the operation hence the advantage of DeleteFile().
FAQ184-2483 - the answer to getting answered.​
Chris [pc2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top