Posted in Programming by Mr. Novocain on August 5, 2010 at 17:12

mp3finder

Get A Quick And Easy Ebook That Introduces Readers To Computer Programming In A Jargon Free, Easy To Understand Way. now!

Download

About
Searches through all files in a directory and finds which are MP3-files then moves them to selected folder.

This becomes useful when pointed to the cache-folder of your web browser, for finding MP3′s you have been listening to online using various flash players (llike Grooveshark for example)

This is done by reading the header of each file, a MP3-file starts with “ID3″ (hex 49 44 33).

Instructions

  • Start mp3finder.exe (if using Vista/Windows 7, right-click and “Run as administrator”)
  • Click “in directory” or fill the textbox next to it with the path to read from
  • Click “out directory” or fill the textbox next to it with the path to save to
  • Click “Start”!
  • Just wait.. it can take some minutes, depending on size and number of files

  • Source code (mp3finder.cs)

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Text;
    7. using System.Windows.Forms;
    8. using System.IO;
    9. using System.Runtime.InteropServices;
    10. using System.Diagnostics;
    11. using System.Security.Principal;
    12.  
    13. using IniHandling;
    14.  
    15. namespace MP3finder
    16. {
    17.     public partial class mp3finder : Form
    18.     {
    19.         static internal bool IsAdmin()
    20.         {
    21.             WindowsIdentity id = WindowsIdentity.GetCurrent();
    22.             WindowsPrincipal p = new WindowsPrincipal(id);
    23.             return p.IsInRole(WindowsBuiltInRole.Administrator);
    24.         }
    25.  
    26.         internal static void RestartElevated()
    27.         {
    28.             ProcessStartInfo startInfo = new ProcessStartInfo();
    29.             startInfo.UseShellExecute = true;
    30.             startInfo.WorkingDirectory = Environment.CurrentDirectory;
    31.             startInfo.FileName = Application.ExecutablePath;
    32.             startInfo.Verb = "runas";
    33.             try
    34.             {
    35.                 Process p = Process.Start(startInfo);
    36.             }
    37.             catch
    38.             {
    39.                 return;
    40.             }
    41.  
    42.             Application.Exit();
    43.         }
    44.  
    45.         private FolderBrowserDialog fbd = new FolderBrowserDialog();
    46.  
    47.         private string folderNameIn = "", folderNameOut = "";
    48.  
    49.         Ini ini = new Ini();
    50.  
    51.         public mp3finder()
    52.         {
    53.             InitializeComponent();
    54.  
    55.             this.fbd.ShowNewFolderButton = true;
    56.             this.fbd.RootFolder = Environment.SpecialFolder.Personal;
    57.  
    58.             if (!IsAdmin())
    59.             {
    60.                 RestartElevated();
    61.                 Application.Exit();
    62.                 this.Close();
    63.             }
    64.         }
    65.  
    66.         private void button1_Click(object sender, EventArgs e)
    67.         {
    68.             folderNameIn = textBox2.Text;
    69.             folderNameOut = textBox3.Text;
    70.             if (folderNameIn.Length < 1 || folderNameOut.Length < 1)
    71.             {
    72.                 MessageBox.Show("You need to choose in and out folders first");
    73.                 return;
    74.             }
    75.  
    76.             ini.to("in", folderNameIn);
    77.             ini.to("out", folderNameOut);
    78.             textBox1.Clear();
    79.  
    80.             textBox1.Text += "Opening directory ‘" + folderNameIn + "’..\r\n";
    81.             DirectoryInfo di = new DirectoryInfo(folderNameIn);
    82.             EnumerateDir(di);
    83.             DirectoryInfo[] dirs = di.GetDirectories();
    84.             foreach (DirectoryInfo dir in dirs)
    85.             {
    86.                 EnumerateDir(dir);
    87.             }
    88.  
    89.             textBox1.Text += "Done!\r\n";
    90.         }
    91.  
    92.         private void EnumerateDir(DirectoryInfo dir)
    93.         {
    94.             textBox1.Text += "Listing files in " + dir.Name + "..\r\n";
    95.             FileInfo[] files = dir.GetFiles();
    96.             foreach (FileInfo fi in files)
    97.             {
    98.                 try
    99.                 {
    100.                     //textBox1.Text += "Opening " + fi.Name + " for reading\r\n";
    101.                     FileStream fs = fi.OpenRead();
    102.                     byte[] readBuffer = new byte[16];
    103.                     fs.Read(readBuffer, 0, 15);
    104.                     //textBox1.Text += "Read file-header\r\n";
    105.                     fs.Close();
    106.  
    107.                     if ((readBuffer[0] == ‘I’ &&
    108.                         readBuffer[1] == ‘D’ &&
    109.                         readBuffer[2] == ’3′)
    110.                         || (
    111.                         readBuffer[0] == ‘i’ &&
    112.                         readBuffer[1] == ‘d’ &&
    113.                         readBuffer[2] == ’3′))
    114.                     {
    115.                         //textBox1.Text += fi.Name + " is an MP3!\r\n";
    116.                         if (!folderNameOut.EndsWith("\\"))
    117.                         {
    118.                             folderNameOut += "\\";
    119.                         }
    120.                         if (!File.Exists(folderNameOut + fi.Name + ".mp3"))
    121.                         {
    122.                             try
    123.                             {
    124.                                 File.Copy(fi.FullName, folderNameOut + fi.Name + ".mp3");
    125.                                 textBox1.Text += fi.Name + " has been copied to " + folderNameOut + fi.Name + ".mp3\r\n";
    126.                                 try { File.Delete(fi.FullName); }
    127.                                 catch { textBox1.Text += "Failed to delete " + fi.Name + " try manually.\r\n"; }
    128.                             }
    129.                             catch { textBox1.Text += "Failed to copy " + fi.Name + " try manually.\r\n"; }
    130.                         }
    131.                         else
    132.                             textBox1.Text += "File has already been copied\r\n";
    133.                     }
    134.                 }
    135.                 catch
    136.                 {
    137.  
    138.                 }
    139.             }
    140.         }
    141.  
    142.         private void button2_Click(object sender, EventArgs e)
    143.         {
    144.             DialogResult result = fbd.ShowDialog();
    145.             if (result == DialogResult.OK)
    146.             {
    147.                 folderNameIn = fbd.SelectedPath;
    148.                 textBox2.Text = folderNameIn;
    149.             }
    150.         }
    151.  
    152.         private void button3_Click(object sender, EventArgs e)
    153.         {
    154.             DialogResult result = fbd.ShowDialog();
    155.             if( result == DialogResult.OK )
    156.             {
    157.                 folderNameOut = fbd.SelectedPath;
    158.                 textBox3.Text = folderNameOut;
    159.             }
    160.         }
    161.  
    162.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    163.         {
    164.             ini.to("in", folderNameIn);
    165.             ini.to("out", folderNameOut);
    166.         }
    167.  
    168.         private void Form1_Load(object sender, EventArgs e)
    169.         {
    170.             ini.open(Application.StartupPath + "\\Settings.ini");
    171.             folderNameIn = ini.from("in");
    172.             folderNameOut = ini.from("out");
    173.             textBox2.Text = folderNameIn;
    174.             textBox3.Text = folderNameOut;
    175.         }
    176.     }
    177. }

    Source code (ini.cs)

    1. using System.Text;
    2. using System.Runtime.InteropServices;
    3.  
    4. namespace IniHandling
    5. {
    6.     public class Ini
    7.     {
    8.         [DllImport("kernel32")]
    9.         private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
    10.         [DllImport("kernel32")]
    11.         private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
    12.  
    13.         private string iniPath = "";
    14.         private static string section = "General";
    15.  
    16.         public void open(string path)
    17.         {
    18.             if (path.IndexOf(\\) == -1)
    19.                 iniPath = ".\\" + path;
    20.             else
    21.                 iniPath = path;
    22.         }
    23.  
    24.         public string from(string keyname)
    25.         {
    26.             StringBuilder ret = new StringBuilder(1024);
    27.             GetPrivateProfileString(section, keyname, "", ret, 1024, this.iniPath);
    28.             return ret.ToString();
    29.         }
    30.  
    31.         public uint getuint(string keyname)
    32.         {
    33.             StringBuilder ret = new StringBuilder(1024);
    34.             GetPrivateProfileString(section, keyname, "", ret, 1024, this.iniPath);
    35.             if (ret.ToString() == "") return 0;
    36.             return uint.Parse(ret.ToString());
    37.         }
    38.  
    39.         public int getint(string keyname)
    40.         {
    41.             StringBuilder ret = new StringBuilder(1024);
    42.             GetPrivateProfileString(section, keyname, "", ret, 1024, this.iniPath);
    43.             if (ret.ToString() == "") return 0;
    44.             return int.Parse(ret.ToString());
    45.         }
    46.  
    47.         public double getdouble(string keyname)
    48.         {
    49.             StringBuilder ret = new StringBuilder(1024);
    50.             GetPrivateProfileString(section, keyname, "", ret, 1024, this.iniPath);
    51.             if (ret.ToString() == "") return 0;
    52.             return double.Parse(ret.ToString());
    53.         }
    54.  
    55.         public bool getbool(string keyname)
    56.         {
    57.             StringBuilder ret = new StringBuilder(1024);
    58.             GetPrivateProfileString(section, keyname, "", ret, 1024, this.iniPath);
    59.             if (ret.ToString() == "") return false;
    60.             return bool.Parse(ret.ToString());
    61.         }
    62.  
    63.         public void to(string keyname, string value)
    64.         {
    65.             WritePrivateProfileString(section, keyname, value, this.iniPath);
    66.         }
    67.     }
    68. }

    Source code (mp3finder.Designer.cs)

    1. namespace MP3finder
    2. {
    3.     partial class mp3finder
    4.     {
    5.         /// <summary>
    6.         /// Required designer variable.
    7.         /// </summary>
    8.         private System.ComponentModel.IContainer components = null;
    9.  
    10.         /// <summary>
    11.         /// Clean up any resources being used.
    12.         /// </summary>
    13.         /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    14.         protected override void Dispose(bool disposing)
    15.         {
    16.             if (disposing && (components != null))
    17.             {
    18.                 components.Dispose();
    19.             }
    20.             base.Dispose(disposing);
    21.         }
    22.  
    23.         #region Windows Form Designer generated code
    24.  
    25.         /// <summary>
    26.         /// Required method for Designer support – do not modify
    27.         /// the contents of this method with the code editor.
    28.         /// </summary>
    29.         private void InitializeComponent()
    30.         {
    31.             this.button1 = new System.Windows.Forms.Button();
    32.             this.textBox1 = new System.Windows.Forms.TextBox();
    33.             this.textBox2 = new System.Windows.Forms.TextBox();
    34.             this.textBox3 = new System.Windows.Forms.TextBox();
    35.             this.button2 = new System.Windows.Forms.Button();
    36.             this.button3 = new System.Windows.Forms.Button();
    37.             this.SuspendLayout();
    38.             //
    39.             // button1
    40.             //
    41.             this.button1.Location = new System.Drawing.Point(400, 403);
    42.             this.button1.Name = "button1";
    43.             this.button1.Size = new System.Drawing.Size(129, 46);
    44.             this.button1.TabIndex = 0;
    45.             this.button1.Text = "Start";
    46.             this.button1.UseVisualStyleBackColor = true;
    47.             this.button1.Click += new System.EventHandler(this.button1_Click);
    48.             //
    49.             // textBox1
    50.             //
    51.             this.textBox1.Cursor = System.Windows.Forms.Cursors.Default;
    52.             this.textBox1.Location = new System.Drawing.Point(12, 12);
    53.             this.textBox1.Multiline = true;
    54.             this.textBox1.Name = "textBox1";
    55.             this.textBox1.ReadOnly = true;
    56.             this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
    57.             this.textBox1.Size = new System.Drawing.Size(517, 385);
    58.             this.textBox1.TabIndex = 1;
    59.             //
    60.             // textBox2
    61.             //
    62.             this.textBox2.Location = new System.Drawing.Point(131, 403);
    63.             this.textBox2.Name = "textBox2";
    64.             this.textBox2.Size = new System.Drawing.Size(263, 20);
    65.             this.textBox2.TabIndex = 2;
    66.             //
    67.             // textBox3
    68.             //
    69.             this.textBox3.Location = new System.Drawing.Point(131, 429);
    70.             this.textBox3.Name = "textBox3";
    71.             this.textBox3.Size = new System.Drawing.Size(263, 20);
    72.             this.textBox3.TabIndex = 3;
    73.             //
    74.             // button2
    75.             //
    76.             this.button2.Location = new System.Drawing.Point(12, 403);
    77.             this.button2.Name = "button2";
    78.             this.button2.Size = new System.Drawing.Size(113, 20);
    79.             this.button2.TabIndex = 4;
    80.             this.button2.Text = "In directory";
    81.             this.button2.UseVisualStyleBackColor = true;
    82.             this.button2.Click += new System.EventHandler(this.button2_Click);
    83.             //
    84.             // button3
    85.             //
    86.             this.button3.Location = new System.Drawing.Point(12, 429);
    87.             this.button3.Name = "button3";
    88.             this.button3.Size = new System.Drawing.Size(113, 20);
    89.             this.button3.TabIndex = 5;
    90.             this.button3.Text = "Out directory";
    91.             this.button3.UseVisualStyleBackColor = true;
    92.             this.button3.Click += new System.EventHandler(this.button3_Click);
    93.             //
    94.             // Form1
    95.             //
    96.             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    97.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    98.             this.ClientSize = new System.Drawing.Size(541, 461);
    99.             this.Controls.Add(this.button3);
    100.             this.Controls.Add(this.button2);
    101.             this.Controls.Add(this.textBox3);
    102.             this.Controls.Add(this.textBox2);
    103.             this.Controls.Add(this.textBox1);
    104.             this.Controls.Add(this.button1);
    105.             this.Name = "Form1";
    106.             this.Text = "MP3 finder";
    107.             this.Load += new System.EventHandler(this.Form1_Load);
    108.             this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
    109.             this.ResumeLayout(false);
    110.             this.PerformLayout();
    111.  
    112.         }
    113.  
    114.         #endregion
    115.  
    116.         private System.Windows.Forms.Button button1;
    117.         private System.Windows.Forms.TextBox textBox1;
    118.         private System.Windows.Forms.TextBox textBox2;
    119.         private System.Windows.Forms.TextBox textBox3;
    120.         private System.Windows.Forms.Button button2;
    121.         private System.Windows.Forms.Button button3;
    122.     }
    123. }
     

    Leave a Reply