Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File 如何使用SSIS包中的Foreach循环容器选择最近创建的文件夹?_File_Ssis_Ienumerator - Fatal编程技术网

File 如何使用SSIS包中的Foreach循环容器选择最近创建的文件夹?

File 如何使用SSIS包中的Foreach循环容器选择最近创建的文件夹?,file,ssis,ienumerator,File,Ssis,Ienumerator,我在SSIS方面遇到了一个有趣的挑战。使用for-each文件枚举器,我需要选择最近创建的子文件夹,然后遍历每个文件 也许一个例子可以更好地解释。文件夹如下所示: c:\data\2011-0703 c:\data\2011-0626 c:\data\2011-0619 如何为每个文件枚举器获取一个用于选择最新文件夹的值?这可以通过查看创建日期或比较文件名来实现 我猜这将通过枚举器中的表达式完成,只是不知道如何完成!在网上也找不到任何东西 谢谢反复浏览这些文件夹。保存第一个的名称。将保存的值与每

我在SSIS方面遇到了一个有趣的挑战。使用for-each文件枚举器,我需要选择最近创建的子文件夹,然后遍历每个文件

也许一个例子可以更好地解释。文件夹如下所示:

c:\data\2011-0703

c:\data\2011-0626

c:\data\2011-0619

如何为每个文件枚举器获取一个用于选择最新文件夹的值?这可以通过查看创建日期或比较文件名来实现

我猜这将通过枚举器中的表达式完成,只是不知道如何完成!在网上也找不到任何东西


谢谢

反复浏览这些文件夹。保存第一个的名称。将保存的值与每个后续文件夹的名称进行比较。如果下一个文件夹是较新的文件夹,请插入该名称并继续。最后,保存的值将是最近文件夹的名称(如果要比较创建日期,则需要同时保存文件夹名称和创建日期)


然后,您可以使用保存的值作为第二个迭代循环的参数。

遍历文件夹。保存第一个的名称。将保存的值与每个后续文件夹的名称进行比较。如果下一个文件夹是较新的文件夹,请插入该名称并继续。最后,保存的值将是最近文件夹的名称(如果要比较创建日期,则需要同时保存文件夹名称和创建日期)


然后,您可以使用保存的值作为第二个迭代循环的参数。

这里有一个可能的选项,您可以借助
脚本任务来实现这一点。下面的示例显示了如何做到这一点。该示例是在SSIS 2008 R2中创建的

逐步过程:

  • 在文件夹路径
    C:\temp\
    中创建三个名为
    2011-0619
    2011-0626
    2011-0703
    的文件夹,如屏幕截图1所示。记下每个文件夹的创建日期

  • 在每个文件夹中放置几个文件,如屏幕截图#2-#4所示

  • public void Main()
    {
        Variables varCollection = null;
    
        Dts.VariableDispenser.LockForRead("User::RootFolder");
        Dts.VariableDispenser.LockForWrite("User::RecentFolder");
        Dts.VariableDispenser.GetVariables(ref varCollection);
    
        string rootFolder = varCollection["User::RootFolder"].Value.ToString();
        DateTime previousFolderTime = DateTime.MinValue;
        string recentFolder = string.Empty;
    
        foreach (string subFolder in System.IO.Directory.GetDirectories(rootFolder))
        {
            DateTime currentFolderTime = System.IO.Directory.GetCreationTime(subFolder);
            if (previousFolderTime == DateTime.MinValue || previousFolderTime <= currentFolderTime)
            {
                previousFolderTime = currentFolderTime;
                recentFolder = subFolder;
            }
        }
    
        varCollection["User::RecentFolder"].Value = recentFolder;
    
        Dts.TaskResult = (int)ScriptResults.Success;
    }
    
    public void Main()
    {
        Variables varCollection = null;
    
        Dts.VariableDispenser.LockForRead("User::FilePath");
        Dts.VariableDispenser.GetVariables(ref varCollection);
    
        MessageBox.Show(varCollection["User::FilePath"].Value.ToString(), "File Path");
    
        Dts.TaskResult = (int)ScriptResults.Success;
    }
    
  • 在SSIS包上,创建四个变量,如屏幕截图#5所示。使用值
    C:\temp\
    设置变量RootFolder(在您的情况下,这将是C:\data)。使用值
    *.
    设置变量FilePattern。变量RecentFolder将与脚本任务中的最近文件夹路径一起分配。为避免设计时错误,请为变量RecentFolder指定有效的文件路径。当文件在最近的文件夹中循环时,变量FilePath将被指定值

  • 在SSIS包上,放置一个脚本任务。将脚本任务中的Main()方法替换为脚本任务代码(获取最新文件夹):部分下给出的脚本任务代码。此脚本获取根文件夹中的文件夹列表,并循环检查创建日期时间以选择最近创建的文件夹。最近创建的文件夹路径随后存储在变量RecentFolder

  • public void Main()
    {
        Variables varCollection = null;
    
        Dts.VariableDispenser.LockForRead("User::RootFolder");
        Dts.VariableDispenser.LockForWrite("User::RecentFolder");
        Dts.VariableDispenser.GetVariables(ref varCollection);
    
        string rootFolder = varCollection["User::RootFolder"].Value.ToString();
        DateTime previousFolderTime = DateTime.MinValue;
        string recentFolder = string.Empty;
    
        foreach (string subFolder in System.IO.Directory.GetDirectories(rootFolder))
        {
            DateTime currentFolderTime = System.IO.Directory.GetCreationTime(subFolder);
            if (previousFolderTime == DateTime.MinValue || previousFolderTime <= currentFolderTime)
            {
                previousFolderTime = currentFolderTime;
                recentFolder = subFolder;
            }
        }
    
        varCollection["User::RecentFolder"].Value = recentFolder;
    
        Dts.TaskResult = (int)ScriptResults.Success;
    }
    
    public void Main()
    {
        Variables varCollection = null;
    
        Dts.VariableDispenser.LockForRead("User::FilePath");
        Dts.VariableDispenser.GetVariables(ref varCollection);
    
        MessageBox.Show(varCollection["User::FilePath"].Value.ToString(), "File Path");
    
        Dts.TaskResult = (int)ScriptResults.Success;
    }
    
  • 在SSIS包上,放置一个Foreach循环容器,并按照屏幕截图#6和#7所示对其进行配置

  • public void Main()
    {
        Variables varCollection = null;
    
        Dts.VariableDispenser.LockForRead("User::RootFolder");
        Dts.VariableDispenser.LockForWrite("User::RecentFolder");
        Dts.VariableDispenser.GetVariables(ref varCollection);
    
        string rootFolder = varCollection["User::RootFolder"].Value.ToString();
        DateTime previousFolderTime = DateTime.MinValue;
        string recentFolder = string.Empty;
    
        foreach (string subFolder in System.IO.Directory.GetDirectories(rootFolder))
        {
            DateTime currentFolderTime = System.IO.Directory.GetCreationTime(subFolder);
            if (previousFolderTime == DateTime.MinValue || previousFolderTime <= currentFolderTime)
            {
                previousFolderTime = currentFolderTime;
                recentFolder = subFolder;
            }
        }
    
        varCollection["User::RecentFolder"].Value = recentFolder;
    
        Dts.TaskResult = (int)ScriptResults.Success;
    }
    
    public void Main()
    {
        Variables varCollection = null;
    
        Dts.VariableDispenser.LockForRead("User::FilePath");
        Dts.VariableDispenser.GetVariables(ref varCollection);
    
        MessageBox.Show(varCollection["User::FilePath"].Value.ToString(), "File Path");
    
        Dts.TaskResult = (int)ScriptResults.Success;
    }
    
  • 将脚本任务放置在Foreach循环容器中。将脚本任务中的Main()方法替换为脚本任务代码(显示文件名):部分下给出的脚本任务代码。此脚本仅显示最近创建的文件夹中的文件名

  • 配置完所有任务后,软件包应如屏幕截图#8所示

  • public void Main()
    {
        Variables varCollection = null;
    
        Dts.VariableDispenser.LockForRead("User::RootFolder");
        Dts.VariableDispenser.LockForWrite("User::RecentFolder");
        Dts.VariableDispenser.GetVariables(ref varCollection);
    
        string rootFolder = varCollection["User::RootFolder"].Value.ToString();
        DateTime previousFolderTime = DateTime.MinValue;
        string recentFolder = string.Empty;
    
        foreach (string subFolder in System.IO.Directory.GetDirectories(rootFolder))
        {
            DateTime currentFolderTime = System.IO.Directory.GetCreationTime(subFolder);
            if (previousFolderTime == DateTime.MinValue || previousFolderTime <= currentFolderTime)
            {
                previousFolderTime = currentFolderTime;
                recentFolder = subFolder;
            }
        }
    
        varCollection["User::RecentFolder"].Value = recentFolder;
    
        Dts.TaskResult = (int)ScriptResults.Success;
    }
    
    public void Main()
    {
        Variables varCollection = null;
    
        Dts.VariableDispenser.LockForRead("User::FilePath");
        Dts.VariableDispenser.GetVariables(ref varCollection);
    
        MessageBox.Show(varCollection["User::FilePath"].Value.ToString(), "File Path");
    
        Dts.TaskResult = (int)ScriptResults.Success;
    }
    
  • 屏幕截图#9-#11显示程序包显示最近创建的文件夹2011-0703中的文件名

  • public void Main()
    {
        Variables varCollection = null;
    
        Dts.VariableDispenser.LockForRead("User::RootFolder");
        Dts.VariableDispenser.LockForWrite("User::RecentFolder");
        Dts.VariableDispenser.GetVariables(ref varCollection);
    
        string rootFolder = varCollection["User::RootFolder"].Value.ToString();
        DateTime previousFolderTime = DateTime.MinValue;
        string recentFolder = string.Empty;
    
        foreach (string subFolder in System.IO.Directory.GetDirectories(rootFolder))
        {
            DateTime currentFolderTime = System.IO.Directory.GetCreationTime(subFolder);
            if (previousFolderTime == DateTime.MinValue || previousFolderTime <= currentFolderTime)
            {
                previousFolderTime = currentFolderTime;
                recentFolder = subFolder;
            }
        }
    
        varCollection["User::RecentFolder"].Value = recentFolder;
    
        Dts.TaskResult = (int)ScriptResults.Success;
    }
    
    public void Main()
    {
        Variables varCollection = null;
    
        Dts.VariableDispenser.LockForRead("User::FilePath");
        Dts.VariableDispenser.GetVariables(ref varCollection);
    
        MessageBox.Show(varCollection["User::FilePath"].Value.ToString(), "File Path");
    
        Dts.TaskResult = (int)ScriptResults.Success;
    }
    
    希望有帮助

    脚本任务代码(获取最新文件夹):

    C#只能在SSIS 2008及以上版本中使用的代码

    public void Main()
    {
        Variables varCollection = null;
    
        Dts.VariableDispenser.LockForRead("User::RootFolder");
        Dts.VariableDispenser.LockForWrite("User::RecentFolder");
        Dts.VariableDispenser.GetVariables(ref varCollection);
    
        string rootFolder = varCollection["User::RootFolder"].Value.ToString();
        DateTime previousFolderTime = DateTime.MinValue;
        string recentFolder = string.Empty;
    
        foreach (string subFolder in System.IO.Directory.GetDirectories(rootFolder))
        {
            DateTime currentFolderTime = System.IO.Directory.GetCreationTime(subFolder);
            if (previousFolderTime == DateTime.MinValue || previousFolderTime <= currentFolderTime)
            {
                previousFolderTime = currentFolderTime;
                recentFolder = subFolder;
            }
        }
    
        varCollection["User::RecentFolder"].Value = recentFolder;
    
        Dts.TaskResult = (int)ScriptResults.Success;
    }
    
    public void Main()
    {
        Variables varCollection = null;
    
        Dts.VariableDispenser.LockForRead("User::FilePath");
        Dts.VariableDispenser.GetVariables(ref varCollection);
    
        MessageBox.Show(varCollection["User::FilePath"].Value.ToString(), "File Path");
    
        Dts.TaskResult = (int)ScriptResults.Success;
    }
    
    屏幕截图#1:

    截图#2:

    屏幕截图#3:

    屏幕截图#4:

    屏幕截图#5:

    屏幕截图#6:

    截图#7:

    截图#8:

    截图#9:

    截图#10:

    截图#11:


    这里有一个可能的选项,您可以在
    脚本任务的帮助下实现这一点。下面的示例显示了如何做到这一点。该示例是在SSIS 2008 R2中创建的

    逐步过程:

  • 在文件夹路径
    C:\temp\
    中创建三个名为
    2011-0619
    2011-0626
    2011-0703
    的文件夹,如屏幕截图1所示。记下每个文件夹的创建日期

  • 在每个文件夹中放置几个文件,如屏幕截图#2-#4所示

  • public void Main()
    {
        Variables varCollection = null;
    
        Dts.VariableDispenser.LockForRead("User::RootFolder");
        Dts.VariableDispenser.LockForWrite("User::RecentFolder");
        Dts.VariableDispenser.GetVariables(ref varCollection);
    
        string rootFolder = varCollection["User::RootFolder"].Value.ToString();
        DateTime previousFolderTime = DateTime.MinValue;
        string recentFolder = string.Empty;
    
        foreach (string subFolder in System.IO.Directory.GetDirectories(rootFolder))
        {
            DateTime currentFolderTime = System.IO.Directory.GetCreationTime(subFolder);
            if (previousFolderTime == DateTime.MinValue || previousFolderTime <= currentFolderTime)
            {
                previousFolderTime = currentFolderTime;
                recentFolder = subFolder;
            }
        }
    
        varCollection["User::RecentFolder"].Value = recentFolder;
    
        Dts.TaskResult = (int)ScriptResults.Success;
    }
    
    public void Main()
    {
        Variables varCollection = null;
    
        Dts.VariableDispenser.LockForRead("User::FilePath");
        Dts.VariableDispenser.GetVariables(ref varCollection);
    
        MessageBox.Show(varCollection["User::FilePath"].Value.ToString(), "File Path");
    
        Dts.TaskResult = (int)ScriptResults.Success;
    }
    
  • 在SSIS包上,创建四个变量,如屏幕截图#5所示。使用值
    C:\temp\
    设置变量RootFolder(在您的情况下,这将是C:\data)。使用值
    *.
    设置变量FilePattern。变量RecentFolder将与脚本任务中的最近文件夹路径一起分配。为避免设计时错误,请为变量RecentFolder指定有效的文件路径。变数<