Ever downloaded an archive of files and all of them had file names about 200 characters wrong with the same string in each? It’s more common that you might think… If you look at any audio archives you may have acquired, the chances are that the file names will have the quality in it (192 or 128 etc..). Additionally if you have video files the resolution or quality might be present.
I, for one, don’t really care what the quality is in the file name and would prefer it to be readable. I spend a lot of time removing this extraneous data so decided to make a short PHP script to remove certain strings on mass.
See the following code which can be run from the command line using the following format:
php -f <filename> <text_to_remove> [int live_mode=0]
Code
#!/usr/bin/php
[int live_mode=0]';
exit;
} else {
$remove = $argv[1];
}
$live_mode = (int)$argv[2];
if ($dir = scandir($cwd)) {
foreach ($dir as $file) {
$path = $cwd . '/' . $file;
if (!in_array($file, $ignore)) {
if (!is_link($path) && !is_dir($path)) {
if (strpos($file, $remove) !== false && $file != $_SERVER['SCRIPT_FILENAME'] && $file != $remove) {
$output .= 'Renamed "' . $path . '" to "' . $cwd . '/' . trim(str_replace($remove, '', $file)) . '"' . "\n";
if ($live_mode) {
$new_path = $cwd . '/' . trim(str_replace($remove, '', $file));
rename($path, $new_path);
}
}
}
}
}
if ($output) {
$output = ($live_mode ? 'LIVE':'TEST') . ' MODE' . "\n\n" . 'Text to remove from filenames was "' . $remove . '"' . "\n\n" . $output;
} else {
$output = 'There were no matches by your criteria: "' . $remove . '"';
}
echo $output . "\n\n";
}
?>
Download
To download the code in file form click the following link: File Rename Script (783 bytes)
