Code covered by the BSD License  

Highlights from
Export MP3 songs from playlist to separate folder

from Export MP3 songs from playlist to separate folder by Andre
Exports MP3 files to separate folder. If not present in suggested folder, files will be searched.

export_m3u_playlist(playlist,hd,searchfolder,exportfolder)
function export_m3u_playlist(playlist,hd,searchfolder,exportfolder)
% export_m3u_playlist(playlist,hd,searchfolder) opens the playlist whose
% files are on hard disk hd and copies them to the exportfolder. If the
% files are not in the place written in the playlist, they will be searched
% in the searchfolder.
% This version works only with windows systems, because the 'dir' command
% in windows cmd is used!
% Duplicate files in the exportfolder will be overwritten!
% Name of hard disk drive has to be given, because it is not given in
% winamp created playlist files! If it exists in other playlist files it
% has to be removed.
% Watch for the use of the backslash in the directory names! Study the
% example.
%
% Example:
%
% playlist = 'my_list.m3u';
% playlist = 'C:\my_plalists\my_list.m3u';
% 
% hard disk drive where mp3 files in playlist are located
% hd = 'C:';
%
% search folder where to search for the songs, if they are not in the
% folder suggested in the playlist
% searchfolder = 'C:\mp3';
%
% copy folder
% exportfolder = 'C:\tempdir\';


% current directory
cdir = cd;

% create export directory if not existing
if ~isdir(exportfolder)
    mkdir(exportfolder);
end    

% open playlist
fid = fopen(playlist);

% read playlist
list = textscan(fid,'%s','delimiter','\n');

% close playlist file
fclose(fid)

% copy from 1x1 cell array to cell array of strings
list = list{1};


for i=1:length(list)
    
   % convert to string 
   line = char(list(i)); 
    
   % if line contains mp3 file
   if strcmp(line(end-2:end),'mp3') 
    
       % find directory delimiters
       backslash = strfind(line,'\');
       
       % get songname and directory
       songname = line(backslash(end)+1:end);
       songdir  = line(1:backslash(end));
       
       % now try to find the songname in original destination
       [status,result] = dos(['dir "',hd,songdir,songname,'" /S /B']);
       
       % check result       
       if status==0 % file is in original folder        
           copyfile([hd,songdir,songname] , [exportfolder,songname],'f');
           disp(['file found in original folder:',songname])
       else % search for file
           % go to search directory
           cd(searchfolder)
           [status,result] = dos(['dir "',songname,'" /S /B']);
           
           if status == 0 % file is in search directory
               copyfile([searchfolder,songdir,songname] , [exportfolder,songname],'f');
               disp(['file found in folder: ',searchfolder,': ',songname])
           else % file is not found at all
               disp(['DID NOT FIND FILE: ',songname]); 
           end
                
       end    
      
   end
end    

% go back to starting directory
cd(cdir);

Contact us at files@mathworks.com