Having Problems Deleting a File in VB6
This should be an easy one. I'm checking to see if a file exists, and if so, then I'm trying to delete it. I'm getting hung up on the actual deletion part.
My unsuccessful snippet of code is commented out in the test procedure below. The function call to FileExists works fine. Please help if you can. Thanks!
Sub test ()
Dim XMLINPUTPATH as string
XMLINPUTPATH = "C:\ABC\XML_Input.xml"
'IF THIS FILE ALREADY EXISTS, THEN DELETE IT!
If FileExists(XMLINPUTPATH) Then
'delete file
' Dim oFileSysObj
' Set oFileSysObj = ?
' oFileSysObj.Deletefile XMLINPUTPATH
End If
End Sub
Public Function FileExists(sPath As String) As Boolean
If Dir(sPath) > "" Then
FileExists = True
Else
FileExists = False
End If
End Function
Re: Having Problems Deleting a File in VB6
Use VB's Kill function (you might have to do a SetAttr before that to remove the Read only tag)
Re: Having Problems Deleting a File in VB6
Or adda reference to Microsoft Scripting Runtime, then do
Dim fso as FileSystemObject
Set fso = New FileSystemObject
fso.Delete....
etc
Re: Having Problems Deleting a File in VB6
With VB, it is as simple as Kill XMLINPUTPATH
But there is a gotcha. The file cannot be read only or hidden. So you will want to check the file attributes and reset them if necessary, using VB's GetAttr and SetAttr functions.
Also Kill will not delete a file that is in use or that you don't have permissions to delete.
baja_vu got there first.
Re: Having Problems Deleting a File in VB6
The Kill function works just fine in my case. Thanks for your help!
Re: Having Problems Deleting a File in VB6
I'm sorry if this is the wrong thread, but I haven't been able to find a suitable one for my related problem...
I have an issue using any method of file deletion on multiple files (i.e. Kill or FSO.deletefile). When this code executes in a single procedure on more than 2 files, the .exe will crash, the previously deleted files return and the .exe disappears. Also, if I run this in the IDE, similarly, the files may return and VB6.exe disappears. I found this problem happens on specific machines. I've tried reinstalling VB6 to no avail. Please help! I know this has something to do with my machine specific system files, or registry or something of that nature due to the machine specific nature of it. I just cannot locate what is corrupt.
Re: Having Problems Deleting a File in VB6
Please start your own NEW thread....state your problem there.
What is 'this code'...? Please put the code that you have and is not working in the post in a new thread....title the Thread appropriately.
Welcome to the Forum.
Sammi