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

发布于 2024-11-18 03:56:33 字数 296 浏览 4 评论 0原文

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

也许举个例子会更好地解释。这些文件夹看起来像这样:

c:\data\2011-0703

c:\data\2011-0626

c:\data\2011-0619

如何获得每个文件枚举器来选择最近的文件夹?这可以通过查看创建日期或比较文件名来实现。

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

谢谢

I've got an interesting challenge with SSIS. Using a for-each file enumerator, I need to pick the subfolder which has been most recently created, and then iterate through each of the files.

Perhaps an example would explain better. The folders look something like this:

c:\data\2011-0703

c:\data\2011-0626

c:\data\2011-0619

How could you get a for each file enumerator to pick the most recent folder? This could either be by looking at the creation date, or comparing the file names.

I'm guessing it would be done with an expression in the enumerator, just can't work out how! Couldn't find anything on the net either.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

两仪 2024-11-25 03:56:33

这是一种可能的选择,您可以借助脚本任务来实现此目的。以下示例展示了如何做到这一点。该示例是在 SSIS 2008 R2 中创建的。

分步过程:

  1. 创建三个文件夹,分别命名为 2011-06192011-06262011-0703< /code> 位于文件夹路径 C:\temp\ 中,如屏幕截图 #1 所示。记下每个文件夹的创建日期值。

  2. 在每个文件夹中放置一些文件,如屏幕截图 #2 - #4 所示。

  3. 在 SSIS 包上,创建四个变量,如屏幕截图 #5 所示。将变量 RootFolder 设置为值 C:\temp\ (在您的情况下,这将是 c:\data)。将变量 FilePattern 设置为值 *.*。变量 RecentFolder 将被分配脚本任务中最近的文件夹路径。为了避免设计时错误,请为变量 RecentFolder 分配有效的文件路径。当文件在最近的文件夹中循环时,变量FilePath将被分配值。

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

  5. 在 SSIS 包上,放置一个 Foreach 循环容器并对其进行配置,如屏幕截图 #6 和 #7 所示。

  6. 将脚本任务放置在 Foreach 循环容器内。将脚本任务中的 Main() 方法替换为脚本任务代码(显示文件名):部分中给出的脚本任务代码。该脚本仅显示最近创建的文件夹中的文件名称。

  7. 配置所有任务后,包应如屏幕截图 #8 所示。

  8. 屏幕截图 #9 - #11 显示该包显示最近创建的文件夹 2011-0703 中的文件名。

希望有帮助。

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

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

脚本任务代码(显示文件名):

C#代码,只能在SSIS 2008及更高版本中使用。

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:

1

屏幕截图 #2:

2

屏幕截图#3:

3

屏幕截图 #4:

4

屏幕截图 #5:

5

屏幕截图 #6:

6

屏幕截图 #7:

7

屏幕截图 #8:

8

屏幕截图 #9:

9

屏幕截图 #10:

10

屏幕截图 #11:

11

Here is one possible option that you can achieve this with the help of Script Task. Following example shows how this can be done. The example was created in SSIS 2008 R2.

Step-by-step process:

  1. Create three folders named 2011-0619, 2011-0626 and 2011-0703 in the folder path C:\temp\ as shown in screenshot #1. Make note of the Date created value of each of the folders.

  2. Place few files in each of the folders as shown in screenshots #2 - #4.

  3. On the SSIS package, create four variables as shown in screenshot #5. Set the variable RootFolder with value C:\temp\ (in your case this will be c:\data). Set the variable FilePattern with value *.*. Variable RecentFolder will be assigned with the recent folder path in the Script Task. To avoid design time errors, assign the variable RecentFolder with a valid file path. Variable FilePath will be assigned with values when the files are looped through in the recent folder.

  4. On the SSIS package, place a Script Task. Replace the Main() method within the Script Task with the script task code give under section Script task code (Get recent folder):. This script gets the list of folders in the root folder and loops through to check the creation datetime to pick the most recently created folder. The recently created folder path is then stored in the variable RecentFolder.

  5. On the SSIS package, place a Foreach Loop container and configure it as shown in screenshots #6 and #7.

  6. Place a Script Task inside the Foreach Loop container. Replace the Main() method within the Script Task with the script task code give under section Script task code (Display file names):. This script simply displays the names of files within the recently created folder.

  7. Once all tasks are configured, the package should look like as shown in screenshot #8.

  8. Screenshots #9 - #11 show that the package displays the file names in the recently created folder 2011-0703.

Hope that helps.

Script task code (Get recent folder):

C# code that can be used only in SSIS 2008 and above.

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

Script task code (Display file names):

C# code that can be used only in SSIS 2008 and above.

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

Screenshot #1:

1

Screenshot #2:

2

Screenshot #3:

3

Screenshot #4:

4

Screenshot #5:

5

Screenshot #6:

6

Screenshot #7:

7

Screenshot #8:

8

Screenshot #9:

9

Screenshot #10:

10

Screenshot #11:

11

無處可尋 2024-11-25 03:56:33

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

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

Iterate through the folders. Save the name of the first one. Compare that saved value to the name of each subsequent folder. If the next folder is more recent, swap that name in and keep going. At the end, your saved value will be the name of the most recent folder (if you're comparing creation dates, you'll need to save both the folder name and the creation date).

You can then use the saved value as an argument to your second iteration loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文