SSIS - 脚本任务在文件名中查找字符串值
我必须在 SSIS 中构建一个作业来检查具有今天日期的文件是否存在,然后将数据加载到目标表中。
我的问题是,我在此文件夹中存储了不同日期的不同文件,格式为 FileName_yyyyMMddhhmmss.csv
,并且无法检查我是否存在包含今天日期 (FileName_20220309) 的文件名。
到目前为止我所做的是创建了3个变量
- FolderPath
- FileName
- FileExistsFlg
对于变量FileName
,我使用了以下表达式来获取格式FileName_20220309
"Player_info_" + (DT_WSTR,50)(((DT_I8) ((DT_WSTR,4)DATEPART("yyyy",GetDate()) + RIGHT("0" +
(DT_WSTR,2)DATEPART("mm",GetDate()) ,2) +RIGHT("0" + (DT_WSTR,2)DATEPART("dd",GetDate()),2))))
我使用了一个脚本任务组件,在其中传递了变量FileName
和 FolderPath
作为 ReadOnlyVariables
和 FileExistsFlg
作为ReadWriteVariables
下面是我在脚本任务组件中使用的脚本
#region Namespaces
using System;
using System.IO;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
#endregion
namespace ST_3692973debdd4531ac4eced28213e38f
{
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain :
Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
public void Main()
{
// TODO: Add your code here
String Filepath = Dts.Variables["User::FolderPath"].Value.ToString()+Dts.Variables["User::FileName"].Value.ToString();
String SearchString = Dts.Variables["User::FileName"].Value.ToString();
if(
File.Exists(Filepath))
{
Dts.Variables["User::FileExistsFlg"].Value = 1;
}
MessageBox.Show(Filepath);
MessageBox.Show(Dts.Variables["User::FileExistsFlg"].Value.ToString());
Dts.TaskResult = (int)ScriptResults.Success;
}
#region ScriptResults declaration
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
}
}
使用上面的代码,我无法检查具有今天日期的文件 (FileName_20220309) 是否存在。
有人可以帮助我如何修改上面的代码来检查文件名中是否存在该字符串吗?
I have to build a job in SSIS to check if file with today's date exist then load data into targeted table.
My problem is I have different files stored with different dates in this folder with the format FileName_yyyyMMddhhmmss.csv
and am not able to check if I have a filename with today's date (FileName_20220309) exist.
What I have done so far is I have created 3 variables
- FolderPath
- FileName
- FileExistsFlg
For the variable FileName
, I have used the following expression to get the format FileName_20220309
"Player_info_" + (DT_WSTR,50)(((DT_I8) ((DT_WSTR,4)DATEPART("yyyy",GetDate()) + RIGHT("0" +
(DT_WSTR,2)DATEPART("mm",GetDate()) ,2) +RIGHT("0" + (DT_WSTR,2)DATEPART("dd",GetDate()),2))))
I have used a Script Task component where I have passed variables FileName
and FolderPath
as ReadOnlyVariables
and FileExistsFlg
as ReadWriteVariables
Below is my script used in the script task component
#region Namespaces
using System;
using System.IO;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
#endregion
namespace ST_3692973debdd4531ac4eced28213e38f
{
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain :
Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
public void Main()
{
// TODO: Add your code here
String Filepath = Dts.Variables["User::FolderPath"].Value.ToString()+Dts.Variables["User::FileName"].Value.ToString();
String SearchString = Dts.Variables["User::FileName"].Value.ToString();
if(
File.Exists(Filepath))
{
Dts.Variables["User::FileExistsFlg"].Value = 1;
}
MessageBox.Show(Filepath);
MessageBox.Show(Dts.Variables["User::FileExistsFlg"].Value.ToString());
Dts.TaskResult = (int)ScriptResults.Success;
}
#region ScriptResults declaration
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
}
}
With the above code, I'm not able to check if a file with today's date (FileName_20220309) exists.
Can someone help with how I can modify the above code to check if the string exists in the filename?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想你想要这样的东西...
这将识别文件夹中具有 yyyyMMdd 格式的今天日期的所有文件。
I think you want something like this...
This will identify any files in a folder that have today's date in the yyyyMMdd format in them.
使用
File.Exists(Filepath)
需要文件的完整路径而不是文件名。例如,File.Exists(@"C:\data_2022.csv");
。您应该枚举目录文件并检查给定名称的文件是否存在,而不是使用FileExists()
函数。考虑使用以下代码:
Using
File.Exists(Filepath)
need a complete path of the file instead of the file name. For example,File.Exists(@"C:\data_2022.csv");
. Instead of using theFileExists()
functions, you should enumerate the directory files and check if a file with the given name exists.Consider using the following code: