Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # This will read all the files of a given extention in a directory
- # Create a subfolder based on the album id3 tag
- # move all the files into the appropriate album sub folder
- #
- # This doesn't work on filenames that have square brackets, they will be skipped
- # Files without album id3 tags will be left unmodified
- #
- # requires:
- # taglib-sharp.dll found at http://download.banshee.fm/taglib-sharp/
- #
- #folderToSearch is the folder that has the files
- $folderToSearch="E:\music\"
- #taglibLocation is the location of the taglib-sharp.dll
- $taglibLocation="C:\Program Files (x86)\taglib-sharp-2.1.0.0-windows\Libraries"
- #fileExtension is the file extension of the files with the ID3 tags (eg: mp3, wma, etc)
- $fileExtension="mp3"
- # ---------------------------------------------------------------------------#
- #Load the taglib-sharp.dll
- try
- {
- #check to ensure that taglib-sharp.dll exists
- Test-Path -path $taglibLocation\taglib-sharp.dll
- }
- catch
- {
- #ERROR taglib-sharp.dll doesn't exist
- write-host "`n`nAn error has occurred:`n`n" -ForegroundColor Red
- write-host "$taglibLocation\taglib-sharp.dll doesn't exist, or can't be read`n`n`n"
- break
- }
- try
- {
- #load taglib-sharp.dll
- [system.reflection.assembly]::loadfile("$taglibLocation\taglib-sharp.dll")
- }
- catch
- {
- #ERROR taglib-sharp.dll can't be loaded
- write-host "`n`nAn error has occurred:`n`n" -ForegroundColor Red
- write-host "taglib-sharp.dll can't be loaded`n`n`n"
- break
- }
- #get a list of all the files in the directory
- try
- {
- #check to ensure that the folder exists, and contains correct files
- Test-Path -path $folderToSearch\* -include *.$fileExtension
- }
- catch
- {
- #ERROR folder doesn't exist, or doesn't contain the correct files
- write-host "`n`nAn error has occurred:`n`n" -ForegroundColor Red
- write-host "$folderToSearch doesn't exist, can't be read, or no $fileExtension files can be found`n`n`n"
- break
- }
- $filelist=Get-ChildItem $folderToSearch *.$fileExtension
- $filelist | forEach {
- #open the file for reading
- $media=[taglib.file]::create($_.fullname)
- write-host Now processing $_.name from album $media.tag.Album
- #Set the directory to the album id3 tag
- $directory=$media.tag.Album
- #check to see if directory already exists, if not then create it
- if(!(test-path -path $folderToSearch$directory))
- {
- write-host "Creating directory $folderToSearch$directory"
- try
- {
- new-item -itemtype directory -path $folderToSearch$directory
- }
- catch
- {
- #ERROR unable to create folder
- write-host "`n`nAn error has occurred:`n`n" -ForegroundColor Red
- write-host "Failed to create folder $foldertoSearch$directory`n`n`n"
- exit
- }
- }
- #move the file
- try
- {
- move-item -path $folderToSearch\$_ -Destination $folderToSearch$directory\$_
- }
- catch
- {
- #ERROR unable to move file
- write-host "`n`nAn error has occurred:`n`n" -ForegroundColor Red
- write-host "Failed to move $folderToSearch\$_ to $folderToSearch$directory\$_`n`n`n"
- write-host "Continuing...`n"
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement