2

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?

0

1 Answer 1

1

You could merge rules by using regular expressions.

e.g

replaceAll("(?i)[\\s\\(]*fe?a?t\\.?[\\s]", " (ft. ")

With this, you could replace all/most of your rules around the 'feature' part.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.