|
-
Nov 24th, 2000, 09:30 PM
#1
Thread Starter
Lively Member
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
-
Nov 24th, 2000, 09:36 PM
#2
-
Nov 24th, 2000, 09:38 PM
#3
Thread Starter
Lively Member
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
-
Nov 24th, 2000, 09:58 PM
#4
Use the Dir() function.
Code:
If Dir("C:\MyFile.exe") <> "" Then
'it exists
Kill "C:\MyFile.exe"
Else
'it doesn't exist
End If
-
Nov 24th, 2000, 10:12 PM
#5
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
-
Nov 25th, 2000, 02:44 PM
#6
Thread Starter
Lively Member
"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
-
Nov 25th, 2000, 11:22 PM
#7
Conquistador
if it doesn't resume???
what do u mean by that?
-
Nov 26th, 2000, 02:14 AM
#8
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 .
-
Nov 26th, 2000, 04:10 AM
#9
Conquistador
-
Nov 26th, 2000, 05:00 AM
#10
Frenzied Member
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
-
Dec 2nd, 2000, 04:34 AM
#11
Conquistador
gates ur signature is 2 big!!
-
Dec 2nd, 2000, 05:13 AM
#12
Addicted Member
Hi,
Try if Fso.FileExists(filespec) Fso.DeleteFile (filespec)
Regards
-
Dec 2nd, 2000, 11:55 AM
#13
Frenzied Member
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.
-
Dec 2nd, 2000, 01:40 PM
#14
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|