Skip to content
Advertisement

SSIS Script C# Script Task to Add current date to a file using a variable

Im have this script to add current year month to a file in a directory. It works perfect with the directory path, but I want to change the directory patch for a variable, because every month its going to be in a different folder. I already have the variable with the directory to use. But what I dont know how to do is, use the variable instead of the directory

    public void Main()
    {
        // TODO: Add your code here

        // const string DIRECTORY_PATH = @"C:TempResults"; //with this works perfect
        const string DIRECTORY_PATH =    @"@v_ResultsExcel"; //I want to use this variable

        const string FILE_NAME_TEMPLATE = "YYYY-MM";

        string previousYearMonthDate = DateTime.Now.AddYears(0).AddMonths(0).ToString("yyyy-MM");

        if (Directory.Exists(DIRECTORY_PATH))
            {
            string[] filePathList = Directory.GetFiles(DIRECTORY_PATH);

            foreach (string filepath in filePathList)
            {
                if (File.Exists(filepath))
                {
                    File.Move(filepath, filepath.Replace(FILE_NAME_TEMPLATE, previousYearMonthDate));
                }
            }
            }
        Dts.TaskResult = (int)ScriptResults.Success;
    }

Advertisement

Answer

Create a variable in your SSIS package. In the script task ReadOnlyVariables select User::varDir. C# code call the read only variable. You can write the expression for this variable to point to the appropriate directory each month.

const string DIRECTORY_PATH =  Dts.Variables["User::varDir"].Value.ToString();
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement