Skip to content

Instantly share code, notes, and snippets.

@eFrane
Created October 21, 2011 15:59
Show Gist options
  • Save eFrane/1304202 to your computer and use it in GitHub Desktop.
Save eFrane/1304202 to your computer and use it in GitHub Desktop.
Convert iTunes Music Library.xml path locations
#!/usr/bin/php
<?php
/**
Convert iTunes Music Library.xml path locations
usage: convert.php library.xml old_library_path new_library_path
converted library is written to stdout
old and new library paths have to include trailing slashes and
lead to the corresponding iTunes Media directories
works for Windows and Mac iTunes Libraries
To reload the iTunes Media Library from the converted XML:
http://support.apple.com/kb/ht1451
**/
$newlib = $argv[2];
$oldlib = $argv[3];
$lib = file_get_contents($argv[1]);
$match1 = '/(Music Folder<\/key><string>)([[:print:]]+)(<\/string>)/';
$lib = preg_replace($match1, '$1'.$path.'$3', $lib);
$oldlib = str_replace(array('[', '(', ')', ']', '.', '/'), array('\[', '\(', '\)', '\]', '\.', '\/'), $oldlib);
$match2 = '/(Location<\/key><string>)('.$oldlib.')([[:print:]]+)(<\/string>)/';
$lib = preg_replace($match2, '$1'.$path.'$3$4', $lib);
print $lib;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment