Created
October 26, 2013 14:00
-
-
Save shahril96/7169790 to your computer and use it in GitHub Desktop.
Rename multiple files using regex match and string replacement.
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 | |
echo "\n Bulk File Renamer @ Shahril"; | |
$help_text = " | |
\r {$argv[0]} --dir C:\Music --pattern \"/\d{2} \-/\" --rename \"Change\" | |
\r Options :- | |
\r --dir <directory> : Directory of bulk file. | |
\r --pattern <regex> : Regex pattern to match. | |
\r --rename <name> : String to replace the pattern. | |
"; | |
if(in_array("--dir", $argv, true)){ | |
$key = array_search("--dir", $argv); | |
$value = $key + 1; | |
$dir = realpath($argv[$value]); | |
}else{ | |
echo $help_text; | |
die(); | |
} | |
if(in_array("--pattern", $argv, true)){ | |
$key = array_search("--pattern", $argv); | |
$value = $key + 1; | |
$pattern = $argv[$value]; | |
}else{ | |
echo $help_text; | |
die(); | |
} | |
if(in_array("--rename", $argv, true)){ | |
$key = array_search("--rename", $argv); | |
$value = $key + 1; | |
$rename = $argv[$value]; | |
}else{ | |
echo $help_text; | |
die(); | |
} | |
if(!file_exists($dir)) die("\n\n Directory not found or incorrect input!\n"); | |
$files = array_diff(scandir($dir), array('..', '.')); | |
if(empty($files)) die("\n\n No file exist in that directory!\n"); | |
chdir($dir); | |
foreach($files as $file){ | |
$changed = preg_replace($pattern, $rename, $file); | |
if(!rename($file, $changed)) die("\n File '{$file}' has some problem!\n"); | |
} | |
echo "\n\n All operations done!\n"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment