<?php
declare(strict_types = 1);
const PATTERN = '/(-m\d-[a-zA-Z]*-[0-9][0-9])/';
if (empty($argv[1])) {
throw new Exception('Usage: php rename.php <directory_path>');
}
$path = str_replace('//', '/', $argv[1] . '/');
$newFiles = [];
foreach (scandir($path) as $fileName) {
if ('.' === $fileName || '..' === $fileName) {
continue;
}
preg_match(PATTERN, $fileName, $matches);
$matched = reset($matches);
if (is_bool($matched)) {
throw new Exception(sprintf('File name "%s" can not be matched', $fileName));
}
$newName = substr($matched, 1, strlen($matched)) . '_' . preg_replace(PATTERN, '', $fileName);
rename($path.$fileName, $path.$newName);
}
Last active
May 2, 2024 02:28
-
-
Save vikbert/d6004c154ab5b5c23540d72f02631714 to your computer and use it in GitHub Desktop.
[scripting] small scripting snippets #php #scripting
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment