Results 1 to 7 of 7

Thread: Killing a file?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091
    I want to delete a file each time I run a certain function just
    to make sure the file does not already exist.

    I used the following command at the start of the routine:

    Kill "c:\es*.dat"

    But, if the file doesn't already exist, I get an error..

    How should I re-write the above code to only Kill if the files exists
    or how to ignore the error (without using On Error Resume Next) if the file
    doesn't exist?

    Thanks for the help..

    Dan

  2. #2
    Guest
    Try this:

    Code:
    If Dir("C:\file.exe") <> "" Then
    Kill "C:\file.exe"
    Else
    Msgbox "Cannot delete file!", vbCritical
    End If

  3. #3
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    Use On Error

    Because you want to delete the file if it exists and do not care about what happens if it does not exist. In this case, you are fine to simply ignore the VB error!


    Code:
    On Error Resume Next
    Kill "C:\file.exe"
    On Error GoTo 0
    ' the above line ensures that future errors are reported!
    If you were in fact interested in the error,
    Code:
    On Error Resume Next
    Kill "C:\file.exe"
    If Err.Number <> 0 then
      MsgBox "uh oh"
    Endif
    On Error GoTo 0
    ' the above line ensures that future errors are reported!
    Regards
    Paul Lewis

  4. #4
    Guest
    But, if the file doesn't already exist, I get an error..

    How should I re-write the above code to only Kill if the files exists
    or how to ignore the error (without using On Error Resume Next) if the file
    doesn't exist?
    He said he didn't want to use On Error Resume Next .


  5. #5
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    hehehe

    Matthew,
    I know, but truly, it is the best way

    I thought maybe he was avoiding using On Error... because maybe (sorry if I am wrong dbassettt74) he didn't know how to cancel the error trapping...

    But to be fair - if there were other reasons why he did not want to use On Error... then I think your answer is pretty much as simple as it gets..

    Cheers
    Paul Lewis

  6. #6
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    Hmmm...

    Why did I say that On Error Resume Next was best?

    I couldn't remember why I always do it that way until just now:

    If the file cannot be deleted for some reason (e.g. open file/read only etc) then you'll get an error which will need to be handled. So you HAVE to use On Error... because you don't know what could be happening on the machine that might cause you to fail to delete a file...

    I hope that gets me off the hook

    Cheers
    Paul Lewis

  7. #7
    Guest
    Actually, that is a good idea. Incase the file exists and it's running. You cannot delete a running file. So that's where the On Error Resume Next comes in. Good thinking Paul.

    By the way, here is also a way to delete an application if it is running. (Even though I said you can't...you can't do it with a normal the Kill function all by itself.) But here is how to end the running application if it's running (if it's not running, it won't do anything, but continue on to deleting the file) and then delete it:

    Code:
    Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
    Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
    Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, lProcessID As Long) As Long
    Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    
    Type PROCESSENTRY32
        dwSize As Long
        cntUsage As Long
        th32ProcessID As Long
        th32DefaultHeapID As Long
        th32ModuleID As Long
        cntThreads As Long
        th32ParentProcessID As Long
        pcPriClassBase As Long
        dwFlags As Long
        szexeFile As String * MAX_PATH
        End Type
    
    
    Public Function KillApp(myName As String) As Boolean
        Const PROCESS_ALL_ACCESS = 0
        Dim uProcess As PROCESSENTRY32
        Dim rProcessFound As Long
        Dim hSnapshot As Long
        Dim szExename As String
        Dim exitCode As Long
        Dim myProcess As Long
        Dim AppKill As Boolean
        Dim appCount As Integer
        Dim i As Integer
        On Local Error GoTo Finish
        appCount = 0
        
        Const TH32CS_SNAPPROCESS As Long = 2&
        
        uProcess.dwSize = Len(uProcess)
        hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
        rProcessFound = ProcessFirst(hSnapshot, uProcess)
        
        Do While rProcessFound
            i = InStr(1, uProcess.szexeFile, Chr(0))
            szExename = LCase$(Left$(uProcess.szexeFile, i - 1))
            If Right$(szExename, Len(myName)) = LCase$(myName) Then
                KillApp = True
                appCount = appCount + 1
                myProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
                AppKill = TerminateProcess(myProcess, exitCode)
                Call CloseHandle(myProcess)
            End If
            rProcessFound = ProcessNext(hSnapshot, uProcess)
        Loop
    
        Call CloseHandle(hSnapshot)
    Finish:
    End Function
    
    Private Sub Command1_Click()
    If Dir("C:\file.exe") <> "" Then
    Call KillApp("C:\file.exe")
    Kill ("C:\file.exe")
    Else
    Msgbox "Cannot delete file!", vbCritical
    End If
    End Sub

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