SSIS C# 脚本修改以在 loop/list 中获取文件创建时间

SSIS C# scrip modification to get file created time in loop/list

我正在使用带有脚本任务的 SSIS 包来获取不早于 n 天的文件并且它工作正常,但现在我需要为每个文件进入下一步 CreatedTime。下面我粘贴了我的脚本的主体。它部分起作用我只是不能将新变量传递到 LastUpdated。坦率地说,我不知道如何处理这个结构,我可以将另一个维度添加到现有列表中创建另一个列表。我打算像使用 FileNameArray 一样使用 User:LastUpdated。 非常感谢!)

        DataTable NewList = new DataTable();
        DataColumn col = new DataColumn("FileName");
        NewList.Columns.Add(col);
          DataColumn col2 = new DataColumn("LastUpdated", System.Type.GetType("System.DateTime"));
          NewList.Columns.Add(col2); 

        foreach (string f in MyDirFiles)
        {
            finf = new System.IO.FileInfo(f);
            if (finf.LastWriteTime > DateTime.Now.AddDays(-7)  )        
                )
            {
               NewList.Rows.Add(System.IO.Path.GetFileName(f) , 
                                System.IO.File.GetCreationTime(f));        
            }
        }
        Dts.Variables["User::FileNameArray"].Value = NewList.Columns["FileName"];  //<--- need convert into object
        ////**Dts.Variables["User::LastUpdated"].Value = NewList(xxx);  


        Dts.TaskResult = (int)ScriptResults.Success;

根据您的代码和评论 - 可以得出以下结论:

  • NewList2 变量具有 DataTable 类型(代码中不存在)
  • User:LastUpdated SSIS 包变量具有 DateTime 类型

在这种情况下 - 您试图将复杂结构 (DataTable) 分配给单值 DateTime 变量,这肯定会引发错误。为此,将 User:LastUpdated 的类型更改为 Object

可以扩展 NewList table 以包含两列,如下例所示

DataTable NewList = new DataTable();
DataColumn col = new DataColumn("FileName");
NewList.Columns.Add(col);
DataColumn col2 = new DataColumn("LastUpdated", System.Type.GetType("System.DateTime"));
NewList.Columns.Add(col2);

添加新行会比较尴尬。

DataRow newRow = NewList.NewRow();
newRow["FileName"] = System.IO.Path.GetFileName(f);
newRow["LastUpdated"] = System.IO.File.GetCreationTime(f);
NewList.Rows.Add(newRow);