summaryrefslogtreecommitdiffhomepage
path: root/musicSorter.rb
blob: 4fe32fdac6a4d6c118cabfbfc67697f01e529d99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/ruby
require 'ffruby'
require 'fileutils'

#Code written by Tradam --> https://github.com/realtradam

#Stores errors when reading tags
errors = Array.new

#All extensions you want considered and moved
fileExtensions = Array["mp3", "m4a", "ogg", "wma"]
for extension in fileExtensions
	for song in Dir.glob("**/*." + extension)
		#Loads the tag of the song
		songData = FFruby::File.new(song)
		
		#Checks if any of the fields are not filled out
		#If they arent, save the song to an error log and skip it
		if songData.title == nil
			errors.push("ERROR, TITLE IS NIL FOR: " + song)
			next
		end
		if songData.genre == nil
			errors.push("ERROR, GENRE IS NIL FOR: " + song)
			next
		end
		if songData.artist == nil
			errors.push("ERROR, ARTIST IS NIL FOR: " + song)
			next
		end
		if songData.album == nil
			errors.push("ERROR, ALBUM IS NIL FOR: " + song)
			next
		end
		if songData.track == nil
			errors.push("ERROR, TRACK NUMBER IS NIL FOR: " + song)
			next
		end
	
		#Loads all the values we will want to use
		#Also removes any slashes in the string, will causes errors when making folders if they arent
		title = songData.title.delete '/'
		genre = songData.genre.delete '/'
		artist = songData.artist.delete '/'
		album = songData.album.delete '/'
		track = songData.track.to_s.split('/')[0]
		
		#Adds a slash to the end, to signify it is a folder and to simplify later code
		songFolderGenre = genre + "/"
		songFolderArtist = artist + "/"
		songFolderAlbum = album + "/"
		
		#Makes tracks sort nicely in file viewers(e.g changes track 3 into 03)
		if track.length == 1
			track = "0" + track
		end
		
		#How we want our songs to be formatted
		songFileName = track + title + "." + extension
		
		#Individually checking each folder if it exists, if not make it
		#Once the folder is confirmed to exist, it will move and rename the song to the correct location
		if Dir.exist?(songFolderGenre)
			if Dir.exist?(songFolderGenre + songFolderArtist)
				if Dir.exist?(songFolderGenre + songFolderArtist + songFolderAlbum)
					if !File.exist?(songFolderGenre + songFolderArtist + songFolderAlbum + songFileName)
						FileUtils.mv song, songFolderGenre + songFolderArtist + songFolderAlbum + songFileName
					end
				else
					Dir.mkdir(songFolderGenre + songFolderArtist + songFolderAlbum)
					FileUtils.mv song, songFolderGenre + songFolderArtist + songFolderAlbum + songFileName
				end
			else
				Dir.mkdir(songFolderGenre + songFolderArtist)
				Dir.mkdir(songFolderGenre + songFolderArtist + songFolderAlbum)
				FileUtils.mv song, songFolderGenre + songFolderArtist + songFolderAlbum + songFileName
			end
		else
			Dir.mkdir(songFolderGenre)
			Dir.mkdir(songFolderGenre + songFolderArtist)
			Dir.mkdir(songFolderGenre + songFolderArtist + songFolderAlbum)
			FileUtils.mv song, songFolderGenre + songFolderArtist + songFolderAlbum + songFileName
		end
	end
end

#Prints out errors to the user, so they know what needs to be fixed
for error in errors
	puts error
end