在 SSIS 中加载最近 7 天的具有特定名称的文件

发布于 2024-12-19 09:07:30 字数 448 浏览 1 评论 0原文

我有一个文件夹,每天转储 7 或 8 个 .xls 文件。在所有这些文件中,我只需要加载 2 个具有特定名称的文件。ABC_datestamp.xls 和 XYZ_datestamp.xls。

问题是我无法隔离这两个文件名,因为同一文件夹中有很多其他文件。第二个挑战是我的经理希望我立即加载上周的所有文件,这意味着我必须专门加载最近 7 天的文件。

现在,我将这些特定的 7 个文件复制到一个文件夹中,然后使用 for every 循环容器加载它。

无论如何,我可以使用脚本任务来生成文件名,例如 ABC_02-Dec-2011.xls、ABC_01-Dec-2011.xls、ABC_30-Nov-2011.xls .......过去 7 天的文件并将其作为变量传递给数据流任务?

还有更好的建议吗?

我知道很多人都问过类似的问题,但我是 SSIS 新手,无法找到好的答案。

多谢..

I have a folder which gets a daily dump of 7 or 8 .xls files. Among all those files, I just need to load 2 files with a specific name.. ABC_datestamp.xls and XYZ_datestamp.xls.

The problem is I am not able to isolate these 2 file names because there are so many other files in the same folder. The second challenge is that my manager wants me to load all files from last week at once, which means I have to specifically load files from last 7 days only.

Right now I am copying those specific 7 files in to a folder and then loading it using a for each loop container.

Is there anyway, I can use a script task to generate the file names like ABC_02-Dec-2011.xls, ABC_01-Dec-2011.xls, ABC_30-Nov-2011.xls .......last 7 days files and pass it as a variable to a DATA FLOW TASK?

Any better suggestions ?

I know a lot of people have asked similar questions but I am new to SSIS and not able to find good answers.

Thanks a lot..

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

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

发布评论

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

评论(2

日裸衫吸 2024-12-26 09:07:30

我不会尝试构建所有可能的文件名的列表,而是循环遍历文件夹中的所有文件并使用脚本任务来确定要进一步处理的文件。

控制流程如下所示:
foreach 循环容器的屏幕截图

FELC_InputFolderContents 的变量映射设置如下:
foreach 循环容器设置的屏幕截图

SCR_SetProcessFileFlag 的配置:
脚本任务设置的屏幕截图

SCR_SetProcessFileFlag 的脚本如下所示:

    public void Main()
    {
        string targetFileFullPath = (string)Dts.Variables["User::TargetFileFullPath"].Value;
        string targetFileName = Path.GetFileName(targetFileFullPath);
        Dts.Variables["ProcessFileFlag"].Value = IsValidFileName(targetFileName);
        Dts.TaskResult = (int)ScriptResults.Success;
    }

    private bool IsValidFileName(string targetFileName)
    {
        bool result = false;
        if (Path.GetExtension(targetFileName) == "xls")
        {
            string fileNameOnly = Path.GetFileNameWithoutExtension(targetFileName);
            if (fileNameOnly.Substring(0, 4) == "ABC_" || fileNameOnly.Substring(0, 4) == "XYZ_")
            {
                string datePart = fileNameOnly.Substring(4);
                DateTime dateRangeStart = DateTime.Today.AddDays(-7);
                DateTime dateRangeEnd = DateTime.Today;
                DateTime fileDate;
                if (DateTime.TryParse(datePart, out fileDate)) 
                {
                    if (dateRangeStart <= fileDate && fileDate <= dateRangeEnd)
                    {
                        result = true;
                    }
                }
            }
        }
        return result;
    }

以及 SCR_SetProcessFileFlagDTT_ProcessSelectedFile 如此配置:
优先约束设置截图

(显然,IsValidFileName 的具体逻辑取决于您的具体需求。 )

Rather than try and build a list of all possible file names, I'd loop through all the files in the folder and use a Script Task to determine which files to process further.

The control flow would look like this:
screenshot of foreach loop container

with FELC_InputFolderContents's variable mappings set thusly:
screenshot of foreach loop container settings

SCR_SetProcessFileFlag's configuration:
screenshot of script task settings

SCR_SetProcessFileFlag's script like so:

    public void Main()
    {
        string targetFileFullPath = (string)Dts.Variables["User::TargetFileFullPath"].Value;
        string targetFileName = Path.GetFileName(targetFileFullPath);
        Dts.Variables["ProcessFileFlag"].Value = IsValidFileName(targetFileName);
        Dts.TaskResult = (int)ScriptResults.Success;
    }

    private bool IsValidFileName(string targetFileName)
    {
        bool result = false;
        if (Path.GetExtension(targetFileName) == "xls")
        {
            string fileNameOnly = Path.GetFileNameWithoutExtension(targetFileName);
            if (fileNameOnly.Substring(0, 4) == "ABC_" || fileNameOnly.Substring(0, 4) == "XYZ_")
            {
                string datePart = fileNameOnly.Substring(4);
                DateTime dateRangeStart = DateTime.Today.AddDays(-7);
                DateTime dateRangeEnd = DateTime.Today;
                DateTime fileDate;
                if (DateTime.TryParse(datePart, out fileDate)) 
                {
                    if (dateRangeStart <= fileDate && fileDate <= dateRangeEnd)
                    {
                        result = true;
                    }
                }
            }
        }
        return result;
    }

and the precedence constraint between SCR_SetProcessFileFlag and DTT_ProcessSelectedFile configured thusly:
screenshot of precedence constraint settings

(Obviously, the specific logic for IsValidFileName depends on your particular requirements.)

无人问我粥可暖 2024-12-26 09:07:30

以下链接中的示例说明了如何循环遍历文件夹中的文件以及与给定月份匹配的存档文件。在示例中,文件被命名为 myfile 20-08-2011myfile 13-09-2011,根据月份值,这些文件随后会移动到存档文件夹,月份名称如 AugustSeptember 等。

该示例仅存档文件。如果你想加载数据,那么你需要放置一个数据流任务。在 Foreach 循环容器内。

如果您仔细阅读该示例,您将获得解决问题的想法。

如何创建一个将所有文件从给定文件夹复制到新文件夹的包?

下面是一个示例,演示如何循环遍历文件夹并将 Excel 文件数据加载到数据库中。

如何导入不同名称的 Excel 文件并将相同的模式写入数据库?

Example in the following link explains how to loop through files in a folder and archive files that match a given month. In the example, the files are named as myfile 20-08-2011, myfile 13-09-2011, based on the month value, those files are then moved to an archive folder with month names like August and September etc.

The example simply archives the files. If you want to load data, then you need to place a Data Flow Task. within the Foreach loop container.

If you go through the example, you will get an idea to resolve your problem.

How do I create a package that would copy all files from a given folder into a new folder?

Here is an example that shows how to loop through a folder and load Excel files data into a database.

How to import Excel files with different names and same schema into database?

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