php怎么重命名文件,PHP:如何重命名文件夹

此代码检索名称以“v”开头的所有目录,后跟数字.

目录已过滤:v1,v2,v3,…..

排除目录:v_1,v2_1,v3a,t1,.,..,xyz

最终目录:v0,v1,v2,v3,…..

如果最终目录需要从v1开始,那么我将再次获取目录列表并再执行一次重命名过程.我希望这有帮助!

$path='main_folder/'; $handle=opendir($path); $i = 1; $j = 0; $foldersStartingWithV = array();

// Folder names starts with v followed by numbers only

// We exclude folders like v5_2, t2, v6a, etc

$pattern = "/^v(\d+?)?$/";

while (($file = readdir($handle))!==false){

preg_match($pattern, $file, $matches, PREG_OFFSET_CAPTURE);

if(count($matches)) {

// store filtered file names into an array

array_push($foldersStartingWithV, $file);

}

}

// Natural order sort the existing folder names

natsort($foldersStartingWithV);

// Loop the existing folder names and rename them to new serialized order

foreach($foldersStartingWithV as $key=>$val) {

// When old folder names equals new folder name, then skip renaming

if($val != "v".$j) {

rename($path.$val, $path."v".$j);

}

$j++;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值