Home > Mobile >  C# reading and rewriting a line in multiple files
C# reading and rewriting a line in multiple files

Time:08-13

I have 650 files that I need to remove a specific words. At the moment it's reading each line in each file, but it's writing the it as a new line rather than replacing the line. Is it possible to make the replace the line rather than write it at the bottom?

        string folderPath = "other";

        foreach (string file in Directory.EnumerateFiles(folderPath, "*.plugin.kts"))
        {

            List<string> lines = new List<string>();
            lines = File.ReadAllLines(file).ToList();
            
            using (StreamWriter writer = new StreamWriter(file, true))
            {
                foreach (string LINE in lines)
                {
                    Console.WriteLine(LINE);
                    if (LINE.Contains("package gg.rsmod.plugins.content.npcs.npcInfo.*"))
                    {
                        var r1 = LINE.Replace("package gg.rsmod.plugins.content.npcs.npcInfo.wyrm", "package gg.rsmod.plugins.content.npcs.npcInfo.other");
                        writer.WriteLine(r1);
                        Console.WriteLine(LINE);
                    }
                    if (LINE.Contains("aggroTimer"))
                    {
                        var r2 = LINE.Replace("aggroTimer", "");
                        writer.WriteLine(r2);
                    }
                    if (LINE.Contains("aggroTimer"))
                    {
                        var r3 = LINE.Replace("aggroMinutes", "");
                        writer.WriteLine(r3);
                    }
                }
            }
        }
        Console.ReadLine();

CodePudding user response:

You have several issues.

There is a quire obvious typo in that you look for aggroTimer, but replace it with aggroMinutes.

But more important you're writing the line to the output whenever one replacement has been made; this approach however does not take into account lines without replacement and lines with multiple replacements. Note that Replace() will just do nothing if the string is not found, so it is safe to call without checking the string first.

You should therefore rather do the following:

foreach (string LINE in lines) {
    writer.WriteLine(LINE
            .Replace("package gg.rsmod.plugins.content.npcs.npcInfo.wyrm", "package gg.rsmod.plugins.content.npcs.npcInfo.other")
            .Replace("aggroMinutes", "")
            .Replace("aggroTimer", ""));
}

CodePudding user response:

You can use the the handy static methods of the System.IO.File Class:

const string p1 = "package gg.rsmod.plugins.content.npcs.npcInfo.wyrm";
const string p2 = "package gg.rsmod.plugins.content.npcs.npcInfo.other";

string[] lines = File.ReadAllLines(file);
for (int i = 0; i < lines.Length; i  )
{
    lines[i] = lines[i]
        .Replace(p1, p2)
        .Replace("aggroTimer", "")
        .Replace("aggroMinutes", "");
}
File.WriteAllLines(file, lines);

File.AppendAllLines appends the lines to the file, while File.WriteAllLines creates a new file or replaces an existing one.

Note, there is no need to test whether a string contains a substring to be replaced before you replace it, as the Replace method will automatically return the original string when it is not contained.

To replace a variable pattern, you can use System.Text.RegularExpressions.Regex.

string pattern = @"package gg\.rsmod\.plugins\.content\.npcs\.npcInfo\.\w ";
const string p2 = "package gg.rsmod.plugins.content.npcs.npcInfo.other";

string[] lines = File.ReadAllLines(file);
for (int i = 0; i < lines.Length; i  )
{
    lines[i] = Regex.Replace(lines[i], pattern, p2)
        .Replace("aggroTimer", "")
        .Replace("aggroMinutes", "");
}
File.WriteAllLines(file, lines);

where \. is the escaped period and \w is a word.

  •  Tags:  
  • c#
  • Related