Don_B
Don_B

Reputation: 243

Counting strings in files and putting the value in different places using c#?

I'm trying to count some strings in files in a given location and then put those values in some nodes. I've tried the below code

var workingPath=@"D:\Test\MyFiles";
            var files = new List<string>();
            if (Directory.Exists(workingPath))
            {
                foreach (var f in Directory.GetDirectories(workingPath, "xml",
                                                           SearchOption.AllDirectories))
                {
                    files.AddRange(Directory.GetFiles(f, "*.xml"));
                }
            }

            foreach (var file in files) {
                string text = File.ReadAllText(file);
                int fig_count = Regex.Matches(text, @"fig id=""fig").Count;
                int tab_count = Regex.Matches(text, @"table-wrap id=""table").Count;
                int eq_count = Regex.Matches(text, @"disp-formula id=""deqn").Count;
                File.WriteAllText(file,Regex.Replace(File.ReadAllText(file), @"<fig-count count=""\d+""/>",@"<fig-count count="""+fig_count+@"""/>"));
                File.WriteAllText(file,Regex.Replace(File.ReadAllText(file), @"<table-count count=""\d+""/>",@"<table-count count="""+tab_count+@"""/>"));
                File.WriteAllText(file,Regex.Replace(File.ReadAllText(file), @"<eq-count count=""\d+""/>",@"<eq-count count="""+eq_count+@"""/>"));
            }

The code works but it is a bit redundant. Can anyone tell me how to make it less redundant?

Upvotes: 0

Views: 70

Answers (2)

wp78de
wp78de

Reputation: 18950

I suggest extracting a TextUpdate method and read & write the file only once:

foreach (var file in files)
{
    string text = File.ReadAllText(file);
    text = UpdateText(text, "fig", Regex.Matches(text, @"fig id=""fig").Count);
    text = UpdateText(text, "table", Regex.Matches(text, @"table-wrap id=""table").Count);
    text = UpdateText(text, "eq", Regex.Matches(text, @"disp-formula id=""deqn").Count);
    File.WriteAllText(file, text);
}

private static string UpdateText(string text, string type, int count)
{
    return Regex.Replace(text, "<" + type + @"-count count=""\d+""/>", "<" + type + @"-count count=""" + count + @"""/>");
}

Upvotes: 3

jdweng
jdweng

Reputation: 34421

The code below only reads and writes file once :

            string text = File.ReadAllText(file);
            int fig_count = Regex.Matches(text, @"fig id=""fig").Count;
            int tab_count = Regex.Matches(text, @"table-wrap id=""table").Count;
            int eq_count = Regex.Matches(text, @"disp-formula id=""deqn").Count;
            text = Regex.Replace(text, @"<fig-count count=""\d+""/>", @"<fig-count count=""" + fig_count + @"""/>");
            text = Regex.Replace(text, @"<table-count count=""\d+""/>", @"<table-count count=""" + tab_count + @"""/>");
            text = Regex.Replace(text, @"<eq-count count=""\d+""/>", @"<eq-count count=""" + eq_count + @"""/>");
            File.WriteAllText(file, text);

Upvotes: 2

Related Questions