0
votes

I am new to SSIS . I am trying to use Script Task to get the last modified date and create date of a file. I have declared two variables to read the file path and file name (File_Path,Filename) in my script task as variables with scope as package and datatype as string.

I want to store the create date and modified date to two diff output variables(Create_Date,Last_Updated) with datatype as Datetime.

my code for the script is as follows

FileInfo fileInfo = new FileInfo(Path.Combine(Dts.Variables["File_Path"].Value.ToString(), Dts.Variables["Filename"].Value.ToString()));

                if (fileInfo.Exists)
                {
                    // Get file creation date
                    Dts.Variables["Create_Date"].Value = fileInfo.CreationTime;

                    // Get last modified date
                    Dts.Variables["Last_Updated"].Value = fileInfo.LastWriteTime;

                }
                else
                {
                    
                    Dts.Events.FireWarning(1, Dts.Variables["System::TaskName"].Value.ToString()
                          , string.Format("File '{0}' does not exist", fileInfo.FullName)
                          , "", 0);
                }```
1
Does your warning fire? How are you determining that the value isn't being updated? If you fire a Dts.Events.FireInformation event after setting the second variable, does do you see the expected updated values? - billinkc
You can add a MessageBox in your SSIS script task during debugging as well to just print values of variables and they pop up in your SSIS package during running in debug mode. It is not necessarily the best but you can do it multiple times anywhere in your code and you want to remove them when your done debugging, but it is helpful. - Brad
In your script task box did you put the variable in the ReadWriteVariable section? Also I usually have done it like this (I needed USER in the variable when setting): Dts.Variables["User::VariableNameHere"].Value - Brad
@billinkc the warning did not fire as the code runs successfully. I am expecting to see the values of the variable change in the variable section of script task. - SC1
What makes you think they aren't being stored? Note that when you drop out of debug mode in SSDT, these variables will go back to their original values. So don't base your assumption on that. If you add a subsequent step to this task, you'll see that during runtime the variables do contain the values. But if you inspect the variable window after dropping out of debug mode, they'll go back to the original (design time) values - Nick.McDermaid

1 Answers

1
votes

SSIS has a Design time and a Run time interface.

Variables are created in the Design time space. There you assign data type and a value. There is an explicit Variable's window that you do all of this with.

During run time, the Variables window will still be visible but the values there are not the run-time value. It's just a reference for what the package was initialized with. The actual values of SSIS variables are to be found in the debug windows. I favor the Locals window (Ctrl+Alt+V, L)

Debug menu, Windows sub menu, Locals highlighted

From there, expand the Variables node

enter image description here

You can also add explicit logging into your Script tasks. This little bit will enumerate through all the variables you selected for readonly or read/write access and pop off their name and value into the run log. If you're running in Visual Studio, it will show up in the Results tab or the Output window (great place to copy errors for further research or asking on forums). If you're running from the server, these will show in the SSISDB.catalog.operation_messages view (unless you picked an incompatible logging mode)

bool fireAgain = false;
string message = "{0}::{1} : {2}";
foreach (var item in Dts.Variables)
{
    Dts.Events.FireInformation(0, "SCR Echo Back", string.Format(message, item.Namespace, item.Name, item.Value), string.Empty, 0, ref fireAgain);
}