Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delelte File if it exists 4

Status
Not open for further replies.

SeaRay

Technical User
Jan 8, 2002
20
0
0
US
I am trying to write code to delete a file if it exists. This is the code that I'm using, but I get an error that says "file not found". What am I doing wrong?

Thanks, Rick Ruscin

Private Sub Command1_Click()
Dim CreditFile As String
Set fs = CreateObject("Scripting.FileSystemObject")

CreditFile = ("C:\Program Files\SSM\001\Credit")
If CreditFile <> &quot;&quot; Then

fs.DeleteFile &quot;C:\Program Files\SSM\001\Credit&quot;

Else

End If

End Sub
 
Now, come on, SeaRay, you're not even trying! What are you doing, trying to get us to write your code for you?

CreditFile = Dir$(&quot;C:\Program Files\SSM\001\Credit&quot;)
Rick Sprague
 
Private Sub Command1_Click()
Dim CreditFile As String
Set fs = CreateObject(&quot;Scripting.FileSystemObject&quot;)
CreditFile = &quot;C:\Program Files\SSM\001\Credit&quot;
If fs.FileExists(CreditFile) Then
fs.GetFile(CreditFile).Delete
End If
End Sub
Compare Code (Text)
Generate Sort in VB or VBScript
 
or even,

function KillFile(sFileName as string) as boolean
'\\returns true if file is found and deleted, else false
KillFile = True
if dir(sFileName)<> &quot;&quot; then
kill sFileName
else
KillFile = false
end if
end function

 
How about something shorter and sweeter...

If Len(Dir(strFilename)) <> 0 Then
Kill strFilename
End If
 
Hi !

The fllowing function would return TRUE if the file specified by the user exists in the directory.

Function FileExist(pFileName As String) as Boolean
Dim I As Integer
I = Len(Dir$(pFileName))
FileExist = IIf(I = 0, False, True)
End Function

Anjana
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top