#!/usr/bin/env ruby require 'mp3info' require 'fileutils' case ARGV[0] when 'artist', 'genre', 'comments', 'title', 'album', 'tracknum' Type_tri = ARGV[0] else puts "\ synopsis: tri_mp3.rb type_tri [chemin] où type_ tri peut être, au choix: artist, genre, comments, title, album, tracknum tri_mp3 permet de classer des fichiers musicaux en les placant dans un répertoire suivant un critère commun que vous aurez choisi, sur la base d'une balise mp3. Si 'chemin' n'est pas spécifié, tri_mp3.rb sera lancé dans le répertoire courant." end chemin = ARGV[1] if chemin != nil and File.directory?( chemin ) chemin += '/' if chemin[/\/$/] == nil repertoire = Dir.new( chemin ) else repertoire = Dir.new( './' ) end str_rep = repertoire.path repertoire.each { |fichier| if File.extname( fichier ) == '.mp3' puts "traitement de " + fichier + '...' f_mp3 = Mp3Info.new( fichier ) balise = f_mp3.tag[ Type_tri ] balise = f_mp3.tag2['TCON'] if Type_tri == 'genre' and balise == nil balise = 'inconnu' if balise == nil balise.delete!( "(" + ")" + "*" + "/" + "\\" + "\0" ) puts str_rep + balise Dir.mkdir( "#{str_rep + balise}" ) if not File.exist?( "#{str_rep + balise}" ) FileUtils.mv( str_rep + fichier, str_rep + balise + '/' + fichier ) end }