PHP batch file rename script (supports regular expressions)

藏色散人
Release: 2023-04-11 13:22:02
forward
1909 people have browsed it

This article brings you relevant knowledge about PHP. It mainly introduces how to use PHP to implement batch file renaming scripts. Friends who are interested can take a look below. I hope it will be helpful to everyone.

PHP batch file rename script (supports regular expressions)

PHP batch file renaming script (supports regular expressions)

As mentioned.

[Disclaimer] Please understand how to use it and use it with caution. Any consequences caused by it have nothing to do with this program and its author~

[Instructions for use]

Format: rename from to path from what file name to is renamed from and which folder path is (path can be omitted, the default is the current folder)

[For example]

rename “(\d )(.*).jpg” “{1}.jpg” {1} represents the first capturing group of the regular expression, {2}, {3} and so on

rename “(\d ).jpg” “{i}.jpg” {i} means starting from 1 and increasing each file by 1, {i10} means starting from 10 and increasing each file by 1, {i10 2} means starting from 10 Start adding 2, {i10-2} means subtracting 2 from 10

[In addition]

--rsort means the file names are processed in reverse order, the default is forward order--debug means debugging, You can print out a list of which files will be renamed to what name --help is to display the help document

[Others]

By default, the renamed files are in the renamed directory under the path directory. The original file will not be renamed, please make sure it is correct and copy it carefully (if you need to rename the source file, please add the --force parameter. Please use this parameter with caution. When an error occurs, the file will be overwritten and cannot be recovered. Therefore, it is not recommended to use unless it has been strictly tested to ensure safety)

[The code is as follows]

