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

Delete a file

Status
Not open for further replies.

Helen1greece

Technical User
Jun 4, 2003
85
GR
Is there a way to delete a file from my Hard-Drive using VB code. I use batch files but it doesn't work qood. Can somebody give me an example code?
 
Look up the keyword Kill. Be careful as this will definately remove files.
 
You can also call the API function SHFileOperation which will give a bit more flexibility than Kill; send the files to the recycle bin, display the Windows progress dialog etc.

Paul Bent
Northwind IT Systems
 
The FileSystemObject, which is part of the Scripting Runtime library scrrun.dll provides all needed methods. This object is included into your project through the Project Menu -> References -> Check Scripting Runtime Library.

A skeleton program may look like this:

Dim strFileName as String

Dim objFso as Scripting.FileSystemObject
Set objFso = New Scripting.FileSystemObject

With objFso
Select Case .FileExists(strFileName)
Case True
.DeleteFile(strFileName,
True)
Case False
' Do Nothing or Error Message "File does not exist"
End Select
End With

Set objFso = Nothing



strFileName should be somewhere initialized with the Name of the File you want to delete.

The True parameter in the object's method means that you want to delete the file, even if it is Read only. Be careful using that one (default = False)


_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
I tried to use the code provided by rvBasic but i get a "Expected Function or variable" error message.

What gives?

Any help would be appreicated.
 
sorry i forgot to mention that the error comes up on the
.DeleteFile(srtFileName,True) line
i'm using the code in a sub.
 
Change the line to either

Call .DeleteFile(strFileName, True)
or
.DeleteFile strFilename, True

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top