Click here to Skip to main content
15,896,727 members
Articles / Programming Languages / C#

m3u Playlist Copier

Rate me:
Please Sign up or sign in to vote.
3.67/5 (2 votes)
25 May 2009CPOL2 min read 31.6K   747   11   1
Lets you choose the songs from m3u playlist and copy all checked songs to a specified folder
M3U_PlayList_Copier

Introduction

Sometimes we have very good and long playlists, but the problem appears when we want to copy the songs from it to any destination like your mobile or your mp3. It is really hard to catch every song from a different folder or partition.

You can download the full code (*.cs file) and the program from the link above.

Note: The program can deal with non-English file names.

As you see in the picture, the opened playlist contains an Arabic file name.

Background

I think no background is needed as the idea is very simple.

We will open the playlist file (which is a normal text file), then build an array with the locations and build a checklist in the form with songs' names with a copy button.

The Code Explained

First we have a public array. So we can use it in all program controls (to be accessed by open dialog and save dialog).

C#
public string[] Final_Adresses=new string[0]; 

After that, we need to treat the playlist language if it is a non-English language.

C#
//Read all bytes to an array to convert it to unicode
//so non-English characters will be shown properly 
byte[] all_bytes = File.ReadAllBytes(openFileDialog1.FileName);

// Perform the conversion from one encoding to the other.
byte[] new_encoded = Encoding.Convert(Encoding.Default, Encoding.Unicode, all_bytes);

//Convert the new encoded array (of bytes) to a normal string
string uniString = Encoding.Unicode.GetString(new_encoded);

//Split the string to array by lines
string[] final_encoded = uniString.Split('\n'); 

In the code above, we read all playlist files as a byte array, then we convert it to Unicode(utf-8). Then we convert the Unicode byte array to a string and we split the new string to get the original names with Unicode characters so it will NOT be displayed like (??????) or any undefined characters.

Then we will make an array with the address, but the problem with the m3u playlists is that it depends on the song location from the playlist itself.

For example:

If the song is in the same directory, the playlist will contain the song name only.
If the song is in a subdirectory in the same directory of the playlist, it will contain the “directory name\song name“, or else, it will contain the full address.
So we will make an array to contain all addresses in unified format.  

C#
//String Array To Save all addresses as it varies with some conditions in M3U PlayList
//We will use an Array List as it doesn't have an initial length
System.Collections.ArrayList address = new System.Collections.ArrayList();
//Read the M3u File from this array
                    for (int x = 2; x < final_encoded.Length; x = x + 2)
                    {
                        //the song is in another Directory
                        if (final_encoded[x].Contains(":"))
                        {
                            //removing the last character "\r" to get the exact path
                            address.Add(final_encoded[x].Remove
						(final_encoded[x].Length - 1));
                            checkedListBox1.Items.Add(final_encoded[x].Remove(0, 
					final_encoded[x].LastIndexOf('\\') + 1));
                        }
                        //if (final_encoded[x].Contains("\\"))
                        else
                        {
                            string tmpadrs = openFileDialog1.FileName;
                            tmpadrs = tmpadrs.Remove(tmpadrs.LastIndexOf('\\'));
                            tmpadrs = tmpadrs + "\\" + final_encoded[x];
                            address.Add(tmpadrs);
                            checkedListBox1.Items.Add(final_encoded[x].Remove(0, 
					final_encoded[x].LastIndexOf('\\') + 1));
                        }
                    }
                    //Converting the array list to a normal array of 
		  //strings to the public array
                    Final_Adresses = (string[])address.ToArray(typeof(string));

Above, we make some checks to determine if the song is in the same directory of the playlist or in another place.

Save or Copy Button

We check if the user checked any songs or NOT, then we copy them.

C#
if (checkedListBox1.CheckedItems.Count > 0)
            {
                DialogResult result = folderBrowserDialog1.ShowDialog();
                if (result == DialogResult.OK)
                {
                    ProgressBar1.Maximum = checkedListBox1.CheckedItems.Count;
                    for (int i = 0; i < Final_Adresses.Length; i++)
                    {
                        if (checkedListBox1.GetItemCheckState(i) == CheckState.Checked)
                        {
                            File.Copy(Final_Adresses[i], 
				folderBrowserDialog1.SelectedPath + 
				Final_Adresses[i].Remove(0, 
				Final_Adresses[i].LastIndexOf('\\')));
                            ProgressBar1.PerformStep();
                        }
                    }
                    MessageBox.Show("Copying Selected Items Completed");
                }
            }
            else
            {
                MessageBox.Show("No Items Selected To Copy");
            } 

Points of Interest

You can deal with any playlist with the same way. Other formats are easier to get the song path as they store the full path with NO conditions for its location.

I may make another copier in the future. If you are interested in this, please visit my blog www.free-codes.blogspot.com.

History

  • 25th May, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Egypt Egypt
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Answersh*t Pin
vahotm27-May-12 9:12
vahotm27-May-12 9:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.