I am currently developing an application that requires the renaming of music files from a variety of variations such as
Artist - Title feat featuring artist, etc
Or
Artist - Title ft featuring artist, etc
Or
Artist - Title (feat. Featuring artist, etc
To
Artist - Title (ft. featuring artist, etc)
I think you get the idea.
My current hacky way to do this:
private void FindAndReplace()
{
try
{
var replaceList = new Dictionary<string, string>
{
{"[", "("},
{"]", ")"},
{"(Official Audio)", ""},
{"(Audio)", ""},
{"OFFICIAL", ""},
{"Official", ""},
{"(Video)", ""},
{"Video)", ""},
{"(video)", ""},
{"video)", ""},
{"(Lyric","" },
{ " Featuring ", " (ft. "},
{",)", ")"},
{" FEAT ", " (FEAT "},
{" Feat ", " (FEAT "},
{" Feat. ", " (FEAT "},
{"(Feat.", "(ft."},
{"(FEAT", "(feat"},
{"( Music",""},
{"(feat", "(ft"},
{"FEAT ", "ft"},
{"( )", ""},
{"()", ""},
{"(|", ""},
{"( |", ""},
{"( )", ""},
{"FT ", "ft. "},
{"Ft ", "ft. "},
{"(Explicit)", ""},
{"ft ", "ft. "},
{" Ft. ", " (ft. "},//[ FT. , (ft. ]
{" FT. ", " (ft. "},//[ FT. , (ft. ]
{" FT", " (ft"},//[ FT, (ft]
{"(FT ", "(ft. "},
{" (ft ", " (ft."},
{" (Ft ", "(ft. "}
};
while (true)
{
var reiterate = false;
foreach (var vari in replaceList)
{
if (FileName.ToLower().Contains(vari.Key.ToLower()))
{
reiterate = true;
}
}
if (reiterate)
foreach (var replaceItem in replaceList.Where(replaceItem => FileName.ToLower().Contains(replaceItem.Key.ToLower()))
)
{
if (FileName.Contains(replaceItem.Key))
FileName = FileName.Replace(replaceItem.Key, replaceItem.Value);
else
FileName = FileName.Replace(replaceItem.Key.ToLower(), replaceItem.Value);
}
if (reiterate) continue;
break;
}
}
catch (Exception ex)
{
}
}
Note that there are a multitude of things that could need fixing in the filename. I occasionally run into errors with this method and they all stem from the ordering of the replace dictionary. Is there some more efficient and cleaner way to achieve my goal?