我的文件名看起来像DDStub-JON DOE-10-08-22-2017.pdf
。如何删除日期以外的所有内容,以使其递归地看起来像08-22-2017.pdf
?我在大约100个文件夹中有大约24个文件。
目录结构如下
DDSTUBS
|-- 23
|-- 45
|-- 65
| |-- DDStub-JON DOE-10-08-22-2017.pdf
| |-- DDStub-JON DOE-08-08-22-2017.pdf
| |-- DDStub-JON DOE-07-08-22-2017.pdf
答案 0 :(得分:1)
递归文件重命名
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(__DIR__));
foreach ($iterator as $file) {
if ($file->isDir()) continue;
$path = $file->getPath();
$fileName = $file->getFilename();
if($file->getExtension() == 'pdf'){
$newFilename = substr($fileName, -14);
rename($path.'/'.$fileName, $path.'/'.$newFilename);
}
}
重命名单个目录文件
在每个文件夹中新建一个文件,然后复制以下代码!
代码
<?php
//Checking directory for files
$files = scandir(__DIR__);
foreach ($files as $file){
//Getting file extension, so it won't affect other files.
$ext = pathinfo($file, PATHINFO_EXTENSION);
if($ext == 'pdf'){
//getting -17 string from end of file name including file extension.
$newName = substr($file, -14);
//renaming file
rename($file, $newName);
}
}
?>
答案 1 :(得分:0)
<?php
$files = glob('your/path/../**/*.pdf'); // recursive
foreach( $files as $file ){
rename( $file, substr($file, -14) )
}