Last active
May 25, 2017 22:50
-
-
Save guardrex/a7e27e558e943094347573d0c47da699 to your computer and use it in GitHub Desktop.
Remove uids from metadata docs (Script 2)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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