Kill files

K

Kathy Webster

I want to delete all files in a folder, and only error show the error
message if one or more of these files happens to be locked for any reason.
Unfortunately, this code is showing error message even when the folder is
empty, not when there is really a problem with deleting one of the files:

Public Function kill_tempfiles() As Boolean

On Local Error GoTo Err

Dim x As String
x = "K:\tempfiles\*.*"
Kill x
Exit Function
Err:
' MsgBox "We are sorry, but there is a problem with one or more of the
temp files in your temp file folder."

End Function

Thanks in advance.
 
K

Klatuu

Check to see if the file exists before you delete it.

Dim x As String

x = Dir("K:\tempfiles\*.*")
do while Len(x) > 0
Kill x
x = Dir()
Loop
Exit Function

Err:
 
D

Douglas J. Steele

I think you'd need

Dim x As String

x = Dir("K:\tempfiles\*.*")
do while Len(x) > 0
Kill "K:\tempfiles\" & x
x = Dir()
Loop
Exit Function
 
K

Klatuu

I thought so too, but I tested it and it did it without the path, but maybe I
was in my default path when I tested it.
 
K

Klatuu

Look in VBA Help for the Dir function
It returns the next file name in the path you specify with the first call to
it. It continutes returning file names until there are no more in the list.
Then it returns ""
 
D

Douglas J. Steele

That implies that something in the code set CurDrive to point to K, and
CurDir to point to K:\tempfiles, or if the database was opened using a
shortcut, the shortcut's Startin folder is K:\tempfile.

Personally, I'd hate to rely on that assumption.
 

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