Skip to content

Instantly share code, notes, and snippets.

@guardrex

guardrex/Program.cs

Last active May 25, 2017
Embed
What would you like to do?
Remove uids from metadata docs (Script 2)
using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using static System.IO.SearchOption;
namespace RemoveDuplicateUids
{
class Program
{
static void Main()
{
string path = @"C:\Users\XXXXXXXXXX\Documents\GitHub\docs-1\add\metadata\";
var files = Directory.EnumerateFiles(path, "*.md", AllDirectories);
Regex regExp = new Regex(@"---(.*?)---", RegexOptions.Compiled | RegexOptions.Singleline);
var countFiles = 0;
foreach (var file in files)
{
var fileText = File.ReadAllText(file);
var captures = regExp.Matches(fileText);
var uniqueCaptures = captures
.OfType<Match>()
.Select(m => m.Value)
.Distinct()
.ToList();
var capturesCount = captures.Count();
var uniqueCapturesCount = uniqueCaptures.Count();
if (capturesCount > uniqueCapturesCount)
{
countFiles++;
Console.WriteLine($"{file.Substring(path.Length)} Captures: {capturesCount} Unique Captures: {uniqueCapturesCount}");
using (System.IO.StreamWriter outfile = new System.IO.StreamWriter(file))
{
int i;
for (i = 0; i < uniqueCaptures.Count() - 1; i++)
{
outfile.WriteLine(uniqueCaptures[i] + Environment.NewLine);
}
outfile.WriteLine(uniqueCaptures[i]);
}
}
}
Console.WriteLine($"Total files updated: {countFiles}");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.