SSIS脚本C#使用变量将当前日期添加到文件的脚本任务



我用这个脚本将当前年份月份添加到目录中的文件中。它与目录路径配合得很好,但我想更改变量的目录补丁,因为每个月它都会在不同的文件夹中。我已经有了要使用的带有目录的变量。但我不知道如何使用变量而不是目录

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;
}

在SSIS包中创建一个变量。在脚本任务ReadOnlyVariables中,选择User::varDir。C#代码调用只读变量。您可以为该变量编写表达式,以便每个月指向相应的目录。

const string DIRECTORY_PATH =  Dts.Variables["User::varDir"].Value.ToString();

最新更新