Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
275 views
in Technique[技术] by (71.8m points)

c# - Extracting substring from a filename and saving it to a variable in SSIS

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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

enter image description here

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...