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

How to Delete a certain Filetype with FSO

Status
Not open for further replies.

CMPaccess

Technical User
Dec 18, 2003
52
AU
I have been trying to write a sub which would look in a folder and delete only certain file types.

for example the folder would have three file types but I only want to get rid of 2.

I hav tried something like this.


Dim PathnameDGN
Dim PathnameDWG
PathnameDWG = DrgDest & "*.dwg"
PathnameDGN = DrgDest & "*.dgn"

Set ds = CreateObject("Scripting.FileSystemObject")
If Not ds.FileExists(PathnameDWG) Then
ds.DeleteFile (PathnameDGN)
Else
ds.DeleteFile (PathnameDWG)
End If

This partially works. Say if the folder contains dgn files it will work but not dwg files unless I switch them round in the code which defeats the object.

Is there a more defined way I can specify which filetypes are to be deleted.

Thansk
 
Ok,

I have cracked it. For those that maybe interested in the future here is how I managed it. In saying that I would appreciate any feedback

thanks

CODE

'Make a new File System object.
Dim fso As FileSystemObject
Dim fsoFolder As Folder
Dim fsoFile As File
Set fso = New FileSystemObject
' Get the FSO Folder (directory) object.
If Len(Dir(DrgDest)) Then
Set fsoFolder = fso.GetFolder(DrgDest)

For Each fsoFile In fsoFolder.Files
If Right(fsoFile.Name, 4) = ".dwg" Then
fsoFile.Delete
Else
If Right(fsoFile.Name, 4) = ".dgn" Then
fsoFile.Delete
End If
End If
Next fsoFile
Else
MsgBox "Invalid Path"
End If
 
Glad you fixed it. You might try using InStr to find all the extensions in a single line. Something like:
Code:
strExt = Right(fsoFile.Name, 4)
If Instr(1, ".dwg.dgn", strExt, 1) > 0
  fsoFile.Delete
End If
Doesn't help much when there are only two file extensions but it would make maintenance easier.

Geoff Franklin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top