Discussion:
VBS Delete file if exist
(too old to reply)
GTVT06
2007-08-17 17:39:41 UTC
Permalink
Hello, I just have a quick noobie question that I'm sure is easy but I
can't figure it out. (I been trying to figure it out on my own for a
couple hours now)
I was just needing a simple code that will delete a file if it exists,
if not proceed with remaining code. I was thinking it would look
something like below.

dim File as string
File = "D:\MyworkFolder\sheet.csv"
If file.Exists(file) Then
file.Delete(file)
Else
Next
End If
Ivar
2007-08-17 18:19:10 UTC
Permalink
The file may well exist, dosn't mean VB can delete it, the file may be
protected or open or in use or some other reason why it wont get deleted.
The following code will either work of report an error, try this:

Private Function DeleteFile(FullPath as string)As Boolean
On Error GoTo ErrChk
Kill FullPath
DeleteFile = True
Exit Function
MsgBox Err.Number & vbcrlf & Err.Discription
End Function
Post by GTVT06
Hello, I just have a quick noobie question that I'm sure is easy but I
can't figure it out. (I been trying to figure it out on my own for a
couple hours now)
I was just needing a simple code that will delete a file if it exists,
if not proceed with remaining code. I was thinking it would look
something like below.
dim File as string
File = "D:\MyworkFolder\sheet.csv"
If file.Exists(file) Then
file.Delete(file)
Else
Next
End If
Ivar
2007-08-17 18:22:42 UTC
Permalink
Oops, hit send to soon, Try This

Private Sub Command1_Click()
If DeleteFile("D:\MyworkFolder\sheet.csv") Then
DoSomeThing
Else
DoSomethingElse
End If
End Sub

Private Function DeleteFile(FullPath as string)As Boolean
On Error GoTo ErrChk
Kill FullPath
DeleteFile = True
Exit Function
ErrChk:
MsgBox Err.Number & vbcrlf & Err.Discription
End Function
Mike Williams
2007-08-17 18:30:27 UTC
Permalink
Post by GTVT06
Hello, I just have a quick noobie question that I'm sure is
easy but I can't figure it out. (I been trying to figure it out
on my own for a couple hours now) I was just needing a
simple code that will delete a file if it exists, if not proceed
with remaining code.
I don't know what you mean by "VBS" in your subject line (is that VB
Script?) but the following code will work in classic VB (VB6 and earlier)
and also in VBA. There are all sorts of ways of finding out if a file exists
and many people go to great lengths to avoid both the user of the Dir
function and the use of error trapping. Personally, I think for the job you
are doing the simplest way will be the best. Just type the following Sub
into the main section of your code window (use Public instead of Private if
you decide to instead type it into a code module so that the Sub can be used
throughout your app):

Private Sub KillFile(ByVal fname As String)
On Error Resume Next
Kill fname
End Sub

You can then use the Sub in the rest of your code in the following very
simple way:

KillFile "D:\MyworkFolder\sheet.csv"

Mike
GTVT06
2007-08-17 18:37:53 UTC
Permalink
Post by Mike Williams
Post by GTVT06
Hello, I just have a quick noobie question that I'm sure is
easy but I can't figure it out. (I been trying to figure it out
on my own for a couple hours now) I was just needing a
simple code that will delete a file if it exists, if not proceed
with remaining code.
I don't know what you mean by "VBS" in your subject line (is that VB
Script?) but the following code will work in classic VB (VB6 and earlier)
and also in VBA. There are all sorts of ways of finding out if a file exists
and many people go to great lengths to avoid both the user of the Dir
function and the use of error trapping. Personally, I think for the job you
are doing the simplest way will be the best. Just type the following Sub
into the main section of your code window (use Public instead of Private if
you decide to instead type it into a code module so that the Sub can be used
Private Sub KillFile(ByVal fname As String)
On Error Resume Next
Kill fname
End Sub
You can then use the Sub in the rest of your code in the following very
KillFile "D:\MyworkFolder\sheet.csv"
Mike
I meant VB Script. I'll try your code. thanks

Continue reading on narkive:
Loading...