Created
May 4, 2022 12:21
-
-
Save bradmartin333/2bcbfdc20b4f177d7a9d7d76f363a3fd to your computer and use it in GitHub Desktop.
Not regex, but a partwise approach to renaming problematic picture filepaths
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
internal class Program | |
{ | |
private static readonly string dirPath = @"C:\Users\delta\Desktop\560_25p"; | |
static void Main(string[] args) | |
{ | |
string[] paths = GetSortedPicturesFrom(dirPath).ToArray(); | |
foreach (string path in paths) | |
{ | |
try | |
{ | |
string[] parts = path.Split(','); | |
string badPart = parts.Where(x => x.Contains("RC ") && x.Contains("R ")).First(); | |
int badIdx = parts.ToList().IndexOf(badPart); | |
List<string> fixingParts = badPart.Split(' ').ToList(); | |
for (int i = 0; i < fixingParts.Count; i++) | |
{ | |
if (int.TryParse(fixingParts[i], out int validNum)) | |
{ | |
fixingParts[i] += ","; | |
break; | |
} | |
} | |
parts[badIdx] = string.Join(" ", fixingParts); | |
string fixedStr = string.Join(",", parts); | |
File.Move(path, fixedStr); | |
} | |
catch (Exception) | |
{ | |
Console.WriteLine($"Fail on {path}"); | |
} | |
} | |
Console.WriteLine($"Done"); | |
Console.ReadKey(); | |
} | |
private static IEnumerable<string> GetSortedPicturesFrom(string searchFolder) | |
{ | |
string[] filters = new string[] { "jpg", "jpeg", "png", "gif", "tiff", "bmp", "svg" }; | |
List<string> filesFound = new List<string>(); | |
foreach (var filter in filters) | |
filesFound.AddRange(Directory.GetFiles(searchFolder, string.Format("*.{0}", filter), SearchOption.AllDirectories)); | |
return filesFound.Where(x => !x.Contains("pattern")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment