SSIS - 脚本任务在文件名中查找字符串值

发布于 2025-01-12 20:43:19 字数 2090 浏览 3 评论 0原文

我必须在 SSIS 中构建一个作业来检查具有今天日期的文件是否存在,然后将数据加载到目标表中。

我的问题是,我在此文件夹中存储了不同日期的不同文件,格式为 FileName_yyyyMMddhhmmss.csv,并且无法检查我是否存在包含今天日期 (FileName_20220309) 的文件名。

到目前为止我所做的是创建了3个变量

  1. FolderPath
  2. FileName
  3. 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))))

我使用了一个脚本任务组件,在其中传递了变量FileNameFolderPath 作为 ReadOnlyVariablesFileExistsFlg 作为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

  1. FolderPath
  2. FileName
  3. 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 技术交流群。

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

发布评论

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

评论(2

迷荒 2025-01-19 20:43:19

我想你想要这样的东西...

var files = DirectoryInfo(folderPath).GetFiles("*" + DateTime.Now.ToString("yyyyMMdd") + "*");

bool flag = files.Length > 0 ? true : false;

这将识别文件夹中具有 yyyyMMdd 格式的今天日期的所有文件。

I think you want something like this...

var files = DirectoryInfo(folderPath).GetFiles("*" + DateTime.Now.ToString("yyyyMMdd") + "*");

bool flag = files.Length > 0 ? true : false;

This will identify any files in a folder that have today's date in the yyyyMMdd format in them.

说谎友 2025-01-19 20:43:19

使用File.Exists(Filepath)需要文件的完整路径而不是文件名。例如,File.Exists(@"C:\data_2022.csv");。您应该枚举目录文件并检查给定名称的文件是否存在,而不是使用 FileExists() 函数。

考虑使用以下代码:

#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(Directory.GetFiles(Filepath,SearchString,SearchOption.AllDirectories).length > 0)
        {
            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
    };

}
}

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 the FileExists() functions, you should enumerate the directory files and check if a file with the given name exists.

Consider using the following code:

#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(Directory.GetFiles(Filepath,SearchString,SearchOption.AllDirectories).length > 0)
        {
            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
    };

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