Results 1 to 14 of 14

Thread: if exist

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2000
    Location
    medina, OH
    Posts
    125
    ho do i delete a file if it exists and if
    it dosent resume?
    "Sometimes the only way you can feel good about yourself is by making someone else look bad And I'm tired of making other people feel good about themselves"-Homer Simpson

  2. #2
    Fanatic Member VBKNIGHT's Avatar
    Join Date
    Oct 2000
    Location
    Port25
    Posts
    619

    Exclamation KILL KILL KILL

    use KILL command.

    If a post has helped you then Please Rate it!

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2000
    Location
    medina, OH
    Posts
    125

    how

    but how do i check if the file exists?
    "Sometimes the only way you can feel good about yourself is by making someone else look bad And I'm tired of making other people feel good about themselves"-Homer Simpson

  4. #4
    Guest
    Use the Dir() function.

    Code:
    If Dir("C:\MyFile.exe") <> "" Then
    'it exists
    Kill "C:\MyFile.exe"
    Else
    'it doesn't exist
    End If

  5. #5
    Guest
    Just a few suggestions...
    A file cannot be deleted if it is in use.
    So you should use On Error Resume Next.
    Place it at the top of all your code to ignore any errors.

    Or you can close the exe and then delete it:

    Code:
    Private Declare Function ProcessFirst Lib "kernel32" _
    Alias "Process32First" (ByVal hSnapshot As Long, uProcess _
    As PROCESSENTRY32) As Long
    
    Private Declare Function ProcessNext Lib "kernel32" _
    Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As _
    PROCESSENTRY32) As Long
    
    Private Declare Function CreateToolhelpSnapshot Lib "kernel32" _
    Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, _
    lProcessID As Long) As Long
    
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject _
    As Long) As Long
    
    Private Declare Function OpenProcess Lib "kernel32" (ByVal _
    dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
    ByVal dwProcessId As Long) As Long
    
    Private Declare Function TerminateProcess Lib "kernel32" _
    (ByVal hProcess As Long, ByVal uExitCode 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
    
    
    Private 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

    And you can use it like:

    Code:
    Private Sub Command1_Click()
         If Dir("C:\MyFile.exe") <> "" Then
              Call KillApp("C:\MyFile.exe")
              Kill "C:\MyFile.exe"
         Else
              Msgbox "File doesn't exist!", vbCritical
         End If
    End Sub

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Aug 2000
    Location
    medina, OH
    Posts
    125

    thanks

    thanks alot for the help
    "Sometimes the only way you can feel good about yourself is by making someone else look bad And I'm tired of making other people feel good about themselves"-Homer Simpson

  7. #7
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    if it doesn't resume???

    what do u mean by that?

  8. #8
    Guest
    Originally posted by da_silvy
    if it doesn't resume???

    what do u mean by that?
    Hehehe.
    Writing/Typing <> Talking
    They both look/sound different.

    For that sentence:

    ho do i delete a file if it exists and if
    it dosent resume?

    It sounds (looks) like:

    how do I delete a file if it exists?
    and if it doesn't then continue on...

    Or something like that.
    Hope I cleared things up for you .

  9. #9
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    yeah sort of... thanks

  10. #10
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341
    Option Explicit

    Public Function Exist(sFile as string, bDirectory as Boolean) As Boolean
    If bDirectory = True then
    If Dir("C:\MyFile.exe", vbDirectory) <> "" Then
    Exist = True
    Else
    Exist = False
    End If
    Else
    If Dir("C:\MyFile.exe", vbDirectory) <> "" Then
    Exist = True
    Else
    Exist = False
    End If
    End If
    End Function

    Public Sub Command1_Click()
    On Error Goto Error

    If Exist("C:\VB\MyApp.exe",False) = True Then
    Kill("C:\VB\MyApp.exe")
    End If

    If Exist("C:\VB",True) = True then
    Kill("C:\VB")
    End If

    Error:
    Exit Sub
    End Sub

  11. #11
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    gates ur signature is 2 big!!

  12. #12
    Addicted Member Tiovital's Avatar
    Join Date
    Apr 2000
    Posts
    249
    Hi,
    Try if Fso.FileExists(filespec) Fso.DeleteFile (filespec)

    Regards

  13. #13
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    PsyVision, we can drasticly short your code:

    Code:
    Public Function Exist(sFile as string, bDirectory as Boolean) As Boolean 
    If bDirectory = True then 
    Exist = Dir("C:\MyFile.exe", vbDirectory) <> "" 
    Else 
    Exist = Dir("C:\MyFile.exe") <> ""
    End If 
    End Function
    And in your code you had twice:
    Dir("C:\MyFile.exe", vbDirectory)
    so it would not matter if you specify bDirectory or not

    have fun mateys, I'm gonna drain my brains with alcohol now... it's my birthday!!! (hehe ok I'll stop now)

    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  14. #14
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341

    Whoops

    Silly me and well. I had a go. Even if it was a load 'a' crap.

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