Skip to content

Instantly share code, notes, and snippets.

@zhaofengli
Created March 29, 2015 06:08
Show Gist options
  • Save zhaofengli/4374225542fb3d340629 to your computer and use it in GitHub Desktop.
Save zhaofengli/4374225542fb3d340629 to your computer and use it in GitHub Desktop.
Moves MP3 files downloaded via Baidu Music
<?php
// Moves MP3 files downloaded via Baidu Music
// By Zhaofeng Li - Public domain
$pattern = "/(?'artist'[^-]+)-(?'album'[^-]+)-(?'title'[^-]+)-/";
$dir = "/sdcard/Baidu_music/download";
$dest = "/sdcard/Music";
$files = scandir( $dir );
foreach ( $files as $file ) {
if ( preg_match( $pattern, $file, $m ) ) {
if ( strpos( $m['artist'], "," ) ) continue;
$destdir = $dest . "/{$m['artist']}/{$m['album']}";
if ( !is_dir( $destdir ) ) {
echo "Creating $destdir\n";
mkdir( $destdir, 0777, true );
}
$target = $destdir . "/{$m['title']}.mp3";
rename( "$dir/$file", $target );
echo "Moved $dir/$file to $target\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment