Fixing iTunes When It Forgets the Hard Disk With All Your Music

Comments

I have all my music on a hard disk, and occasionally iTunes resets the music library location to my home folder - just like that, without asking. So once again I find myself having to fix iTunes, because it can’t find the songs and it shows me the dreaded exclamation marks instead. Here’s what I do to fix it.

Disclaimer: If you follow the instructions on this post, you do so at your own risk. I suggest you backup all the files involved.

Reset the iTunes Library

Open the iTunes music folder in Finder ( it’s in your home folder / Music / iTunes) and drag iTunes Music Library.xml and iTunes Library.itl out on the desktop. This will reset iTunes next time you start it, but don’t start it yet. We’ll be working on the xml file, whilte the itl file is just there for backup.

Change the location of where iTunes keeps the music

Has iTunes changed the location from your hard disk to your home folder? Bastard. In the iTunes preferences > Advanced, click on “change” next to the “iTunes music folder location”, and select your hard disk. I leep “Keep music folder organized” unticked.

Look inside iTunes Liberary to see what the problem is

If your iTunes library is huge, it’s a good idea to use Terminal to look at the first few lines, which you can do easily with

1
head -n100 /Users/ME/Desktop/iTunes\ Music\ Library.xml

(you can just type “head -n100 ” and drag the file onto Terminal to let OS X type out the name for you)

The output will be something along the lines of

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
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Major Version</key><integer>1</integer>
  <key>Minor Version</key><integer>1</integer>
  <key>Date</key><date>2012-08-13T21:09:21Z</date>
  <key>Application Version</key><string>10.6.3</string>
  <key>Features</key><integer>5</integer>
  <key>Music Folder</key><string>file://localhost/Volumes/HD1T/Music/</string>
  <key>Library Persistent ID</key><string>C326B89DCF4457DF</string>
  <key>Tracks</key>
  <dict>
      <key>41557</key>
      <dict>
          <key>Track ID</key><integer>41557</integer>
          <key>Name</key><string>Fifths (Jazzanova 6 Sickth Mix)</string>
          <key>Artist</key><string>Ski</string>
          <key>Album</key><string>Jazzanova: The Remixes, 1997-2000</string>
          <key>Grouping</key><string>where:de</string>
          <key>Genre</key><string>ambient-synth-90s</string>
          <key>Kind</key><string>MPEG audio file</string>
          <key>Size</key><integer>19718194</integer>
          <key>Total Time</key><integer>482925</integer>
          <key>Disc Number</key><integer>2</integer>
          <key>Disc Count</key><integer>2</integer>
          <key>Track Number</key><integer>1</integer>
          <key>Track Count</key><integer>10</integer>
          <key>Year</key><integer>2000</integer>
          <key>Date Modified</key><date>2008-12-05T14:26:06Z</date>
          <key>Date Added</key><date>2012-07-14T07:42:37Z</date>
          <key>Bit Rate</key><integer>320</integer>
          <key>Sample Rate</key><integer>44100</integer>
          <key>Play Count</key><integer>12</integer>
          <key>Play Date</key><integer>3321087305</integer>
          <key>Play Date UTC</key><date>2009-03-28T10:15:05Z</date>
          <key>Rating</key><integer>80</integer>
          <key>Album Rating</key><integer>80</integer>
          <key>Album Rating Computed</key><true/>
          <key>Artwork Count</key><integer>1</integer>
          <key>Persistent ID</key><string>E5B52C3DE807B7DC</string>
          <key>Track Type</key><string>File</string>
          <key>File Type</key><integer>1297106739</integer>
          <key>Location</key><string>file://localhost/Users/ME/Music/iTunes/iTunes%20Media/Music/Ski/Jazzanova_%20The%20Remixes,%201997-2000/Fifths%20(Jazzanova%206%20Sickth%20Mix).mp3</string>

You need to look out for the file:// bit of the xml. Here it is telling me that it is looking into /Users/ME/Music/, i.e. my home folder instead of my external hard disk. Bastard.

Use sed to fix the path and create a new xml file

sed is a nifty unix utility which you can use from Terminal. It can open a file, go through it line by line, change things on each line, and then put the result in another file. At least, that’s how I am using it here.

1
sed 's/file:\/\/localhost\/Users\/ME\/Music\/iTunes\/iTunes%20Media\//file:\/\/localhost\/Volumes\/HD1T\//'  < /Users/ME/Desktop/iTunes\ Music\ Library.xml  > /Users/ME/Music/iTunes/iTunes\ Music\ Library.xml

Here’s a breakdown of what the command does.

sed
calls the sed command - it stands for Stream EDitor
’s/…/…./’
sed will itself need instructions, which are strings hence the ’ (apostrophe). s/a/b/ means “if you find an a, substitue it with b”
file:\/\/localhost\/Users\/ME…
this is the string we are looking for. Note that given that forward slashes are used by sed for s/…/…/, if you need them in the string you have to escape them with backward slashes
file:\/\/localhost\/Volumes\/HD1T\/
the string we are replacing with
< /Users/ME/Desktop/iTunes\ Music\ Library.xml
The less than sign means “use this file as input” - in this case, the xml file I had dragged to the Desktop
> /Users/ME/Music/iTunes/iTunes\ Music\ Library.xml
and this means “create this file as output”. We are asking sed to recreate the xml library file from the one on the desktop, but changing some bits around as it does so

Create a broken iTunes library

Now this is important - if you just start iTunes now, with the new .xml file there and no .itl file there, nothing will happen. iTunes will ignore the xml file, and just reset to an empty library. But if iTunes finds a broken .itl file, it will use the .xml file we just fiddled with to recreate the .itl file. The easiest way to create a broken .itl file is from terminal:

1
touch /Users/ME/Music/iTunes/iTunes\ Library.itl

which will create an empty file - broken enough for iTunes.

Restart iTunes

Now iTunes will finally pay attention to the xml file - it will show a dialog saying ‘Importing ‘iTunes Music Library.xml’…” This may take a while, even though it isn’t actually copying mp3 files around.

Comments