Skip to content

Instantly share code, notes, and snippets.

@mjs3339
Last active April 15, 2018 01:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjs3339/e4bbeba2a9cdd3bc2ed00fdd3c2f0cfa to your computer and use it in GitHub Desktop.
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
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