根据csv文件的行数移动文件

时间:2016-08-30 13:03:27

标签: c# sql-server csv ssis

我有一个SSIS包,它有一个.csv文件作为平面文件源。条件拆分根据ID在各种SQL Server登台表中插入数据。然后,任何未成功插入的行都将重定向到错误.csv文件,每个ID一个。

有了这么多错误文件,我想对行数进行评估(除了标题行)。如果至少有一个数据错误行,则这些行将通过发送电子邮件任务发送给源开发人员,否则它们将通过文件系统任务存档。

我正在努力解决这个问题。我知道错误文件目录中的Foreach循环容器可以用于循环并通过脚本任务执行行计数。但是,我不确定如何将单个文件名传递给脚本任务。此外,我不确定之后我可以进行有条件的电子邮件/文件系统任务移动。

在脚本任务方面,我有一些看起来像这样(它的错误)的脚本任务,失败是它不超过1并获得存档,传递它通过电子邮件发送,只是为了一些想法。我对复杂的C#脚本很新。

        public void Main()
        {
        string folderPath = Dts.Variables["User::errorDirectory"].Value.ToString();
        string archivePath = @"\\SERVER\placeholderfilepath\blah\blah";
        var FileNames = new List<string>();
        var FilePaths = new List<string>();

        DirectoryInfo directoryInfo = new DirectoryInfo(folderPath);
        FileInfo[] files = directoryInfo.GetFiles();

        var lineCount = 0;


        foreach (FileInfo fileInfo in files)
        {

            lineCount = File.ReadAllLines(fileInfo.FullName).Count();

            //MessageBox.Show(lineCount.ToString());

            if (lineCount == 1)
            {
                File.Move(folderPath, archivePath);
            }

        }




       Dts.TaskResult = (int)ScriptResults.Success;

       }

经过几个小时的搜索后,我找不到任何有关此内容的内容,所以对任何指针/文章都会感激不尽。

提前致谢。

2 个答案:

答案 0 :(得分:1)

而不是ForEach循环来计算文件,只需处理脚本任务中的所有内容。使用filesystemobject获取文件夹中的文件集合,并使用集合的count属性。

答案 1 :(得分:1)

您可以尝试将行计数组件添加到将数据写入.CSV的数据流中。行计数会将行数放到某个变量中,然后你就会知道.CSV中是否有任何行

我可以在你的代码中看到问题

string folderPath = Dts.Variables["User::errorDirectory"].Value.ToString();
        string archivePath = @"\\SERVER\placeholderfilepath\blah\blah";
        var FileNames = new List<string>();
        var FilePaths = new List<string>();

        DirectoryInfo directoryInfo = new DirectoryInfo(folderPath);
        FileInfo[] files = directoryInfo.GetFiles();

        var lineCount = 0;


        foreach (FileInfo fileInfo in files)
        {

            lineCount = File.ReadAllLines(fileInfo.FullName).Count();

            //MessageBox.Show(lineCount.ToString());

            if (lineCount == 1) 
            {
                File.Move(fileInfo.FullName, Path.Combine(archivePath, fileInfo.Name));
            }

        }

        Dts.TaskResult = (int)ScriptResults.Success;