Tuesday, December 18, 2012

MATLAB -> Dir2html (Create Html List of Directories)


Script to create a HTML file of a given Directory:
 Following is the Script that takes in the directory Path and outputs a Dir.html file. Extremely Useful for Large Databases of Music and Movies..Enjoy...

Download link from Matlab-Central :
http://www.mathworks.com/matlabcentral/fileexchange/44849-dir2html

Consists of Two files ->
1. File.m
2. File2html.m
          Run the File.m and specify the Directory Path...Code for the same are as below...

%%% Code for File.m Begins here %%%%

fid = fopen('DirList.html','wb');
dir = input('Enter The Directory :','s');
fprintf(fid,'%s\n%s','<html>','<body>');
File2html(dir);
fprintf(fid,'\n%s\n%s','</body>','</html>');   
fclose(fid);
%%%%%%%% Code Ends %%%%%%%%

%%% Code for File2html.m Begins here %%%%

function File2html(Directory)
fid = evalin('base','fid');
fprintf(fid,'%s','<b>');
fprintf(fid,'%s',Directory);
fprintf(fid,'%s','</b>');
Dirnames = dir (Directory);
for i=3:size(Dirnames,1)
   if (Dirnames(i).isdir )
       newdir = strcat(Directory,'\',Dirnames(i).name);
       File2html(newdir);
   else
       filename = Dirnames(i).name;
       %fprintf(fid,'%s%s',i,'>');
       link=(strcat('<p><a href="',Directory,'\',filename,'">'));   
       fprintf(fid,'%s',link);
       fprintf(fid,'%s',filename);
       fprintf(fid,'%s\n','</a></p>');
   end 
end
end
%%%%%%%% Code Ends %%%%%%%%