今天早上整理素材因此需要一个批量重命名的脚本,本想用 shell,原想在网上搜个现成的就可以了,但是多不太合适,忽然想到 PHP 也是可以在终端下执行的,于是匆匆写了这个东西
-
#!/usr/bin/php
-
<?php
-
function bat($directory){
-
$dir = dir($directory);
-
$i=0;
-
while($file = $dir->read())
-
{
-
if((is_dir(“$directory/$file”)) && (($file!=”.”) || ($file!=”..”)))
-
{
-
echo “$file\n”;
-
}else{
-
$fileName = (string) $file;
-
$deFile = explode(‘.’, $fileName);
-
$count=count($deFile);
-
$fileExt = $deFile[$count-1];
-
$baseName =””;
-
for($j=0;$j<$count-1;$j++){
-
$baseName .=$deFile[$j];
-
}
-
echo “$baseName\t文件类型:$fileExt\n”;
-
if(isset($argv)){
-
$newName = $argv[1].$i.’.’.$fileExt;
-
}else{
-
$newName = $i.’.’.$fileExt;
-
}
-
if($fileExt!=’php’&&$fileExt!=’DS_Store’){
-
rename($fileName , $newName );
-
}
-
}
-
$i++;
-
}
-
$dir->close();
-
}
-
bat(“./”);
因为急于其它事情未能完善,仅作记录或供参考,还有些问题,脚本执行后会莫名少几个文件,有时间再来研究,
ps:如果是很重要的文件,请不要执行哦,莫谓言之不预哦~~
转载请注明:XAMPP中文组官网 » PHP 代替 shell 批量修改文件名