Delete file if it starts with a number

  • Thread starter Thread starter Michael Smith
  • Start date Start date
M

Michael Smith

I would like to delete all files in a folder where the file name starts
with a number. So it would delete 33.xls but not delete Mike.xls

The file in question is C:\Temp

TIA! - Mike
 
Mike,

Set a reference to 'Microsoft Scripting Runtime' & the following should work.

Public Sub DeleteAllNumericStarts()

Dim fso As New FileSystemObject
Dim fsoFile As File

For Each fsoFile In fso.GetFolder("C:\temp")

If Asc(Left(fsoFile.Name, 1)) >= 48 And Asc(Left(fsoFile.Name, 1))
<= 57 Then fsoFile.Delete

Next fsoFile

End Sub

Regards,

Chris.
 
Correction -

Missed .Files on the For ... Each

Public Sub DeleteAllNumericStarts()

Dim fso As New FileSystemObject
Dim fsoFile As File

For Each fsoFile In fso.GetFolder("C:\temp").Files

If Asc(Left(fsoFile.Name, 1)) >= 48 And Asc(Left(fsoFile.Name, 1))
<= 57 Then fsoFile.Delete

Next fsoFile

End Sub
 
Back
Top