Deleting files

G

Guest

Basically I am trying to delate any file in my folder with a mdb extension.
Ive tried using the * wildcard below but VBA takes this literally. Anybody
know what I should change?

Bruce


Private Sub myDelete()

Set fs = CreateObject("Scripting.FileSystemObject")

myDest = "C:\myFolder"

'Delete old replicated files
If fs.FileExists(myDest & "*.mdb") Then
fs.DeleteFile myDest & "*.mdb", True
End If

End Sub
 
A

A C

I thought DeleteFile is allowed to contain wildcards, its probably
FileExists that is the problem. So just delete away without looking for the
files in the first place.

You will however need to error trap DeleteFile (well probably more like on
error resume next) in case it cannot find any files that match the wildcard
string, that throws up an error. If you dont want to do that then use the
Dir function instead of FileExists to check if any files exist, Dir is
allowed to contain wildcards.
 
N

Naresh Nichani MVP

Hi:

Try this

Private Sub myDelete()

Set fs = CreateObject("Scripting.FileSystemObject")

checkAGAIN:
myDest = "C:\myFolder"
For each f in fs.GetFolder(myDest).Files
if Ucase(Right(f.Name.4)) = Ucase(".Mdb") then
fs.DeleteFile f.Path
GoTo checkAGAIN
end if
Next

End Sub

Regards,

Naresh Nichani
Microsoft Access MVP
 
A

A C

Hi Naresh

Do you know what the following will do?
Set fs = CreateObject("Scripting.FileSystemObject")

checkAGAIN:
myDest = "C:\myFolder"
For each f in fs.GetFolder(myDest).Files
if Ucase(Right(f.Name.4)) = Ucase(".Mdb") then
fs.DeleteFile f.Path
'****DELETED THIS LINE GoTo checkAGAIN
end if
Next

Lets assume the fso contains 5 objects, and the 2nd object is a file called
xxx.mdb. First time around the loop f points to the 1st file object which
does not contain mdb in the name so it does nothing. Next f moves to the
2nd file object.
2nd time through the loop it detects this file is called xxx.mdb and deletes
it.
Now what???
Next f moves to the ???? file object.

Basically the question is whether a fso operates dynamically or statically,
and whether this matches how the OS maintains it files (or pointers to files
if fso is just a pointer). This sentence is probably full of the wrong
exact lingo, its more the point I am interested in.

Thanks
AC
 
G

Guest

Dim fso, f, f1, fc


Set fso = CreateObject("Scripting.FileSystemObject")

If (fso.FolderExists("C:\myFolder")) Then
Set f = fso.GetFolder("C:\myFolder")
Set fc = f.Files
For Each f1 In fc
If Right(f1.Name, 4) = ".mdb" Then
f1.Delete
End If
Next
End if

set f =nothing
set fc = nothing
set fso = nothing
 
G

Guest

You may want to look at the Kill statement. It seems like it will do what
you want.

Something like:

Kill "C:\myFolder\*.mdb"

HTH, Ted Allen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top