技術めも

Tools

数字ファイル名 一括桁あわせ

<?php
// 数字ファイル名 一括桁あわせ

if (empty($argv[1])) {
    echo '[used] php keta.php {path}'.PHP_EOL;
    exit;
}

$fileList = glob($argv[1].'/*.*');
foreach ($fileList as $filePath) {
    $info = pathinfo($filePath);
    // rename
    if (preg_match('@^[0-9]+$@', $info['filename'])) {
        $newName = sprintf('%03d', $info['filename']);
        $newFilePath = $info['dirname'].'/'.$newName.'.'.$info['extension'];
        echo $filePath.'  ->  '.$newFilePath.PHP_EOL;
        rename($filePath, $newFilePath);
    }
}