Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 15
Default Remove DateTimestamp from String

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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 206
Default Remove DateTimestamp from String

This assumes a 4-digit year, a "-" following the year, and removal of all
text to the right of the year.

Sub test()
Dim Mystr As String

Mystr = "Change 69231 Ticket2008-10-01 14.48.18.953.xls"
Mystr = Left(Mystr, InStr(Mystr, "-") - 5) & ".xls"
End Sub

Mike F
"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



  #3   Report Post  
Posted to microsoft.public.excel.programming
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



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Remove DateTimestamp from String

Not sure of the OP's naming system, but you could get a false hit with a
name like this...

Mystr = "Change 69231-A Ticket2008-10-01 14.48.18.953.xls"

--
Rick (MVP - Excel)


"Mike Fogleman" wrote in message
...
This assumes a 4-digit year, a "-" following the year, and removal of all
text to the right of the year.

Sub test()
Dim Mystr As String

Mystr = "Change 69231 Ticket2008-10-01 14.48.18.953.xls"
Mystr = Left(Mystr, InStr(Mystr, "-") - 5) & ".xls"
End Sub

Mike F
"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




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Remove DateTimestamp from String

Assuming that the structure of the date portion of the filename is this
*fixed* pattern..

yyyy-mm-dd hh.mm.ss.sss

then, assuming we are talking about .xls files, this should work...

If FileName Like "*####-##-## ##.##.##.###.xls" Then
FileName = Left(FileName, Len(FileName) - 27) & ".xls"
End If

--
Rick (MVP - Excel)


"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




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 206
Default Remove DateTimestamp from String

For any file extension use:

Mystr = Left(Mystr, InStr(Mystr, "-") - 5) & _
Right(Mystr, Len(Mystr) - InStrRev(Mystr, ".") + 1)

Mike F
"Mike Fogleman" wrote in message
...
This assumes a 4-digit year, a "-" following the year, and removal of all
text to the right of the year.

Sub test()
Dim Mystr As String

Mystr = "Change 69231 Ticket2008-10-01 14.48.18.953.xls"
Mystr = Left(Mystr, InStr(Mystr, "-") - 5) & ".xls"
End Sub

Mike F
"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





  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Remove DateTimestamp from String

Here is the solution for any extension...

If FileName Like "*####-##-## ##.##.##.###.*" Then
Filename = Left(Filename, InStrRev(Filename, ".") - 24) & _
Mid(Filename, InStrRev(Filename, "."))
End If

--
Rick (MVP - Excel)


"Rick Rothstein" wrote in message
...
Assuming that the structure of the date portion of the filename is this
*fixed* pattern..

yyyy-mm-dd hh.mm.ss.sss

then, assuming we are talking about .xls files, this should work...

If FileName Like "*####-##-## ##.##.##.###.xls" Then
FileName = Left(FileName, Len(FileName) - 27) & ".xls"
End If

--
Rick (MVP - Excel)


"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



  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Remove DateTimestamp from String

The OP might want to verify his datestamps always include seconds to 3dp
before using this excellent suggestion, that is if indeed the OP has seen
any of these suggestions.

Regards,
Peter T


"Rick Rothstein" wrote in message
...
Here is the solution for any extension...

If FileName Like "*####-##-## ##.##.##.###.*" Then
Filename = Left(Filename, InStrRev(Filename, ".") - 24) & _
Mid(Filename, InStrRev(Filename, "."))
End If

--
Rick (MVP - Excel)


"Rick Rothstein" wrote in message
...
Assuming that the structure of the date portion of the filename is this
*fixed* pattern..

yyyy-mm-dd hh.mm.ss.sss

then, assuming we are talking about .xls files, this should work...

If FileName Like "*####-##-## ##.##.##.###.xls" Then
FileName = Left(FileName, Len(FileName) - 27) & ".xls"
End If

--
Rick (MVP - Excel)


"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





  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Remove DateTimestamp from String

If the number of decimal places in the seconds can vary (that is, they are
not fixed at 3 decimal places), then something like this would be needed...

For X = 1 To Len(FN)
If Mid(FN, X) Like "####-##-## ##.##.##*" Then
FN = Left(FN, X - 1) & Mid(FN, InStrRev(FN, "."))
Exit For
End If
Next

where I changed the variable name from FileName to FN to make sure one of
the longer lines wouldn't word wrap in the newsgroup reader.

--
Rick (MVP - Excel)


"Peter T" <peter_t@discussions wrote in message
...
The OP might want to verify his datestamps always include seconds to 3dp
before using this excellent suggestion, that is if indeed the OP has seen
any of these suggestions.

Regards,
Peter T


"Rick Rothstein" wrote in message
...
Here is the solution for any extension...

If FileName Like "*####-##-## ##.##.##.###.*" Then
Filename = Left(Filename, InStrRev(Filename, ".") - 24) & _
Mid(Filename, InStrRev(Filename, "."))
End If

--
Rick (MVP - Excel)


"Rick Rothstein" wrote in message
...
Assuming that the structure of the date portion of the filename is this
*fixed* pattern..

yyyy-mm-dd hh.mm.ss.sss

then, assuming we are talking about .xls files, this should work...

If FileName Like "*####-##-## ##.##.##.###.xls" Then
FileName = Left(FileName, Len(FileName) - 27) & ".xls"
End If

--
Rick (MVP - Excel)


"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





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Remove the right most 3 characters from a string Jonathan Brown Excel Programming 10 October 29th 08 10:44 AM
remove text from string Todd Excel Worksheet Functions 3 May 25th 06 11:10 PM
Remove specified value from string Todd Huttenstine Excel Programming 1 January 31st 06 07:32 PM
Remove specified string from value Todd Huttenstine Excel Programming 1 January 31st 06 05:13 PM
Remove specified value from string Todd Huttenstine Excel Programming 2 January 31st 06 04:48 PM


All times are GMT +1. The time now is 10:49 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"