#cs
	================================
	Music Tagger.exe   -    Music Tagger by Stefan Ruf aka Scripter192
	================================
	License:

	You may redistribute this script or parts of it IF you credit me
	================================
#ce








#NoTrayIcon
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Compression=0
#AutoIt3Wrapper_UseUpx=n
#AutoIt3Wrapper_Res_Comment=visit www.scripter192.bplaced.de
#AutoIt3Wrapper_Res_Description=Music Tagger
#AutoIt3Wrapper_Res_Fileversion=1.0.0.0
#AutoIt3Wrapper_Res_LegalCopyright=Scripter192
#AutoIt3Wrapper_Res_Language=1031
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <ID3.au3>
#include <file.au3>
#include <array.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ID3.au3>
#include <ButtonConstants.au3>
$Form1 = GUICreate("Music Tagger", 487, 217, 193, 370)
$Label1 = GUICtrlCreateLabel("Are those informations alright?", 24, 16, 438, 36)
GUICtrlSetFont(-1, 20, 400, 0, "Arial")
$Label2 = GUICtrlCreateLabel("Name:", 24, 72, 442, 20)
GUICtrlSetFont(-1, 10, 400, 0, "Arial")
$Label3 = GUICtrlCreateLabel("Titele:", 24, 98, 40, 20)
GUICtrlSetFont(-1, 10, 400, 0, "Arial")
$Label4 = GUICtrlCreateLabel("Artist:", 24, 123, 40, 20)
GUICtrlSetFont(-1, 10, 400, 0, "Arial")
$Button1 = GUICtrlCreateButton("Yes", 8, 152, 233, 57, $BS_DEFPUSHBUTTON)
$Button2 = GUICtrlCreateButton("No", 240, 152, 233, 57)
$Input1 = GUICtrlCreateInput("Input1", 64, 96, 393, 21)
$Input2 = GUICtrlCreateInput("Input1", 64, 120, 393, 21)
$Button3 = GUICtrlCreateButton("", 464, 96, 17, 49)
$dir = FileSelectFolder("Select a folder to proceed...", "C:" & @HomePath)
If $dir = "" Then Exit
ProgressOn("Processing...", "Scanning folder for mp3 files...", "0%", Default, Default, 16)
Dim $mp3s[1]
$mp3s[0] = 0
search($dir)
ProgressSet(0, "", "Searching for data...")
Dim $artist[1][2]
$artist[0][0] = 0

Dim $title[1][2]
$title[0][0] = 0
For $i = 1 To $mp3s[0]
	$filename = StringSplit(StringReplace($mp3s[$i], ".mp3", ""), "\")
	$filename = $filename[$filename[0]]
	ProgressSet($i / $mp3s[0] * 100, $filename)


	$data = StringSplit($filename, "-")
	If $data[0] = 2 Then
		_ID3ReadTag($mp3s[$i])
		If ((_ID3GetTagField("TIT2") = "" and  _ID3GetTagField("Title") = "") or (_ID3GetTagField("TPE1") = "" and _ID3GetTagField("Artist") = "")) Then
			$data[1] = StringStripWS($data[1], 7)
			$data[2] = StringStripWS($data[2], 7)
			If $data[1] <> "" And $data[2] <> "" Then
				$artist[0][0] += 1
				ReDim $artist[$artist[0][0] + 1][2]
				$artist[$artist[0][0]][0] = $data[1]
				$artist[$artist[0][0]][1] = $i
				$title[0][0] += 1
				ReDim $title[$title[0][0] + 1][2]
				$title[$title[0][0]][0] = $data[2]
				$title[$title[0][0]][1] = $i
			EndIf
		EndIf
	EndIf
Next
ProgressOff()
If $title[0][0] = 0 Then
	MsgBox(32, "Music Tagger", "No Un-Tagged mp3 files found!")
	Exit
EndIf
If (MsgBox(36, "Music Tagger", $title[0][0] & " files found! Do you want to proceed?" & @CRLF & "( This could take some time, since you have to review every file )") = 7) Then Exit

For $i = 1 To $title[0][0]
	$filename = StringSplit(StringReplace($mp3s[$title[$i][1]], ".mp3", ""), "\")
	$filename = $filename[$filename[0]]
	GUICtrlSetData($Label2, "Name: " & $filename)
	GUICtrlSetData($Input1, $title[$i][0])
	GUICtrlSetData($Input2, $artist[$i][0])
	GUISetState(@SW_SHOW)
	$go = False
	$proceed = False
	While Sleep(100)
		$msg = GUIGetMsg()
		If $msg = $Button1 Then

			$proceed = True
			ExitLoop
		ElseIf $msg = $Button2 Then
			ExitLoop
		ElseIf $msg = $Button3 Then
			$t1 = GUICtrlRead($Input1)
			$t2 = GUICtrlRead($Input2)
			GUICtrlSetData($Input1, $t2)
			GUICtrlSetData($Input2, $t1)
		EndIf

	WEnd
	$t1 = GUICtrlRead($Input1)
	$t2 = GUICtrlRead($Input2)
	GUISetState(@SW_HIDE)
	If $proceed Then
		_ID3ReadTag($mp3s[$title[$i][1]])
		_ID3SetTagField("TIT2", $t1)
		_ID3SetTagField("Title", $t1)
		_ID3SetTagField("TPE1", $t2)
		_ID3SetTagField("Artist", $t2)
		_ID3WriteTag($mp3s[$title[$i][1]])
	EndIf
Next



MsgBox(32, "Music Tagger", "All files reviewed and tagged!" & @CRLF & "Thank you for using our software!")
Exit











Func search($pfad)
	$folders = _FileListToArray($pfad, "*", 2)

	$files = _FileListToArray($pfad, "*.mp3")
	If IsArray($files) Then
		For $i = 1 To $files[0]
			$mp3s[0] += 1
			ReDim $mp3s[$mp3s[0] + 1]
			$mp3s[$mp3s[0]] = $pfad & "\" & $files[$i]
			ProgressSet(0, $files[$i])
		Next
	EndIf
	If IsArray($folders) Then
		For $i = 1 To $folders[0]

			search($pfad & "\" & $folders[$i])
		Next
	EndIf
EndFunc   ;==>search
