SSIS 在遍历子文件夹的 foreach 循环中检索当前文件夹

发布于 2024-12-10 05:11:57 字数 520 浏览 0 评论 0原文

我使用 SSIS 读取输入中的 .txt 文件,并对其执行业务逻辑,将输出结果保存在与当前输入文件名称相同的文件中(文件名动态存储在变量中)。

当所有文件都存储在同一文件夹中时,访问它们没有问题,因为我对数据流中的平面文件连接字符串使用以下表达式: "path" + @[User::inputFileName] + " .txt"

现在我必须处理带有子文件夹的文件夹(我在 foreach 循环中设置遍历子文件夹),并且平面文件连接字符串存在一些问题,因为我无法使用如下通配符: my path \\子文件夹*" + @[User::inputFileName] + ".txt" 其中每个子文件夹都具有相同的名称,并且仅更改名称的最后一部分

如何将当前子文件夹名称保存在变量中,以便我可以。按以下方式使用它? "path\\"+ @[User::currentSubFolder] +"\\" + @[User::inputFileName] + ".txt"

I use SSIS to read .txt files in input and execute my business logic over them saving the output results in a file whose name is the same as the current inpout file (file name dynamically stored in a variable).

When all the files are stored in the same folder, I have no problem accessing them since I use the following expression for the flat file connection string in the data flow: "path" + @[User::inputFileName] + ".txt"

Now I have to process a folder with subfolders (I set traverse subfolders in the foreach loop) and I have some issues with the flat file connection string since I cannot use a wildcard like: my path\\subfolder*" + @[User::inputFileName] + ".txt" where every subfolder has same name and changes only the last portion of the name.

How can I save the current subfolder name in a variable so that I can use it in the following way? "path\\"+ @[User::currentSubFolder] +"\\" + @[User::inputFileName] + ".txt"

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

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

发布评论

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

评论(1

蓝海 2024-12-17 05:11:57

我能够解决我的问题,因此我在这里写下我的解决方案,以防其他人遇到同样的情况。

我在 foreach 循环之前使用了脚本转换块。我可以从中检索当前的完整路径(随后在平面文件连接字符串中使用)和不带扩展名的输入文件名,以用作包含 SSIS 脚本结果的输出文件名。

为了保留兴趣值,我使用了 2 个变量:一个用于文件名,一个用于路径。

这里是脚本代码:

Public Sub Main()

    'Variable Index 0 => FileName
    'Variable Index 1 => filePath

    Dim fullPath As String = Dts.Variables.Item(1).Value.ToString
    Dim fileName As String = Path.GetFileName(fullPath)
    fileName = fileName.Substring(0, fileName.Length - 4) 

    Dts.Variables.Item(0).Value = fileName
    Dim x As String = Dts.Variables.Item(0).Value.ToString

    Dts.TaskResult = Dts.Results.Success
End Sub

I was able to solve my issue, therefore I write here my solution in the case someone else would be in the same situation.

I used a script transformation block before my foreach loop. From it I can retrieve the current full path (used afterwards in the Flat File connection string) and the input file name without extension to be used as output file name containing the results of the SSIS scripts.

In order to keep the values of interests I used 2 variables: one for the file name and one for the path.

Here the script code:

Public Sub Main()

    'Variable Index 0 => FileName
    'Variable Index 1 => filePath

    Dim fullPath As String = Dts.Variables.Item(1).Value.ToString
    Dim fileName As String = Path.GetFileName(fullPath)
    fileName = fileName.Substring(0, fileName.Length - 4) 

    Dts.Variables.Item(0).Value = fileName
    Dim x As String = Dts.Variables.Item(0).Value.ToString

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