#!/usr/bin/php
<?php
//【免责声明】请了解怎么使用和正则的情况下谨慎使用,由此造成的任何后果均与本程序及其作者无关
//PHP批量文件重命名脚本(支持正则)
// 格式: rename from to path from是从什么文件名 to是重命名什么文件名 path是哪个文件夹(path可省略,默认为当前文件夹)
// 比如: rename "(\d+)(.*)\.jpg" "{1}.jpg" {1}代表正则的第一个捕获组,{2},{3}以此类推
//       rename "(\d+).jpg" "{i}.jpg" {i}代表从1开始每个文件增加1,{i10}代表从10开始每个文件加1,{i10+2}代表从10开始加2,{i10-2}代表从10开始减2
// 另外: --rsort代表文件名按倒序处理,默认为正序 --debug代表调试,可以打印出哪个文件将会重命名为什么名称的列表 --help是显示帮助文档
// 默认重命名后的文件在path目录下的renamed目录里,不会重命名原文件,请确认无误后再拷贝(如需重命名在源文件上,请加--force参数,该参数请谨慎使用,当出现错误时,文件会被覆盖掉且无法恢复,因此非经过严格测试,确保安全情况下不建议使用)
if(in_array(&#39;--help&#39;, $argv)) {
    //显示help菜单
    help();
    exit();
}
//开启调试模式
$debug = 0;
if(in_array(&#39;--debug&#39;, $argv)) {
    unset($argv[array_search("--debug",$argv,true)]);
    $debug = 1;
}
//开启覆盖式重命名,默认是拷贝式重命名
$forceCover = 0;
if(in_array(&#39;--force&#39;, $argv)) {
    unset($argv[array_search("--force",$argv,true)]);
    $forceCover = 1;
    //让用户确认是否使用--force参数,防止误操作
    fwrite(STDOUT,&#39;您正在使用--force参数,该参数可能导致文件被覆盖且无法恢复,您确认使用吗?yes使用,no不使用:&#39;);
    $confirm = strtolower(trim(fgets(STDIN)));
    if($confirm != &#39;yes&#39; && $confirm != &#39;y&#39;) {
        //不使用
        $forceCover = 0;
    }
}
//--rsort把文件按自然排序倒序,默认是按自然排序正序
$sortType = &#39;sort&#39;;
if(in_array(&#39;--rsort&#39;, $argv)) {
    unset($argv[array_search("--rsort",$argv,true)]);
    $sortType = &#39;rsort&#39;;
}
//获取命令行参数,建议加“”防止字符被shell解析
$argv = array_values($argv);
$from = $argv[1]??&#39;&#39;;
if($from == "*" || $from == "*.*"){
    $from = ".*";
}
$to = $argv[2]??&#39;&#39;;
//path默认为当前目录
$path = $argv[3]??&#39;&#39;;
$path = $path ? rtrim($path, &#39;/&#39;) . &#39;/&#39; : &#39;&#39;;
if(!$from||!$to){
    //参数错误报错并显示帮助
    echo "[error] from and to is required.\n";
    help();
    exit();
}
//遍历所有文件并按自然排序
$files = glob("{$path}*");
if($sortType == &#39;rsort&#39;) {
    rsort($files, SORT_NATURAL);
} else {
    sort($files, SORT_NATURAL);
}
//匹配$to中{i10+1}字符串
preg_match("#\{(?<i>[iI])(?<init>(\d+){0,1})(?<op>[+-]{0,1})(?<step>(\d+){0,1})\}#", $to, $toMatches);
//设置初始值
$init = !empty($toMatches[&#39;init&#39;]) ? intval($toMatches[&#39;init&#39;]) : 1;
$count = $init ?: 1;
//遍历所有文件名进行替换处理
foreach ($files as $filename) {
    $tofilename = $to;
    //替换$to中{i10+1}字符串
    if(!empty($toMatches)) {
        $tofilename = str_replace($toMatches[0], $count, $tofilename);
    }
    //替换每个文件名的捕获组(即$from正则中的捕获组)
    preg_match("#{$from}#i", $filename, $fromMatches);
    if(!empty($fromMatches)) {
        foreach($fromMatches as $key => $val) {
            //跳过第一个,第一个不是捕获组
            if($key > 0) {
                $tofilename = str_replace("{{$key}}", $val, $tofilename);
            }
        }
    }
    // 根据$to中{i10+1}字符串判断操作方向
    if(!empty($toMatches)) {
        $step = !empty($toMatches[&#39;step&#39;]) ? intval($toMatches[&#39;step&#39;]) : 1;
        $op = !empty($toMatches[&#39;op&#39;]) ? trim($toMatches[&#39;op&#39;]) : "+";
        if($op == "+"){
            $count = $count + $step;
        } else {
            $count = $count - $step;
        }
    }
    //调试时只显示不实际写入
    if($debug) {
        $tofilename = $path.$tofilename;
        echo "$filename => $tofilename\n";
        continue;
    }
    //写入重名名后的文件
    if($forceCover){
        //覆盖式重命名
        rename($filename, $path.$tofilename);
    } else {
        //拷贝式重命名
        if(!file_exists($path.&#39;renamed/&#39;)){
            mkdir($path.&#39;renamed/&#39;,0777,true);
        }
        copy($filename, $path.&#39;renamed/&#39;.$tofilename);
    }
}
function help() {
    echo "帮助文档:\n";
    echo "格式:rename from to path from是从什么文件名 to是重命名什么文件名 path是哪个文件夹(path可省略,默认为当前文件夹)\n";
    echo "列如:rename \"(\d+)(.*)\.jpg\" \"{1}.jpg\" {1}代表正则的第一个捕获组,{2},{3}以此类推\n";
    echo "列如:rename \"(\d+).jpg\" \"{i}.jpg\" {i}代表从1开始每个文件增加1,{i10}代表从10开始每个文件加1,{i10+2}代表从10开始加2,{i10-2}代表从10开始减2\n";
    echo "另外: --rsort代表文件名按倒序处理,默认为正序 --debug代表调试,可以打印出哪个文件将会重命名为什么名称的列表 --help是显示帮助文档\n";
    echo "默认重命名后的文件在path目录下的renamed目录里,不会重命名原文件,请确认无误后再拷贝(如需重命名在源文件上,请加--force参数,该参数请谨慎使用,当出现错误时,文件会被覆盖掉且无法恢复,因此非经过严格测试,确保安全情况下不建议使用)\n";
}
Copy after login

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of PHP batch file rename script (supports regular expressions). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!