HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Program to search many files and remove a certain string

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
searchprogramremovefilesmanyandcertainstring

Problem

The reasons I create this program is because we had an issue of XML elements showing up with older assembly versions in, which were duplicate elements of the same element with the new assembly version in. Here is an example of the problematic files we have:


    Version="dot_net_4_assembly_version_number", 

    Version="dot_net_2_assembly_version_number", "identical_data_here"

    Version="dot_net_4_assembly_version_number", "identical_data_here"


So my program will be given a directory, and loop through all files in all sub-directories of that directory, and then if it matches with the regex to find the references to the older assemblies, it will remove that xml element.

Here is the code:

```
class Program
{
static int totalChangedFilesCount;
static int totalRemovedDotNetTwoReferences;

static void Main(string[] args)
{
var allFilePaths = new List();
allFilePaths = GetAllFilePaths("C:/Temp/SolutionToTest");

foreach(var filePath in allFilePaths)
{
RemoveOldAssemblyReferencesFromFile(filePath);
}

Console.WriteLine($"Total files changed: {totalChangedFilesCount}");
Console.WriteLine($"Total .NET 2 refernces removed: {totalRemovedDotNetTwoReferences}");

Console.ReadLine();

}

private static List GetAllFilePaths(string sourceDirectory, string filePattern = ".")
{
var filePaths = new List();

try
{
foreach (string file in Directory.EnumerateFiles(sourceDirectory, filePattern, SearchOption.AllDirectories))
{
filePaths.Add(file);
}
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}

return filePaths;

}

static void RemoveOldAssemblyReferencesFromFile(string filePath)
{
string xmlToClean = string.Empty;

try
{
if (string.IsNullOrEmpty(filePath))
{

Solution

I cannot look at it. You treat a XML like it was a normal text file. This is insane.

You should parse it with the XDocument.Load, find all the data and value elements with LINQ or XPath and then check the value if it matches the pattern and if necessary call XElement.Remove and resave the file.

Btw. this regex doesn't work



The . in the version number need to be escaped as well as the /.

Using the . is probably also killing the performance: see Why Using the Greedy . in Regular Expressions Is Almost Never What You Actually Want

Code Snippets

<data.*\s*.*Version=1.0.0.5.*\s*</data>

Context

StackExchange Code Review Q#152147, answer score: 6

Revisions (0)

No revisions yet.