Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
C# Rename File List - Creates a New List of Files with Duplicates Renamed
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