Jump to content

Read Artist Name and Album and Create Folder And Move


Recommended Posts

Posted

I have a folder full of tracks. and im after a script that will create the folder by reading the artist and album name. and then move the tracks in to that folder.

 

Any Help?

 

Thanks

Posted

In Powershell we can grab the extended file attributes, at least from mp3 :

 

function Add-FileDetails { 
param( 
[Parameter(ValueFromPipeline=$true)] 
$fileobject, 
$hash = @{Artists = 13; Album = 14; Year = 15; Genre = 16; Title = 21; Length = 27; Bitrate = 28} 
) 
begin {  
 $shell = New-Object -COMObject Shell.Application  


} 
process { 
 if ($_.PSIsContainer -eq $false) { 
  $folder = Split-Path $fileobject.FullName 
  $file = Split-Path $fileobject.FullName -Leaf 
  $shellfolder = $shell.Namespace($folder) 
  $shellfile = $shellfolder.ParseName($file) 
  #Write-Progress 'Adding Properties' $fileobject.FullName 
  $hash.Keys |  
  ForEach-Object { 
   $property = $_ 
   $value = $shellfolder.GetDetailsOf($shellfile, $hash.$property) 
   if ($value -as [Double]) { $value = [Double]$value } 
   $fileobject | Add-Member NoteProperty "Extended_$property" $value -force 
  } 
 } 
 $fileobject 
} 
} 
#------------------------------------------------------------------------------

 

A typical use of that might be to list some of the attributes :

 

get-childitem $args -ea silentlycontinue | Add-FileDetails | select-object Mode, LastWriteTime, Length, Name, Extended_Bitrate | format-table Mode, LastWriteTime, Length, Name, Extended_Bitrate -auto

 

You probably want to pipe that into a list then process the list to create teh directory and move the files.

  • Thanks 1
Posted

rename. when I say the same format, I mean it can rename to something like [Track Number] - [Artist] [Track Name].extension

 

you can also use it to add any missing meta data and album art.

Posted (edited)

You get to choose what you want Picard/MusicBrainz to do IIRC. It can do any of those options and also fix broken ID3 tags. Also, here's the Linux bash script solution to your problem. :)

 

#!/bin/bash

#Renames and organizes your music data.
#Provide the full path of your source music folder as an argument.
#Renames music files according to its metadata  in format "Track - Title - Album - Artist". 
#Creates a directory in the target folder with Artist name and stores file in it.


if [ -z "$1" ];then #setting default directory
cd ~
else	
cd "$1"
fi

target_dir='~/Music'

for F in ./*.mp3 #change this line to 'for F in ./*' if you have files other than mp3 format
do
if [ -f "$F" ];then
	# Get metadata
	track=`mminfo "$F"|grep track|awk -F: '{print $2}'`
	track=${track:1}	
	artist=`mminfo "$F"|grep artist|awk -F: '{print $2}'`
	artist=${artist:1}
	title=`mminfo "$F"|grep title|awk -F: '{print $2}'`
	title=${title:1}
	album=`mminfo "$F"|grep album|awk -F: '{print $2}'`
	album=${album:1}
		if [  -n "$artist"  ] && [  -n "$title"  ] && [ -n "$album" ];then
			filename="$track - $title - $album - $artist.mp3";echo $filename
			cp "$F" "$filename"  #renaming the file(copy)
				if [ -d "$target_dir/$artist/$album" ];then    #moving file to the artist folder
					mv "$filename" "$target_dir/$artist/$album"  
				else 
					if [ -d "$target_dir/$artist" ];then
						mkdir "$target_dir/$artist/$album"
						mv "$filename" "$target_dir/$artist/$album"
					else
						mkdir "$target_dir/$artist"
						mkdir "$target_dir/$artist/$album"
						mv "$filename" "$target_dir/$artist/$album"
					fi
				fi
		else
			echo -e "Skipping b/c insufficient metadata: $F \n"
		fi
fi
done

Edited by Geoff
Posted

Since I'm in need of a bit of powershell therapy, here's the hacked together script to process a single path (not recursive). Files without the extended attributes populated won't get moved. I need a very similar thing to get rid of all the 128bit rubbish that I foolishly thought would be OK.

 

function Add-FileDetails { 
   param( 
       [Parameter(ValueFromPipeline=$true)] 
       $fileobject, 
       $hash = @{Artists = 13; Album = 14; Year = 15; Genre = 16; Title = 21; Length = 27; Bitrate = 28} 
   ) 

   begin {  
     $shell = New-Object -COMObject Shell.Application            
   } 
   process { 
     if ($_.PSIsContainer -eq $false) { 
      $folder = Split-Path $fileobject.FullName 
      $file = Split-Path $fileobject.FullName -Leaf 
      $shellfolder = $shell.Namespace($folder) 
      $shellfile = $shellfolder.ParseName($file) 
      #Write-Progress 'Adding Properties' $fileobject.FullName 
      $hash.Keys |  
      ForEach-Object { 
       $property = $_ 
       $value = $shellfolder.GetDetailsOf($shellfile, $hash.$property) 
       if ($value -as [Double]) { $value = [Double]$value } 
       $fileobject | Add-Member NoteProperty "Extended_$property" $value -force 
      } 
     } 
     $fileobject 
   } 
} 

clear

$flist = Get-Childitem -Path "F:\test" | Add-FileDetails
foreach ($file in $flist) {    
   $targ_dir = $file.DirectoryName + "\" + $file.Extended_Artists    
   if (!(Test-Path $targ_dir)) {
       New-Item $targ_dir -type directory
   }
   if ( $file.Attributes -ne "Directory" ) {
       Move-Item $file.FullName $targ_dir
   }
} 

  • Thanks 1
  • 7 years later...
Posted (edited)

Sorry for the necro... but I came across this thread (good old EDUGEEK) on a Google search for just such a thing, as I'm downsizing my home PC setup and don't need computers all around the house. Most of the other PS scripts require some sort of module downloading or blogs/forum posts (like this one) that suggest installing some other software (Picard/Media Monkey), but I'd rather not.

 

The above script almost got me there (put them in 'Artist' folders but not 'Album' folders within those), but I'm not a Powershell guy so I'm not exactly sure why...

 

So I'm hoping to consolidate all the various DATA/STORAGE hard drives and moving all my music onto one disk, just copying it all across for now.

 

Eventually I know I'm going to have to go through it and things like the iTunes backup just has a massive folder with thousands of tracks.

 

Most have full ID3 tags (but at the least have Artist & Album and that's all I'm interested in), some have strange track names (#?@! Yourself - Steve Vai) that I can deal with manually I guess.

 

But at the minimum I'd like it to create 'Artist' folders and within that 'Album' folders and have all the tracks moved to the relevant folders.

Edited by Koldov
Posted

Thanks, may have fixed it though... added in the red part on a hunch that is what is creating the folders and it seems to have worked OK.

 

function Add-FileDetails { 
   param( 
       [Parameter(ValueFromPipeline=$true)] 
       $fileobject, 
       $hash = @{Artists = 13; Album = 14; Year = 15; Genre = 16; Title = 21; Length = 27; Bitrate = 28} 
   ) 

   begin {  
     $shell = New-Object -COMObject Shell.Application            
   } 
   process { 
     if ($_.PSIsContainer -eq $false) { 
      $folder = Split-Path $fileobject.FullName 
      $file = Split-Path $fileobject.FullName -Leaf 
      $shellfolder = $shell.Namespace($folder) 
      $shellfile = $shellfolder.ParseName($file) 
      #Write-Progress 'Adding Properties' $fileobject.FullName 
      $hash.Keys |  
      ForEach-Object { 
       $property = $_ 
       $value = $shellfolder.GetDetailsOf($shellfile, $hash.$property) 
       if ($value -as [Double]) { $value = [Double]$value } 
       $fileobject | Add-Member NoteProperty "Extended_$property" $value -force 
      } 
     } 
     $fileobject 
   } 
} 

clear

$flist = Get-Childitem -Path "F:\test" | Add-FileDetails
foreach ($file in $flist) {    
   $targ_dir = $file.DirectoryName + "\" + $file.Extended_Artists [b][color="#FF0000"]+ "\" + $file.Extended_Album[/color][/b]
   if (!(Test-Path $targ_dir)) {
       New-Item $targ_dir -type directory
   }
   if ( $file.Attributes -ne "Directory" ) {
       Move-Item $file.FullName $targ_dir
   }
}

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...