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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting files with VB

Status
Not open for further replies.

newVBuser

Programmer
Jan 5, 2009
3
Hi,

I've got this code which deletes a file using VB:

Dim FileToDel As String

FileToDel = "C:\SomeFileName.txt"

If System.IO.File.Exists(FileToDel) = True Then

System.IO.File.Delete(FileToDel)
MsgBox("File has been successfully deleted")

End If


Is there a way to delete a collection of files at once when they share the same name but have different extensions? I.e...

FileName.doc
FileName.csv
FileName.ppt
FileName.dbf
FileName.txt
etc....

Thanks

 
I've now stated looking at this code instead, but am still not sure how to specify any extension, i.e...

FileName.AnyExtension


Dim KillFile As String
KillFile = "c:\temp\temp.doc"
'Check that file exists
If Len(Dir$(KillFile)) > 0 Then
'First remove readonly attribute, if set
SetAttr KillFile, vbNormal
'Then delete the file
Kill KillFile
End If
 
Typed, untested:
Code:
strPath = "C:\temp\"
strFile = Dir(strPath & "temp.*")
While strFile <> ""
  SetAttr strPath & strFile, vbNormal
  Kill strPath & strFile
  strFile = Dir()
WEnd

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top