LoginSignup
8
7

More than 5 years have passed since last update.

mp3ファイルをアルバムごとにフォルダに振り分ける

Last updated at Posted at 2014-03-12

Shell32の参照設定を行う必要があります。
Windows 7でしか動作しないと思います。
(2014/3/12 追記:.m4aファイルにも対応しています。私の環境では、.mp3と同様に動作します。)

mp3ID3.cs
using System;
using System.IO;
using System.Text;
using Shell32;

class mp3ID3 {
    static void Main() {
        string currentDir = System.Environment.CurrentDirectory;
        ShellClass shell = new ShellClass();
        Folder folder = shell.NameSpace(currentDir);
        DirectoryInfo di = new DirectoryInfo(currentDir);
        FileInfo[] fiary = di.GetFiles("*.mp3");
        Encoding sjisEnc = Encoding.GetEncoding("Shift_JIS");
        StreamWriter sw = new StreamWriter(currentDir + "\\" + "log.txt", false, sjisEnc);

        foreach (FileInfo fi in fiary) {
            string artistName = folder.GetDetailsOf(folder.ParseName(fi.Name), 13); // アーティスト名
            string albumName = folder.GetDetailsOf(folder.ParseName(fi.Name), 14);  // アルバム名
            string artistDir = currentDir + "\\" + artistName;
            string albumDir = artistDir + "\\" + albumName;
            DirectoryInfo diNewArtistFolder = new DirectoryInfo(artistDir);
            DirectoryInfo diNewAlbumFolder = new DirectoryInfo(albumDir);

            if (!File.Exists(artistDir)) {
                diNewArtistFolder.Create();
            }
            if (!File.Exists(albumDir)) {
                diNewAlbumFolder.Create();
            }

            sw.WriteLine(fi.FullName);
            sw.WriteLine("を");
            try {
                fi.MoveTo(albumDir+"\\"+fi.Name);
            } catch (Exception e) {
                Console.Error.WriteLine(e);
                Console.Error.WriteLine("何かキーを押してください.");
                Console.ReadLine();
                return;
            }
            sw.WriteLine(fi.FullName);
            sw.WriteLine("に移動しました。");
            sw.WriteLine();
        }

        sw.Close();
    }
}

8
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
7