ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Delete file if it starts with a number (https://www.excelbanter.com/excel-programming/354166-delete-file-if-starts-number.html)

Michael Smith

Delete file if it starts with a number
 
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



*** Sent via Developersdex http://www.developersdex.com ***

Chris Marlow

Delete file if it starts with a number
 
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.

--
Chris Marlow
MCSD.NET, Microsoft Office XP Master


"Michael Smith" wrote:

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



*** Sent via Developersdex http://www.developersdex.com ***


Chris Marlow

Delete file if it starts with a number
 
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
--
Chris Marlow
MCSD.NET, Microsoft Office XP Master


"Chris Marlow" wrote:

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.

--
Chris Marlow
MCSD.NET, Microsoft Office XP Master


"Michael Smith" wrote:

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



*** Sent via Developersdex http://www.developersdex.com ***


Michael Smith

Delete file if it starts with a number
 
That's it...thanks!...would have never thought of that.

*** Sent via Developersdex http://www.developersdex.com ***


All times are GMT +1. The time now is 12:25 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com