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

我的目录中有数百个文件。许多文本文件的代码列值为空白,我需要迭代所有文本文件并填充它。我可以编写代码以在新行中添加代码值,但无法在代码列下编写它。字符串值为:“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;

        }

    }


慕莱坞森
浏览 134回答 3
3回答

元芳怎么了

编辑 请尝试此假设其空格分隔。它在我的 VS2017 上工作,请在顶部添加 using 语句,如下所示。&nbsp;using System.Text.RegularExpressions&nbsp; &nbsp; public static void AddAStringtoAllTextFiles()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string path = @"C:\Users\ur\Desktop\TestFiles\";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var fileEntries = Directory.GetFiles(path);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int indexPosition2InsertData=1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (var entry in fileEntries)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var lines = File.ReadAllLines(entry);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (var index = 1; index < lines.Length; index++) //starting&nbsp; from first row, leaving the header&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var split= Regex.Split(lines[index].Trim(), @"\s{1,}"); //reading the line with space(s)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(split.Length==5) //edited //checking if the row is not blank&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var list = split.ToList(); //convert to list to insert&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.Insert(indexPosition2InsertData, "STRINGTOENTER"); //inserting at the index 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lines[index] = string.Join("\t", list);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; File.WriteAllLines(entry, lines);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch (Exception e)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw e;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }运行代码后我得到了这个。Id&nbsp; &nbsp; Code&nbsp; &nbsp; File_Number&nbsp; &nbsp;Suffix&nbsp; Check_Number&nbsp; &nbsp; Check_Date047 STRINGTOENTER&nbsp; &nbsp;7699&nbsp; &nbsp; 01&nbsp; 99999&nbsp; &nbsp;11/11/20121&nbsp; &nbsp;-6.15请让我知道这可不可以帮你。

当年话下

假设每个文件都有正确的制表符分隔(考虑到问题质量,这是一个很大的假设)// Get the filesvar fileEntries = Directory.GetFiles(path);// iterate through each file nameforeach (var entry in fileEntries){&nbsp; // Load the File into the lines array&nbsp; var lines = File.ReadAllLines(entry);&nbsp; // Iterate over each line&nbsp; if(lines.Length >1)&nbsp; {&nbsp; &nbsp; &nbsp;// Split the lines by tab&nbsp; &nbsp; &nbsp;var split = lines[1].Split('\t');&nbsp; &nbsp; &nbsp;// your code should be at array index 1&nbsp; &nbsp; &nbsp;split[1] = "STRINGTOENTER";&nbsp; &nbsp; &nbsp;// write the whole line back&nbsp; &nbsp; &nbsp;lines[1] = string.Join("\t", split);&nbsp; &nbsp; &nbsp;// write the file&nbsp; &nbsp; &nbsp;File.WriteAllLines(entry, lines);&nbsp; }}注意:您可能应该使用 CSV 解析器来执行此操作,这仅用于学术目的并且完全未经测试

泛舟湖上清波郎朗

我想根据您的输入展示我想要的解决方案。令人惊奇的是,一段简单的代码可以帮助解决更大、更复杂的问题。再次感谢!&nbsp; &nbsp; &nbsp;public static void AddClientCodetoAllTextFiles(string update_batch_with_clientcode, string batchfilepathtobeupdated)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var fileEntries = Directory.GetFiles(@batchfilepathtobeupdated.Trim());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (var entry in fileEntries)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var lines = File.ReadAllLines(entry);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (lines.Length > 1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 1; i < lines.Length - 1; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var split = lines[i].Split('\t');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; split[1] = update_batch_with_clientcode.Trim();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lines[i] = string.Join("\t", split);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; File.WriteAllLines(entry, lines);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch (Exception e)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw e;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP