View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
urkec urkec is offline
external usenet poster
 
Posts: 131
Default Deleting files in a directory

You can also use the FileSystemObject object for that.

Loop through the files in C:\GLPVC\ , check if the file type is "Type" and
the first character in the file name is numeric, and, if so, delete the file:

Sub deleteFiles()

fPath = "C:\GLPVC\"

Set fso = CreateObject _
("Scripting.FileSystemObject")

Set folder = fso.GetFolder(fPath)

For Each file In folder.Files

If file.Type = "File" And _
IsNumeric(Left(file.Name, 1)) Then
Debug.Print file.Name
'file.Delete
End If

Next

Set folder = Nothing
Set fso = Nothing

End Sub


Instead of checking the file type, you can delete all the files without
extension:


Sub deleteFiles1()

fPath = "C:\GLPVC\"

Set fso = CreateObject _
("Scripting.FileSystemObject")

Set folder = fso.GetFolder(fPath)

For Each file In folder.Files

If fso.GetExtensionName(file) = "" And _
IsNumeric(Left(file.Name, 1)) Then
Debug.Print file.Name
'file.Delete
End If

Next

Set folder = Nothing
Set fso = Nothing

End Sub




--
urkec


"Sam" wrote:

I have a process that creates csv files in a directory, however a byproduct
is an identical log file, e.g. 5C015000. If you were to rename the file with
a .xls extension, it would be exactly the same as the csv that I'm saving. It
has no extension and is file type "File".

If the directory were C:\GLPVC\ what code can I use to loop through all
files in the directory (there may be hundreds) and delete all files that
begin with a numeric character or whose file type is "File"?