anonit

Sorting mp3 files based on Album ID3 Tags in Powershell

Feb 7th, 2014
611
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # This will read all the files of a given extention in a directory
  2. # Create a subfolder based on the album id3 tag
  3. # move all the files into the appropriate album sub folder
  4. #
  5. # This doesn't work on filenames that have square brackets, they will be skipped
  6. # Files without album id3 tags will be left unmodified
  7. #
  8. # requires:
  9. # taglib-sharp.dll found at http://download.banshee.fm/taglib-sharp/
  10. #
  11.  
  12. #folderToSearch is the folder that has the files
  13. $folderToSearch="E:\music\"
  14. #taglibLocation is the location of the taglib-sharp.dll
  15. $taglibLocation="C:\Program Files (x86)\taglib-sharp-2.1.0.0-windows\Libraries"
  16. #fileExtension is the file extension of the files with the ID3 tags (eg: mp3, wma, etc)
  17. $fileExtension="mp3"
  18.  
  19. # ---------------------------------------------------------------------------#
  20.  
  21. #Load the taglib-sharp.dll
  22. try
  23. {
  24.     #check to ensure that taglib-sharp.dll exists
  25.     Test-Path -path $taglibLocation\taglib-sharp.dll
  26.    
  27. }
  28. catch
  29. {
  30.      #ERROR taglib-sharp.dll doesn't exist
  31.      write-host "`n`nAn error has occurred:`n`n" -ForegroundColor Red
  32.      write-host "$taglibLocation\taglib-sharp.dll doesn't exist, or can't be read`n`n`n"
  33.      break
  34. }
  35.  
  36. try
  37. {
  38.     #load taglib-sharp.dll
  39.     [system.reflection.assembly]::loadfile("$taglibLocation\taglib-sharp.dll")
  40. }
  41. catch
  42. {
  43.     #ERROR taglib-sharp.dll can't be loaded
  44.     write-host "`n`nAn error has occurred:`n`n" -ForegroundColor Red
  45.     write-host "taglib-sharp.dll can't be loaded`n`n`n"
  46.     break
  47. }
  48.  
  49.  
  50. #get a list of all the files in the directory
  51. try
  52. {
  53.     #check to ensure that the folder exists, and contains correct files
  54.     Test-Path -path $folderToSearch\* -include *.$fileExtension
  55. }
  56. catch
  57. {
  58.     #ERROR folder doesn't exist, or doesn't contain the correct files
  59.     write-host "`n`nAn error has occurred:`n`n" -ForegroundColor Red
  60.     write-host "$folderToSearch doesn't exist, can't be read, or no $fileExtension files can be found`n`n`n"
  61.     break
  62. }
  63.  
  64. $filelist=Get-ChildItem $folderToSearch *.$fileExtension
  65. $filelist | forEach {
  66.     #open the file for reading
  67.     $media=[taglib.file]::create($_.fullname)
  68.     write-host Now processing $_.name from album $media.tag.Album
  69.     #Set the directory to the album id3 tag
  70.     $directory=$media.tag.Album
  71.     #check to see if directory already exists, if not then create it
  72.     if(!(test-path -path $folderToSearch$directory))
  73.     {
  74.         write-host "Creating directory $folderToSearch$directory"
  75.         try
  76.         {
  77.             new-item -itemtype directory -path $folderToSearch$directory
  78.         }
  79.         catch
  80.         {
  81.             #ERROR unable to create folder
  82.             write-host "`n`nAn error has occurred:`n`n" -ForegroundColor Red
  83.             write-host "Failed to create folder $foldertoSearch$directory`n`n`n"
  84.             exit
  85.         }
  86.     }
  87.     #move the file
  88.     try
  89.     {
  90.         move-item -path $folderToSearch\$_ -Destination $folderToSearch$directory\$_
  91.     }
  92.     catch
  93.     {
  94.         #ERROR unable to move file
  95.         write-host "`n`nAn error has occurred:`n`n" -ForegroundColor Red
  96.         write-host "Failed to move $folderToSearch\$_ to $folderToSearch$directory\$_`n`n`n"
  97.         write-host "Continuing...`n"
  98.     }
  99. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×