I have a situation where I need to extract dates from the file names whose general pattern is [XXXX_BBBB]_YYYY-MM-DD[.fileExtension] example Sales_person_2019-05-03.xlsx.

I am using c# in the SSIS script task component to achieve this.

below is my code:

public void Main()
{

            // TODO: Add your code here
   string pat;
   string date;
   string filename = 'Sales_person_2019-05-03.xlsx'

   // Get the Date part from the file name only
   pat = @"[0-9]{2}[0-9]{2}[0-9]{4}";
   Regex r = new Regex(pat, RegexOptions.IgnoreCase);
   date = r.Match(filename);
   MessageBox.Show(date.ToString());}


    Dts.TaskResult = (int)ScriptResults.Success;
}

but this is not working. Can someone help, please. Newbie on C#

1 Answers

1
Hadi On

You can achieve this without regular expressions, just use string functions (IndexOf() and Substring()):

Since you are handling fixed pattern [XXXX_BBBB]_YYYY-MM-DD[.fileExtension], just retrieve the 10 characters located after the second underscore.

public void Main()
{

    string filename = "Sales_person_2019-05-03.xlsx";

    // Get the Date part from the file name only
    string filedate = filename.Substring(filename.IndexOf('_',filename.IndexOf('_') + 1) + 1,10);

    DateTime dt = DateTime.ParseExact(filedate, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None)

    Dts.TaskResult = (int)ScriptResults.Success;
}