View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Peter T Peter T is offline
external usenet poster
 
Posts: 5,600
Default Remove DateTimestamp from String

Following will rename files that include "yyyy-" in the filename where is a
year in the loop, eg inlcudes "2008-".

Sub RenameFiles()
Dim i As Long, j As Long
Dim cnt As Long, pos As Long
Dim sPath As String, col As Collection
' be sure to close any "date" named files before running
Set col = New Collection

sPath = Application.DefaultFilePath ' << change to your path

If Right$(sPath, 1) < "\" Then sPath = sPath & "\"
cnt = FilesToCol(sPath, col)
If cnt Then

For i = 1995 To 2012 ' << change to potential years to cater for
For j = 1 To col.Count
pos = InStr(2, col(j), i & "-")
If pos Then
Name sPath & col(j) As sPath & Left$(col(j), pos - 1) &
".xls"
End If
Next
Next
End If
End Sub

Function FilesToCol(sPath As String, c As Collection) As Long
Dim sFile As String
Call Dir("nul")
sFile = Dir(sPath & "*.xls")
Do While Len(sFile)
c.Add sFile
sFile = Dir()
Loop
FilesToCol = c.Count
End Function

Regards,
Peter T

PS this is untested, best read "will" as "should" !


"Steve" wrote in message
...
All,
I'm working on a macro that works with the name of files in a certain
directory.

My problem is that some of the files may have a datetimestamp within
the filename.

example: "Change 69231 Ticket2008-10-01 14.48.18.953.xls"

Would like to change to "Change 69231 Ticket.xls"


How can I find and delete the timestamp within a string like the
above.

Steve