Delete temp files

S

Sam

I need a code that will delete all files in a directory that are of a certain
type.

We have an issue with one of our servers whereby the temp files that are
created when an ".xls" file saves are not deleted when the file saves. They
are named: "29D41410", "31138510", etc. and do not contain file extensions.
Properties list the Type of file as "File". Our netword amins are stumped but
until a solution is found, I would like to enter a code into the Workbook
Before_Close event to loop through all files in the directory and delete
these junk files.

Can anyone help?

Thanks,

Sam
 
R

Rick Rothstein

You say "of a certain type"... does that mean there are other files in the
directory that you do not want to delete? If so, do they have file
extensions on them or not?
 
S

Sam

The other files are the original Excel workbooks that are being saved. They
have the ".xls" extension. I want to loop through all files in the directory
and only delete the Types "File" as explained in my original message.

Thanks, Rick. Can this be done?
 
R

Rick Rothstein

This subroutine will remove **ANY** file in the specified directory whose
filename (as returned by the Dir function) does *not* have a dot in it.

Sub RemoveFilesWithNoExtensions(Directory As String)
Dim FileName As String
FileName = Dir(Directory & "\*")
Do While Len(FileName)
If InStr(FileName, ".") = 0 Then Kill Directory & "\" & FileName
FileName = Dir
Loop
End Sub

You would call it from your own function something like this...

Sub TestIt()
'
' beginning code
'
RemoveFilesWithNoExtensions "D:\Temp\TestFolder"
'
'
'
End Sub

Now, I would advise you to test the subroutine out on a copy of your
directory to make sure it works the way you want before you use it on a
directory containing real data... that is because the Kill statement that I
am using permanently deletes any files it processed (it does NOT send them
to the Recycle Bin).
 
S

Sam

Thanks! I'll give it a try.

Rick Rothstein said:
This subroutine will remove **ANY** file in the specified directory whose
filename (as returned by the Dir function) does *not* have a dot in it.

Sub RemoveFilesWithNoExtensions(Directory As String)
Dim FileName As String
FileName = Dir(Directory & "\*")
Do While Len(FileName)
If InStr(FileName, ".") = 0 Then Kill Directory & "\" & FileName
FileName = Dir
Loop
End Sub

You would call it from your own function something like this...

Sub TestIt()
'
' beginning code
'
RemoveFilesWithNoExtensions "D:\Temp\TestFolder"
'
'
'
End Sub

Now, I would advise you to test the subroutine out on a copy of your
directory to make sure it works the way you want before you use it on a
directory containing real data... that is because the Kill statement that I
am using permanently deletes any files it processed (it does NOT send them
to the Recycle Bin).
 

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