mp3finder
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
Source code (mp3finder.cs)
-
using System;
-
using System.Collections.Generic;
-
using System.ComponentModel;
-
using System.Data;
-
using System.Drawing;
-
using System.Text;
-
using System.Windows.Forms;
-
using System.IO;
-
using System.Runtime.InteropServices;
-
using System.Diagnostics;
-
using System.Security.Principal;
-
-
using IniHandling;
-
-
namespace MP3finder
-
{
-
public partial class mp3finder : Form
-
{
-
static internal bool IsAdmin()
-
{
-
WindowsIdentity id = WindowsIdentity.GetCurrent();
-
return p.IsInRole(WindowsBuiltInRole.Administrator);
-
}
-
-
internal static void RestartElevated()
-
{
-
startInfo.UseShellExecute = true;
-
startInfo.WorkingDirectory = Environment.CurrentDirectory;
-
startInfo.FileName = Application.ExecutablePath;
-
startInfo.Verb = "runas";
-
try
-
{
-
Process p = Process.Start(startInfo);
-
}
-
catch
-
{
-
return;
-
}
-
-
Application.Exit();
-
}
-
-
-
private string folderNameIn = "", folderNameOut = "";
-
-
-
public mp3finder()
-
{
-
InitializeComponent();
-
-
this.fbd.ShowNewFolderButton = true;
-
this.fbd.RootFolder = Environment.SpecialFolder.Personal;
-
-
if (!IsAdmin())
-
{
-
RestartElevated();
-
Application.Exit();
-
this.Close();
-
}
-
}
-
-
private void button1_Click(object sender, EventArgs e)
-
{
-
folderNameIn = textBox2.Text;
-
folderNameOut = textBox3.Text;
-
if (folderNameIn.Length < 1 || folderNameOut.Length < 1)
-
{
-
MessageBox.Show("You need to choose in and out folders first");
-
return;
-
}
-
-
ini.to("in", folderNameIn);
-
ini.to("out", folderNameOut);
-
textBox1.Clear();
-
-
textBox1.Text += "Opening directory ‘" + folderNameIn + "’..\r\n";
-
EnumerateDir(di);
-
DirectoryInfo[] dirs = di.GetDirectories();
-
foreach (DirectoryInfo dir in dirs)
-
{
-
EnumerateDir(dir);
-
}
-
-
textBox1.Text += "Done!\r\n";
-
}
-
-
private void EnumerateDir(DirectoryInfo dir)
-
{
-
textBox1.Text += "Listing files in " + dir.Name + "..\r\n";
-
FileInfo[] files = dir.GetFiles();
-
foreach (FileInfo fi in files)
-
{
-
try
-
{
-
//textBox1.Text += "Opening " + fi.Name + " for reading\r\n";
-
FileStream fs = fi.OpenRead();
-
fs.Read(readBuffer, 0, 15);
-
//textBox1.Text += "Read file-header\r\n";
-
fs.Close();
-
-
if ((readBuffer[0] == ‘I’ &&
-
readBuffer[1] == ‘D’ &&
-
readBuffer[2] == ’3′)
-
|| (
-
readBuffer[0] == ‘i’ &&
-
readBuffer[1] == ‘d’ &&
-
readBuffer[2] == ’3′))
-
{
-
//textBox1.Text += fi.Name + " is an MP3!\r\n";
-
if (!folderNameOut.EndsWith("\\"))
-
{
-
folderNameOut += "\\";
-
}
-
if (!File.Exists(folderNameOut + fi.Name + ".mp3"))
-
{
-
try
-
{
-
File.Copy(fi.FullName, folderNameOut + fi.Name + ".mp3");
-
textBox1.Text += fi.Name + " has been copied to " + folderNameOut + fi.Name + ".mp3\r\n";
-
try { File.Delete(fi.FullName); }
-
catch { textBox1.Text += "Failed to delete " + fi.Name + " try manually.\r\n"; }
-
}
-
catch { textBox1.Text += "Failed to copy " + fi.Name + " try manually.\r\n"; }
-
}
-
else
-
textBox1.Text += "File has already been copied\r\n";
-
}
-
}
-
catch
-
{
-
-
}
-
}
-
}
-
-
private void button2_Click(object sender, EventArgs e)
-
{
-
DialogResult result = fbd.ShowDialog();
-
if (result == DialogResult.OK)
-
{
-
folderNameIn = fbd.SelectedPath;
-
textBox2.Text = folderNameIn;
-
}
-
}
-
-
private void button3_Click(object sender, EventArgs e)
-
{
-
DialogResult result = fbd.ShowDialog();
-
if( result == DialogResult.OK )
-
{
-
folderNameOut = fbd.SelectedPath;
-
textBox3.Text = folderNameOut;
-
}
-
}
-
-
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
-
{
-
ini.to("in", folderNameIn);
-
ini.to("out", folderNameOut);
-
}
-
-
private void Form1_Load(object sender, EventArgs e)
-
{
-
ini.open(Application.StartupPath + "\\Settings.ini");
-
folderNameIn = ini.from("in");
-
folderNameOut = ini.from("out");
-
textBox2.Text = folderNameIn;
-
textBox3.Text = folderNameOut;
-
}
-
}
-
}
Source code (ini.cs)
-
using System.Text;
-
using System.Runtime.InteropServices;
-
-
namespace IniHandling
-
{
-
public class Ini
-
{
-
[DllImport("kernel32")]
-
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
-
[DllImport("kernel32")]
-
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
-
-
private string iniPath = "";
-
private static string section = "General";
-
-
public void open(string path)
-
{
-
if (path.IndexOf(‘\\‘) == -1)
-
iniPath = ".\\" + path;
-
else
-
iniPath = path;
-
}
-
-
public string from(string keyname)
-
{
-
GetPrivateProfileString(section, keyname, "", ret, 1024, this.iniPath);
-
return ret.ToString();
-
}
-
-
public uint getuint(string keyname)
-
{
-
GetPrivateProfileString(section, keyname, "", ret, 1024, this.iniPath);
-
if (ret.ToString() == "") return 0;
-
return uint.Parse(ret.ToString());
-
}
-
-
public int getint(string keyname)
-
{
-
GetPrivateProfileString(section, keyname, "", ret, 1024, this.iniPath);
-
if (ret.ToString() == "") return 0;
-
return int.Parse(ret.ToString());
-
}
-
-
public double getdouble(string keyname)
-
{
-
GetPrivateProfileString(section, keyname, "", ret, 1024, this.iniPath);
-
if (ret.ToString() == "") return 0;
-
return double.Parse(ret.ToString());
-
}
-
-
public bool getbool(string keyname)
-
{
-
GetPrivateProfileString(section, keyname, "", ret, 1024, this.iniPath);
-
if (ret.ToString() == "") return false;
-
return bool.Parse(ret.ToString());
-
}
-
-
public void to(string keyname, string value)
-
{
-
WritePrivateProfileString(section, keyname, value, this.iniPath);
-
}
-
}
-
}
Source code (mp3finder.Designer.cs)
-
namespace MP3finder
-
{
-
partial class mp3finder
-
{
-
/// <summary>
-
/// Required designer variable.
-
/// </summary>
-
private System.ComponentModel.IContainer components = null;
-
-
/// <summary>
-
/// Clean up any resources being used.
-
/// </summary>
-
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
-
protected override void Dispose(bool disposing)
-
{
-
if (disposing && (components != null))
-
{
-
components.Dispose();
-
}
-
base.Dispose(disposing);
-
}
-
-
#region Windows Form Designer generated code
-
-
/// <summary>
-
/// Required method for Designer support – do not modify
-
/// the contents of this method with the code editor.
-
/// </summary>
-
private void InitializeComponent()
-
{
-
this.SuspendLayout();
-
//
-
// button1
-
//
-
this.button1.Name = "button1";
-
this.button1.TabIndex = 0;
-
this.button1.Text = "Start";
-
this.button1.UseVisualStyleBackColor = true;
-
//
-
// textBox1
-
//
-
this.textBox1.Cursor = System.Windows.Forms.Cursors.Default;
-
this.textBox1.Multiline = true;
-
this.textBox1.Name = "textBox1";
-
this.textBox1.ReadOnly = true;
-
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
-
this.textBox1.TabIndex = 1;
-
//
-
// textBox2
-
//
-
this.textBox2.Name = "textBox2";
-
this.textBox2.TabIndex = 2;
-
//
-
// textBox3
-
//
-
this.textBox3.Name = "textBox3";
-
this.textBox3.TabIndex = 3;
-
//
-
// button2
-
//
-
this.button2.Name = "button2";
-
this.button2.TabIndex = 4;
-
this.button2.Text = "In directory";
-
this.button2.UseVisualStyleBackColor = true;
-
//
-
// button3
-
//
-
this.button3.Name = "button3";
-
this.button3.TabIndex = 5;
-
this.button3.Text = "Out directory";
-
this.button3.UseVisualStyleBackColor = true;
-
//
-
// Form1
-
//
-
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
-
this.Controls.Add(this.button3);
-
this.Controls.Add(this.button2);
-
this.Controls.Add(this.textBox3);
-
this.Controls.Add(this.textBox2);
-
this.Controls.Add(this.textBox1);
-
this.Controls.Add(this.button1);
-
this.Name = "Form1";
-
this.Text = "MP3 finder";
-
this.ResumeLayout(false);
-
this.PerformLayout();
-
-
}
-
-
#endregion
-
-
private System.Windows.Forms.Button button1;
-
private System.Windows.Forms.TextBox textBox1;
-
private System.Windows.Forms.TextBox textBox2;
-
private System.Windows.Forms.TextBox textBox3;
-
private System.Windows.Forms.Button button2;
-
private System.Windows.Forms.Button button3;
-
}
-
}

Leave a Reply