【问题标题】:Loop through set of files and check if it is comma delimited in C#循环遍历一组文件并检查它是否在 C# 中以逗号分隔
【发布时间】:2012-08-14 15:03:38
【问题描述】:

我需要遍历一组文件并检查它是否在 C# 中以逗号分隔。我对 C# 非常陌生。请帮助我。

提前致谢。

【问题讨论】:

  • 你试过什么?您如何知道文件是否以逗号分隔?文本中包含一个或多个逗号是否足够?
  • 为什么不使用众多可用的 CSV 库之一,看看文件是否有效?
  • 您已标记了 SSIS,您是否尝试在 SSIS 中使用 C#?您必须使用 C# 还是您需要在 SSIS 中验证文件的根本问题? CSV(N 列)是否有预期的格式,或者任何带有逗号的内容就足够了?尝试更详细地更新您的问题,否则问题将被关闭。

标签: c# ssis


【解决方案1】:

正如有人已经指出的那样,这不是一个简单的解决方案。 如果每个逗号分隔的文件都有一个指定的扩展名(例如:csv),这可能非常容易。如果不是,以下算法应该可以工作:

  1. 检索指定目录中文件的所有名称(路径+名称)。如果需要,只过滤那些可能感兴趣的。提示:看看System.IO.DirectorySystem.IO.FileSystem.IO.DirectoryInfoSystem.IO.FileInfo
  2. 您必须检查每个文件,并检查它是否以逗号分隔。这将是棘手的部分。您可以构建一个正则表达式,它将检查文件的每一行并告诉您它是否是逗号分隔的。

正则表达式一开始有点难学,但过一段时间就会有回报。

【讨论】:

    【解决方案2】:

    这是一个快速的控制台应用程序,它将获取一个目录,扫描目录中的所有文件,然后遍历它们并返回包含逗号的行的百分比 -vs- 文件中的总行数。正如已经指出的那样,您可以验证一些 CSV 库。这只是一个快速入门的示例。

    要使用它,请在 Visual Studio 中创建一个新的控制台应用项目并将其命名为“TestStub”,然后将其复制并粘贴到“Program.cs”文件中。

    namespace TestStub
    {
        using System;
        using System.IO;
        using System.Text;
    
        public class Program
        {
            private static char[] CSV = { ',', ',' };
            private static bool csvFound = false;
            /// <summary>
            /// This is the console program entry point
            /// </summary>
            /// <param name="args">A list of any command-line args passed to this application when started</param>
            public static void Main(string[] args)
            {
                // Change this to use args[0] if you like 
                string myInitialPath = @"C:\Temp";
                string[] myListOfFiles;
    
                try
                {
                    myListOfFiles = EnumerateFiles(myInitialPath);
    
                    foreach (string file in myListOfFiles)
                    {
                        Console.WriteLine("\nFile {0} is comprised of {1}% CSV delimited lines.",
                            file, 
                            ScanForCSV(file));
                    }
    
                    Console.WriteLine("\n\nPress any key to exit.");
                    Console.ReadKey();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(
                        "Error processing {0} for CSV content: {1} :: {2}", 
                        myInitialPath, 
                        ex.Message, 
                        ex.InnerException.Message);
                }
            }
    
            /// <summary>
            /// Get a list of all files for the specified path
            /// </summary>
            /// <param name="path">Directory path</param>
            /// <returns>String array of files (with full path)</returns>
            public static string[] EnumerateFiles(string path)
            {
                string[] arrItems = new string[1];
    
                try
                {
                    arrItems = Directory.GetFiles(path);
                    return arrItems;
                }
                catch (Exception ex)
                {
                    throw new System.IO.IOException("EnumerateFilesAndFolders() encountered an error:", ex);
                }
            }
            /// <summary>
            /// Determines if the supplied file has comma separated values
            /// </summary>
            /// <param name="filename">Path and filename</param>
            /// <returns>Percentage of lines containing CSV elements -vs- those without</returns>
            public static float ScanForCSV(string filename)
            {
                //
                // NOTE: You should look into one of the many CSV libraries
                // available. This method will not carefully scruitinize
                // the file to see if there's a combination of delimeters or
                // even if it's a plain-text (e.g. a newspaper article)
                // It just looks for the presence of commas on multiple lines
                // and calculates a percentage of them with and without
                //
                float totalLines = 0;
                float linesCSV = 0;
    
                try
                {
                    using (StreamReader sReader = new StreamReader(filename))
                    {
                        int elements = 0;
                        string line = string.Empty;
                        string[] parsed = new string[1];
    
                        while (!sReader.EndOfStream)
                        {
                            ++totalLines;
                            line = sReader.ReadLine();
                            parsed = line.Split(CSV);
                            elements = parsed.Length;
                            if (elements > 1)
                            {
                                ++linesCSV;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new System.IO.IOException(string.Format("Problem accessing [{0}]: {1}", filename, ex.Message), ex);
                }
                return (float)((linesCSV / totalLines) * 100);
            }
        }  
    }
    

    }

    【讨论】:

      猜你喜欢
      • 2022-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-23
      • 2021-02-20
      • 2012-05-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多