ho do i delete a file if it exists and if
it dosent resume?
Printable View
ho do i delete a file if it exists and if
it dosent resume?
use KILL command.
but how do i check if the file exists?
Use the Dir() function.
Code:If Dir("C:\MyFile.exe") <> "" Then
'it exists
Kill "C:\MyFile.exe"
Else
'it doesn't exist
End If
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
thanks alot for the help
if it doesn't resume???
what do u mean by that?
Hehehe.Quote:
Originally posted by da_silvy
if it doesn't resume???
what do u mean by that?
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 :rolleyes:.
yeah sort of... thanks
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
gates ur signature is 2 big!!
Hi,
Try if Fso.FileExists(filespec) Fso.DeleteFile (filespec)
Regards
PsyVision, we can drasticly short your code:
And in your code you had twice: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
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)
Silly me and well. I had a go. Even if it was a load 'a' crap.