SSIS:从文件夹中获取任何平面文件源并将名称缓存为超级全局变量
我正在 SSIS 和 Visual Studio 2008 中工作。执行时,我需要让 SSIS 包执行以下任务:
- 检查文件夹中的文件
- 的源
- 如果文件存在,则获取该文件并将其用作平面文件存储 文件名放入全局变量中,我可以在包的其他部分访问
该变量该包将由其他脚本运行。因此,我们需要它在每次包运行时检查文件。我们试图防止出现这样的情况:当文件出现时,我们必须监视文件夹并手动执行包。
有什么建议吗?
I'm working in SSIS and Visual Studio 2008. When executed, I need to have the SSIS package perform the following tasks:
- Check a folder for a file
- If a file exists take the file and use it as the source for the flat file
- Store the name of the file into a global variable that I can access in other parts of my package
The package will be run by some other script. Thus we need it to check for the file every time the package runs. We are trying to prevent the scenario where we have to monitor the folder and execute the package manually when the file appears.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最简单的方法是设置一个 Foreach 循环容器,其中包含包的所有“工作”(或者,您可以将其作为前导步骤并使用其中的条件表达式)。假设您有 2 个名为 FileName 的变量(您将为其分配值)和一个包含我们应该查找的“位置”的 InputFolder 变量
您也可以通过脚本任务来执行此操作,如果您想查看,请告诉我。
编辑
该脚本再次假设您已定义变量 InputFolder 和 FileName。创建脚本任务组件并将 InputFolder 选中为只读变量,将 FileName 选中为读/写变量。
The easiest way would be to set up a Foreach Loop container that has all the "work" of your package inside of it (optionally, you can it as a precursor step and use a conditional expression off of it). Assuming you have 2 variables called FileName (which is what you will have the value assigned to) and an InputFolder variable that contains the "where" we should be looking
You can also do this via a script task, if you'd like to see that, let me know.
EDIT
This script again assumes you have the variables InputFolder and FileName defined. Create a Script Task Component and check InputFolder as a read only variable, FileName as a read/write variable.
这是一个可能的选择。您可以使用
Foreach Loop
容器来实现此目的。请查找我在下面提供的示例。希望这能提供一个想法。分步过程:
在 SSIS 包上,创建 3 个变量,如屏幕截图 #1 所示。范围
CheckFile
表示包名称。变量Folder
将代表您要检查文件的文件夹。Filename
表示要检查的文件名。变量FilePath
将是您需要的全局变量。如果文件存在,则填写文件路径值,否则为空。在包的控制流选项卡上,放置一个
Foreach 循环容器
和一个脚本任务
。脚本任务是为了展示 Foreach 循环容器执行完成后变量保留值。请参阅屏幕截图 #2。配置 ForEach 循环容器,如屏幕截图 #3 和 #4 所示。
将 S
cript Task
中的 Main() 方法替换为Script task code
部分中给出的代码。这是为了演示变量FilePath
保留的值。屏幕截图#5显示路径
c:\temp\
中不存在文件,屏幕截图#6显示相应的包执行。< /p>屏幕截图 #7 显示文件
TestFile.txt
存在于路径c:\temp\
中,屏幕截图 #8< /strong> 显示相应的包执行。如果您想在文件存在时对其进行处理,可以在
Foreach 循环容器
中放置一个数据流任务
来执行此操作。希望有帮助。
脚本任务代码:
C#代码,只能在
SSIS 2008及以上版本
中使用。截图#1:
屏幕截图 #2:
屏幕截图 #3:
屏幕截图 #4:
屏幕截图 # 5:
屏幕截图 #6:
屏幕截图 #7:
屏幕截图 # 8:
Here is a possible option. You can achieve this using the
Foreach Loop
container. Please find the example that I have provided below. Hopefully, that gives an idea.Step-by-step process:
On the SSIS package, create 3 variables are shown in screenshot #1. Scope
CheckFile
represents the package name. VariableFolder
will represent the folder that you would like to check for the file.Filename
represents the file name to check for. VariableFilePath
will be the global variable that you will need. It will be filled in with the file path value if the file exists, otherwise it will be empty.On the package's Control Flow tab, place a
Foreach Loop container
and aScript Task
. Script Task is to showcase that the variable retains the value after the Foreach Loop container execution is complete. Refer screenshot #2.Configure ForEach Loop container as shown in screenshots #3 and #4.
Replace the Main() method within the S
cript Task
with the code given under theScript task code
section. This is to demonstrate the value retained by the variableFilePath
.Screenshots #5 shows no files exist in the path
c:\temp\
and screenshot #6 shows the corresponding package execution.Screenshots #7 shows the file
TestFile.txt
exists in the pathc:\temp\
and screenshot #8 shows the corresponding package execution.If you would like to process the file when it exists, you can place a
Data Flow Task
within theForeach Loop container
to do that.Hope that helps.
Script task code:
C# code that can be used only in
SSIS 2008 and above
..Screenshot #1:
Screenshot #2:
Screenshot #3:
Screenshot #4:
Screenshot #5:
Screenshot #6:
Screenshot #7:
Screenshot #8: