I would like to parse (extract) the date from my filename. Please Can anyone reply.
Giving an example would be helpful. Parsing a date from my_latest_database.fp7 is going to be quite difficult I imagine.
How about giving us an example of your file name ?
TXT_20120323_0001.ZIP. Can you please take this as example.
In this TXT_20120323(Date)_0001.zip
t = FileName ;
p1 = Position ( t ; "_" ; 1 ; 1 ) + 1 ;
p2 = Position ( t ; "_" ; 1 ; 2 ) ;
d = Middle ( t ; p1 ; p2 - p1 ) ;
y = Left ( d ; 4 ) ;
m = Middle ( d ; 5 ; 2 ) ;
d = Right ( d ; 2 )
Date ( m ; d ; y )
And this does of course only continue to work if the 'date' part of the filename is always 8 characters long
jrenfrew wrote: And this does of course only continue to work if the 'date' part of the filename is always 8 characters long
If it's not, then nothing will work.
Perfect. You can save a bit of effort by using MiddleWords instead of position.
Let ( middlewords( "TXT_20120323_0001"; 2 ; 1 ) ; Date ( Middle ( s ; 5 ; 2 ) ; right ( s ; 2 ) ; left( s ; 4 ) ) )
Well, it depends on how many words is "TXT", doesn't it? That's the problem when working with a single example, instead of having clear rules.
In the example there is a boundary character on each side of the numbers representing a date. That makes it a candidate for chunking using the "words" functions. The words functions are a bit more flexible than "position" as many different characters compromise a word boundary. Common variations work with "words", especially important if the file names are generated manually. It even allows for different boundary characters to be used.
Of course, if TXT is a multiword string, it can be calculated from the rear with a combination of LeftWords( RightWords( text ; 3 ) ; 1 )
one more option...is to subsitute the _ with a carriage return. then your working with just the string you need to parse
String = GetValue ( Substitute ( data ; "_" ; ¶ ) ; 2 )
Middle ( String ; 5 ; 2 ) & "/" & Right ( String ; 2 ) & "/" & Left ( String ; 4 ))
Retrieving data ...