使用 SSIS 和脚本组件移动文件 [英] Moving files using SSIS and Script Component

查看:43
本文介绍了使用 SSIS 和脚本组件移动文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 SSIS 的脚本组件中编写一个代码,将文件移动到相应的文件夹.我得到的文件通常命名为Dem323_04265.45.23.4"、Dem65_459.452.56"、Ec2345_456.156.7894",我需要将它们移动到相应的文件夹中.我的文件夹的名称是Oklahoma City (323)"、New York(65)"..我需要将这些文件移动到匹配的文件夹中,例如Dem323_04265.45.23.4"将转到文件夹Oklahoma City"(323)".我需要修改我的代码,以便位于前两个或三个字母和下划线之间的数字与位于括号中的数字相匹配.我已经为此工作了好几天,我是 ssis 和 c# 的新手,所以任何帮助将不胜感激.这是我到目前为止的代码:

I need to write a code in script component in SSIS that will move files to corresponding folders. Files that I'm getting are usually named, for example "Dem323_04265.45.23.4", "Dem65_459.452.56", "Ec2345_456.156.7894" and I need to move them to a corresponding folders. The name of my folders are "Oklahoma City (323)", "New York(65)".. I need to move those files to matching folders, so for example "Dem323_04265.45.23.4" would go to folder "Oklahoma City (323)". I need to modify my code so the number that is located between first two or three letter and underline matches number located in parenthesis. I've been working on this for several days already and I'm new with ssis and c#, so any help would be appreciated. This is the code I have so far:

     public void Main()
    {
        string filename;
       // string datepart;
        bool FolderExistFlg;
        filename = Dts.Variables["User::FileName"].Value.ToString();
        // datepart = (filename.Substring(filename.Length - 12)).Substring(0, 8);
        var folderNumber = Regex.Match(
            filename,

    //Dts.Variables["OutputMainFolder"].Value.ToString(),
                    @"\(([^)]*)\)").Groups[1].Value;
        FolderExistFlg = Directory.Exists(Dts.Variables["OutputMainFolder"].Value.ToString() + "\\" + folderNumber);

        if (!FolderExistFlg)
        {
            Directory.CreateDirectory(Dts.Variables["OutputMainFolder"].Value.ToString() + "\\" + folderNumber);
        }

        File.Move(Dts.Variables["SourceFolder"].Value.ToString() + "\\" + filename + "\\" + folderNumber,
        Dts.Variables["OutputMainFolder"].Value.ToString()  + "\\" + filename);
        Dts.TaskResult = (int)ScriptResults.Success;
    }

    #region ScriptResults declaration

    enum ScriptResults
    {
        Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
        Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
    };
    #endregion

}

}

推荐答案

好了,下面的代码片段将根据匹配条件移动文件.您需要根据您的配置处理源输入.

Here you go, the below snippet would move the files as per the match criteria. You need to take care of the source input as per your configuration.

    string filePath = @"C:\Packages\StackOverflow";
    //string fileName = string.Empty;
    //get list of files
    string[] filePaths = Directory.GetFiles(filePath);

    //get list of folders
    string[] dirPaths = Directory.GetDirectories(filePath);

    //loop through the files and move them
    foreach(string fileNames in filePaths)
    {
        string[] pathArr = fileNames.Split('\\');
        string fileName = pathArr.Last().ToString();
        int index = fileName.IndexOf('_');
        string fileNamePart = fileName.Substring(0, index);
        //get the first numeric part of the filename to perform the match
        var fileNameNumPart = Regex.Replace(fileNamePart, "[^0-9]", "");
        //find related directory
        var dirMatch = dirPaths.FirstOrDefault(stringToCheck => stringToCheck.Contains(fileNameNumPart.ToString()));
        if (dirMatch != null)
        {
            // move would fail if file already exists in destination
            if (!File.Exists(dirMatch + '\\' + fileName))
            {
                File.Move(fileNames, dirMatch + '\\' + fileName);
            }                        
        }
    }

这篇关于使用 SSIS 和脚本组件移动文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