Capture date from a file name
Hi all,
I have to captue the date from a filename which is in a variable. Could someone please help me.
FileName1 : CS_ABC_YYYY_MMDDYYYY.xml From this file name I have to capture the year YYYY in one variable and also the date MMDDYYYY into another variable.
For Example CS_ABC_2009_11192010.xml variable1 should capture 2009 and variable2 should capture 11192010
FileName2 : ABC.DEF.ELIG.09242010.txt From this I have to capture the date 09242010 into a variable.
Thanks
November 19th, 2010 10:34am
You can use a ForEach loop to get the file name into a variable which you parse (read) using an expression or an Script Task.
A good example you can base your solution on is http://www.rafael-salas.com/2007/02/ssis-loop-through-files-in-date-range.html
(read till the end where an expression is presented).Arthur My Blog
Free Windows Admin Tool Kit Click here and download it now
November 19th, 2010 10:36am
Is the pattern that the last 8 characters before the extension the date, in MMddyyyy fomat?Russel Loski, MCT
November 19th, 2010 10:36am
Yes the pattern is mmddyyyy
Free Windows Admin Tool Kit Click here and download it now
November 19th, 2010 10:40am
Script task:
string fileName = Dts.Variables ["User::FileName"].Value.ToString () ;
string name = System.IO.Path.GetFileNameWithoutExtension(fileName);
string dateString = name.Substring(name.Length - 8, 8);
DateTime dt ;
if (DateTime.TryParseExact (dateString , "MMddyyyy", null,System.Globalization.DateTimeStyles.AssumeLocal , out dt)) {
Dts.Variables["User::MyDate"].Value = dt;
}
VB
dim fileName as string = Dts.Variables ("User::FileName").Value.ToString ()
Dim name as string = System.IO.Path.GetFileNameWithoutExtension(fileName)
dim dateString as string = name.Substring(name.Length - 8, 8)
dim dt as DateTime
if (DateTime.TryParseExact (dateString , "MMddyyyy", null,System.Globalization.DateTimeStyles.AssumeLocal , dt)) then
Dts.Variables("User::MyDate").Value = dt
end if
Russel Loski, MCT
November 19th, 2010 11:16am
Or, use expressions:
(DT_DBTIMESTAMP)(SUBSTRING(@[User::FileName], LEN(@[User::FileName]) - 7, 4) + "-" + SUBSTRING(@[User::FileName], LEN(@[User::FileName]) - 11, 2) + "-" + SUBSTRING(@[User::FileName], LEN(@[User::FileName]) - 9, 2))
For more info,
look here.
Talk to me now on
Free Windows Admin Tool Kit Click here and download it now
November 19th, 2010 11:23am
Thank you for your suggestions. I solved the issue using reverse and substring functions.
November 19th, 2010 4:09pm