在文本文件的特定位置附加字符串

时间:2019-08-22 02:17:20

标签: c# .net

我在目录中有数百个文件。许多文本文件的“代码列”值为空白,我需要遍历所有文本文件并填充它。我能够编写代码以在新行中添加代码值,但是我无法在代码列下编写代码。字符串值为:“ STRINGTOENTER”。我只希望它在标题后的第一行中输入。最后一行应该留空

 
Id    Code    File_Number   Suffix  Check_Number    Check_Date
047 7699 01 99999 11/11/2012 1 -6.15
以下是我的代码段,这些代码段在换行符处添加了值。我想我需要在这里做一个正则表达式或制表符分隔类型的解决方案。

 public static void AddAStringtoAllTextFiles()
    {
        try
        {
            string path = @"C:\Users\ur\Desktop\TestFiles\";
            string[] fileEntries = Directory.GetFiles(path);
            for (int i = 0; i < fileEntries.Length; i++)
            {

                File.AppendAllText(fileEntries[i], "STRINGTOENTER" + Environment.NewLine);
           }
        }

        catch (Exception e)
        {
            throw e;
        }
    }

3 个答案:

答案 0 :(得分:1)

已编辑 请在假定其空间已划定的前提下尝试此操作。

它在我的VS2017上运行,并在顶部添加using语句,如下所示。

 using System.Text.RegularExpressions

    public static void AddAStringtoAllTextFiles()
    {
        try
        {
            string path = @"C:\Users\ur\Desktop\TestFiles\";
            var fileEntries = Directory.GetFiles(path);
            int indexPosition2InsertData=1;
            foreach (var entry in fileEntries)
            {
                var lines = File.ReadAllLines(entry);
                for (var index = 1; index < lines.Length; index++) //starting  from first row, leaving the header
                {
                    var split= Regex.Split(lines[index].Trim(), @"\s{1,}"); //reading the line with space(s)
                    if(split.Length==5) //edited //checking if the row is not blank
                    {
                        var list = split.ToList(); //convert to list to insert
                        list.Insert(indexPosition2InsertData, "STRINGTOENTER"); //inserting at the index 1
                        lines[index] = string.Join("\t", list);
                    }
                }
                File.WriteAllLines(entry, lines);
            }
        }

        catch (Exception e)
        {
            throw e;
        }
    }

运行代码后,我得到了这个。

Id    Code    File_Number   Suffix  Check_Number    Check_Date

047 STRINGTOENTER   7699    01  99999   11/11/2012
1   -6.15

请告诉我这是否有帮助。

答案 1 :(得分:0)

假设每个文件都有正确的制表符分隔符(考虑到问题的质量,这是一个很大的假设)

// Get the files
var fileEntries = Directory.GetFiles(path);

// iterate through each file name
foreach (var entry in fileEntries)
{

  // Load the File into the lines array
  var lines = File.ReadAllLines(entry);

  // Iterate over each line
  if(lines.Length >1)
  {
     // Split the lines by tab
     var split = lines[1].Split('\t');
     // your code should be at array index 1
     split[1] = "STRINGTOENTER";
     // write the whole line back
     lines[1] = string.Join("\t", split);

     // write the file
     File.WriteAllLines(entry, lines);
  }

}

注意 :您可能应该使用CSV解析器进行此操作,这仅出于学术目的,未经测试

答案 2 :(得分:0)

我想根据您的输入显示我想要的解决方案。一段简单的代码如何有助于解决更大,更复杂的问题,真是令人惊讶。再次感谢!

     public static void AddClientCodetoAllTextFiles(string update_batch_with_clientcode, string batchfilepathtobeupdated)
    {      

        try
        {
            var fileEntries = Directory.GetFiles(@batchfilepathtobeupdated.Trim());

            foreach (var entry in fileEntries)
            {
                var lines = File.ReadAllLines(entry);

                if (lines.Length > 1)
                {
                    for (int i = 1; i < lines.Length - 1; i++)
                    {
                        var split = lines[i].Split('\t');
                        split[1] = update_batch_with_clientcode.Trim();
                        lines[i] = string.Join("\t", split);

                        File.WriteAllLines(entry, lines);
                    }

                }

            }

        }

        catch (Exception e)
        {
            throw e;
        }
    }
相关问题