Extracting substring from a filename and saving it

2019-09-21 10:59发布

问题:

I am not able to extract filename and save it to a variable in SSIS. I am trying to save a sub string of a filename if and only if it matches the pattern in regex in VIn.

For example:

EIRSRequest_INC9999999.csv //this should extract INC9999999 and save it to VIn variable

public void Main()
{
    String source = @"F:\";
    //String destination = @"F:\";
    Regex regex = new Regex(@"EIRSRequest_(?<val>[a-zA-Z0-9]{1,})");
    DirectoryInfo di = new DirectoryInfo(source);
    //FileInfo fi = new FileInfo(di.GetFiles);

    foreach (FileInfo fi in di.GetFiles())
    {
        var match = regex.Match(Path.GetFileNameWithoutExtension(fi.Name));

        if (match.Success)
        {
            //Console.WriteLine(match.Groups["val"].Value);

               Dts.Variables["User::vIn"].Value= match.Groups["val"].Value;
            //Console.WriteLine(ticket);

            //File.Move(source + "\\" + fi.Name, destination + "\\" + fi.Name);
            //Console.WriteLine("This is a new line");

            break;
        }
    }

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

回答1:

For an SSIS Script task variable to be updated, the variable should be set up as 'Read/Write' variable.

Add your variable to ReadWriteVariables and check.

You should be able to see the official docs as well.

The comma-delimited list of existing variables made available to the package by the user for read/write access.



标签: c# ssis