Last active
April 15, 2018 01:06
-
-
Save mjs3339/e4bbeba2a9cdd3bc2ed00fdd3c2f0cfa to your computer and use it in GitHub Desktop.
C# Rename File List - Creates a New List of Files with Duplicates Renamed
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
public static class RenameFileList | |
{ | |
/// <summary> | |
/// Creates a new list of files with duplicates renamed. | |
/// </summary> | |
public static IEnumerable<SourceTarget> ProcessDuplicateNames(this IEnumerable<string> lst, string targetDirectory) | |
{ | |
var NewFileList = new CHashSet<SourceTarget>(); | |
var tfl = new CHashSet<string>(); | |
foreach (var s in lst) | |
{ | |
var counter = 0; | |
var directory = Path.GetDirectoryName(s); | |
if (directory == null) | |
continue; | |
var extension = Path.GetExtension(s); | |
var filename = Path.GetFileNameWithoutExtension(s); | |
var possibleNewName = $"{filename}{extension}"; | |
var st = new SourceTarget(); | |
st.Source = s; | |
st.fileData = new FileData(s); | |
if (!tfl.Contains(possibleNewName)) | |
{ | |
tfl.Add(possibleNewName); | |
st.Target = Path.Combine(targetDirectory, $"{filename}{extension}"); | |
NewFileList.Add(st); | |
} | |
else | |
{ | |
var newtargetName = ""; | |
do | |
{ | |
counter++; | |
possibleNewName = $"{filename}_Copy({counter}){extension}"; | |
newtargetName = Path.Combine(targetDirectory, $"{filename}_Copy({counter}){extension}"); | |
} while (tfl.Contains(possibleNewName)); | |
tfl.Add(possibleNewName); | |
st.Target = newtargetName; | |
NewFileList.Add(st); | |
} | |
} | |
return NewFileList; | |
} | |
public static IEnumerable<SourceTarget> ProcessDuplicateNames(this IEnumerable<FileData> lst, string targetDirectory) | |
{ | |
var NewFileList = new CHashSet<SourceTarget>(); | |
var tfl = new CHashSet<string>(); | |
foreach (var s in lst) | |
{ | |
var counter = 0; | |
var directory = Path.GetDirectoryName(s.FullName); | |
if (directory == null) | |
continue; | |
var extension = Path.GetExtension(s.FullName); | |
var filename = Path.GetFileNameWithoutExtension(s.FullName); | |
var possibleNewName = $"{filename}{extension}"; | |
var st = new SourceTarget(); | |
st.Source = s.FullName; | |
st.fileData = s; | |
if (!tfl.Contains(possibleNewName)) | |
{ | |
tfl.Add(possibleNewName); | |
st.Target = Path.Combine(targetDirectory, $"{filename}{extension}"); | |
NewFileList.Add(st); | |
} | |
else | |
{ | |
var newtargetName = ""; | |
do | |
{ | |
counter++; | |
possibleNewName = $"{filename}_Copy({counter}){extension}"; | |
newtargetName = Path.Combine(targetDirectory, $"{filename}_Copy({counter}){extension}"); | |
} while (tfl.Contains(possibleNewName)); | |
tfl.Add(possibleNewName); | |
st.Target = newtargetName; | |
NewFileList.Add(st); | |
} | |
} | |
return NewFileList; | |
} | |
public static IEnumerable<SourceTarget> ProcessDuplicateNames(this IEnumerable<FileInfo> lst, string targetDirectory) | |
{ | |
var NewFileList = new CHashSet<SourceTarget>(); | |
var tfl = new CHashSet<string>(); | |
foreach (var s in lst) | |
{ | |
var counter = 0; | |
var directory = Path.GetDirectoryName(s.FullName); | |
if (directory == null) | |
continue; | |
var extension = Path.GetExtension(s.FullName); | |
var filename = Path.GetFileNameWithoutExtension(s.FullName); | |
var possibleNewName = $"{filename}{extension}"; | |
var st = new SourceTarget(); | |
st.Source = s.FullName; | |
st.fileInfo = s; | |
if (!tfl.Contains(possibleNewName)) | |
{ | |
tfl.Add(possibleNewName); | |
st.Target = Path.Combine(targetDirectory, $"{filename}{extension}"); | |
NewFileList.Add(st); | |
} | |
else | |
{ | |
var newtargetName = ""; | |
do | |
{ | |
counter++; | |
possibleNewName = $"{filename}_Copy({counter}){extension}"; | |
newtargetName = Path.Combine(targetDirectory, $"{filename}_Copy({counter}){extension}"); | |
} while (tfl.Contains(possibleNewName)); | |
tfl.Add(possibleNewName); | |
st.Target = newtargetName; | |
NewFileList.Add(st); | |
} | |
} | |
return NewFileList; | |
} | |
public class SourceTarget | |
{ | |
public FileData fileData; | |
public FileInfo fileInfo; | |
public string Source = ""; | |
public string Target = ""; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment