Moving files using SSIS and Script Component

2019-03-05 11:14发布

问题:

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

}

}

回答1:

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);
            }                        
        }
    }


标签: c# ssis