Results 1 to 7 of 7

Thread: Having Problems Deleting a File in VB6

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    15

    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

  2. #2
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    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)

  3. #3
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    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

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    15

    Re: Having Problems Deleting a File in VB6

    The Kill function works just fine in my case. Thanks for your help!

  6. #6
    New Member
    Join Date
    Jul 2017
    Posts
    9

    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.

  7. #7
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,624

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width