Created
November 3, 2014 15:30
-
-
Save MiniverCheevy/5c940eedec5a4f2489da to your computer and use it in GitHub Desktop.
Recursive Solution Renamer
This file contains 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.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Text.RegularExpressions; | |
namespace Renamer | |
{ | |
internal class Program | |
{ | |
private const string path = @"E:\test\string-replicator"; | |
private const string outPath = @"E:\test\rapid-query"; | |
private const string from = "StringReplicator"; | |
private const string to = "RapidQuery"; | |
private static List<string> directories = new List<string>(); | |
private static List<string> files = new List<string>(); | |
private static string[] fileExtensions = new string[] { ".cs", ".csproj", ".sln", ".bat", ".tt", ".aspx", ".ascx", ".config", ".htm", ".html" }; | |
private static void Main(string[] args) | |
{ | |
CopyAll(new DirectoryInfo(path), new DirectoryInfo(outPath)); | |
renameDirectories(outPath); | |
renameAndRewriteFiles(outPath); | |
foreach (var s in directories) | |
{ | |
renameAndRewriteFiles(s); | |
} | |
Console.WriteLine("Done. Press any key to exit."); | |
Console.ReadKey(); | |
} | |
private static void renameAndRewriteFiles(string directory) | |
{ | |
foreach (var file in Directory.GetFiles(directory)) | |
{ | |
var old = file; | |
var oldPath = old.Substring(0, old.LastIndexOf(@"\", System.StringComparison.Ordinal)); | |
var oldFile = old.Substring(old.LastIndexOf(@"\", System.StringComparison.Ordinal)); | |
var newFile = replaceEx(oldFile, from, to); | |
var newPath = oldPath + @"\" + newFile; | |
newPath = newPath.Replace(@"\\", @"\"); | |
if (old != newPath) | |
{ | |
if (File.Exists(newPath)) | |
{ | |
File.SetAttributes(newPath, FileAttributes.Normal); | |
File.Delete(newPath); | |
} | |
File.Move(old, newPath); | |
} | |
var extension = Path.GetExtension(newPath).ToLower(); | |
if (fileExtensions.Contains(extension)) | |
{ | |
var contents = File.ReadAllText(newPath); | |
if (contents.ToLower().Contains(from.ToLower())) | |
{ | |
contents = Regex.Replace(contents, from, to, RegexOptions.IgnoreCase); | |
File.SetAttributes(newPath, FileAttributes.Normal); | |
File.Delete(newPath); | |
using (var sw = File.CreateText(newPath)) | |
{ | |
sw.Write(contents); | |
sw.Flush(); | |
sw.Close(); | |
} | |
} | |
} | |
else if (extension == ".vspscc") | |
{ | |
File.SetAttributes(newPath, FileAttributes.Normal); | |
File.Delete(newPath); | |
} | |
} | |
} | |
public static void CopyAll(DirectoryInfo source, DirectoryInfo target) | |
{ | |
if (Directory.Exists(target.FullName) == false) | |
{ | |
Directory.CreateDirectory(target.FullName); | |
} | |
foreach (var file in source.GetFiles()) | |
{ | |
file.CopyTo(Path.Combine(target.ToString(), file.Name), true); | |
} | |
foreach (var diSourceSubDir in source.GetDirectories()) | |
{ | |
var nextTargetSubDir = | |
target.CreateSubdirectory(diSourceSubDir.Name); | |
CopyAll(diSourceSubDir, nextTargetSubDir); | |
} | |
} | |
private static void renameDirectories(string dir) | |
{ | |
foreach (var file in Directory.GetDirectories(dir)) | |
{ | |
var oldFile = file; | |
var oldPath = oldFile.Substring(0, oldFile.LastIndexOf(@"\", System.StringComparison.Ordinal)); | |
var oldDir = oldFile.Substring(oldFile.LastIndexOf(@"\", System.StringComparison.Ordinal)); | |
var newDir = replaceEx(oldDir, from, to); | |
var newPath = oldPath + @"\" + newDir; | |
newPath = newPath.Replace(@"\\", @"\"); | |
if (oldFile != newPath) | |
{ | |
Directory.Move(oldFile, newPath); | |
} | |
if (!directories.Contains(newPath)) | |
{ | |
directories.Add(newPath); | |
renameDirectories(newPath); | |
} | |
} | |
} | |
private static string replaceEx(string original, | |
string pattern, string replacement) | |
{ | |
int position0,position1; | |
var count = position0 = position1 = 0; | |
var upperString = original.ToUpper(); | |
var upperPattern = pattern.ToUpper(); | |
var inc = (original.Length/pattern.Length)* | |
(replacement.Length - pattern.Length); | |
var chars = new char[original.Length + Math.Max(0, inc)]; | |
while ((position1 = upperString.IndexOf(upperPattern, position0, System.StringComparison.Ordinal)) != -1) | |
{ | |
for (var i = position0; i < position1; ++i) | |
chars[count++] = original[i]; | |
for (var i = 0; i < replacement.Length; ++i) | |
chars[count++] = replacement[i]; | |
position0 = position1 + pattern.Length; | |
} | |
if (position0 == 0) return original; | |
for (var i = position0; i < original.Length; ++i) | |
chars[count++] = original[i]; | |
return new string(chars, 0, count); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment