Created
May 6, 2017 03:23
-
-
Save quydm/4b81a14c2364f46380142c002326d871 to your computer and use it in GitHub Desktop.
Rename songs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* I have many .mp3 files with naming rule is: <song's name> - <singer's name> [MP3 320kbps].mp3. Ex: Dao Nguyen - NSUT Thuy Huong [MP3 320kbps].mp3 | |
* I want to rename them using new naming rule is: <song's name>.mp3. Ex: Dao_Nguyen.mp3 | |
*/ | |
date_default_timezone_set('Asia/Ho_Chi_Minh'); | |
$dir = "directory"; | |
if (!is_dir($dir)) | |
exit($dir . "is not a directory" . PHP_EOL); | |
$files = scandir($dir); | |
foreach ($files as $file) { | |
$file_path = $dir . $file; | |
if (is_file($file_path)) { | |
$pieces = explode("-", $file); | |
$new_name = str_replace(" ", "_", trim($pieces[0])); | |
rename($file_path, $dir . $new_name . ".mp3"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment