Click here to Skip to main content
Licence CPOL
First Posted 25 May 2009
Views 8,194
Downloads 148
Bookmarked 11 times

m3u Playlist Copier

By pcprogramer | 25 May 2009
Lets you choose the songs from m3u playlist and copy all checked songs to a specified folder

1

2
1 vote, 100.0%
3

4

5
3.00/5 - 1 vote
μ 3.00, σa 5.00 [?]
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).

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

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

//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.  

//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.

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)

About the Author

pcprogramer

Other

Egypt Egypt

Member
Hi all
I am just a student in Computer Science from Egypt who wants to every thing about technology around me
 
I am very ambitious and have a lot of dreams and goals to achieve
 
My interests now (this days) is every thing about web , but i still having fun with windows applications under .Net
 
Actually i try to make all simple programs i need in my usual tasks
 
You can watch my progress in my small blog where i try to (learn by doing)
 
There , I try to upload and explain my programs from my made wishing it may be useful to any one , as i said before i make it for my usual tasks and needs
 
I also provide the explained source code ,so any one can edit the programs to be suitable for his usage or needs
 
Please comment there with your opinions
Visit My Blog at wWw.Free-Codes.BlogSpot.com
 
Also I'll be very happy if any one invited me to any co-operation in any Project

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120209.1 | Last Updated 25 May 2009
Article Copyright 2009 by pcprogramer
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid