批量重命名文件夹中的文件 - PHP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4993590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:29:34  来源:igfitidea点击:

Bulk Rename Files in a Folder - PHP

php

提问by user580950

I have 1000 images in a Folder, which has SKU# word in all the images. For examples

我在一个文件夹中有 1000 张图像,所有图像中都有 SKU# 字样。举些例子

WV1716BNSKU#.zoom.1.jpg
WV1716BLSKU#.zoom.3.jpg

what i need to do is read all the filenames and rename it to the following

我需要做的是读取所有文件名并将其重命名为以下内​​容

WV1716BN.zoom.1.jpg
WV1716BL.zoom.3.jpg

So remove SKU# from filename, is it possible in PHP to do bulk renaming ?

那么从文件名中删除 SKU#,是否可以在 PHP 中进行批量重命名?

回答by Aston

Yeah, just open the directory and create a loop to access all images and rename them, like:

是的,只需打开目录并创建一个循环来访问所有图像并重命名它们,例如:

<?php

if ($handle = opendir('/path/to/files')) {
    while (false !== ($fileName = readdir($handle))) {
        $newName = str_replace("SKU#","",$fileName);
        rename($fileName, $newName);
    }
    closedir($handle);
}
?>

References:

参考:

http://php.net/manual/en/function.rename.php

http://php.net/manual/en/function.rename.php

http://php.net/manual/en/function.readdir.php

http://php.net/manual/en/function.readdir.php

http://php.net/manual/en/function.str-replace.php

http://php.net/manual/en/function.str-replace.php

回答by Kamil Tom?ík

piece of cake:

小菜一碟:

foreach (array_filter(glob("$dir/WV1716B*.jpg") ,"is_file") as $f)
  rename ($f, str_replace("SKU#", "", $f));

(or $dir/*.jpgif number doesn't matter)

(或者$dir/*.jpg如果数字无关紧要)

回答by ircmaxell

Well, using iterators:

好吧,使用迭代器:

class SKUFilterIterator extends FilterIterator {
    public function accept() {
        if (!parent::current()->isFile()) return false;
        $name = parent::current()->getFilename();
        return strpos($name, 'SKU#') !== false;
    }
}
$it = new SkuFilterIterator(
    new DirectoryIterator('path/to/files')
);

foreach ($it as $file) {
    $newName = str_replace('SKU#', '', $file->getPathname());
    rename($file->getPathname(), $newName);
}

The FilterIterator basically filters out all non-files, and files without the SKU#in them. Then all you do is iterate, declare a new name, and rename the file...

FilterIterator 基本上过滤掉所有非文件,以及其中没有的文件SKU#。然后你要做的就是迭代,声明一个新名称,然后重命名文件......

Or in 5.3+ using the newGlobIterator:

或者在 5.3+ 中使用新的GlobIterator

$it = new GlobIterator('path/to/files/*SKU#*');
foreach ($it as $file) {
    if (!$file->isFile()) continue; //Only rename files
    $newName = str_replace('SKU#', '', $file->getPathname());
    rename($file->getPathname(), $newName);
}

回答by RobertPitt

The steps to completing this is pretty simple:

完成此操作的步骤非常简单:

  • iterate over each file using fopen, readdir
  • for each file parse the file name into segments
  • copy the old file into a new directly called old (sanity reasons)
  • rename the root file top the new name.
  • 使用fopen,迭代每个文件readdir
  • 对于每个文件,将文件名解析为段
  • 将旧文件复制到一个直接称为旧的新文件中(理智原因)
  • 将根文件重命名为新名称。

A small example:

一个小例子:

if ($handle = opendir('/path/to/images'))
{
    /* Create a new directory for sanity reasons*/
    if(is_directory('/path/to/images/backup'))
    {
         mkdir('/path/to/images/backup');
    }

    /*Iterate the files*/
    while (false !== ($file = readdir($handle)))
    {
          if ($file != "." && $file != "..")
          {
               if(!strstr($file,"#SKU"))
               {
                   continue; //Skip as it does not contain #SKU
               }

               copy("/path/to/images/" . $file,"/path/to/images/backup/" . $file);

               /*Remove the #SKU*/
               $newf = str_replace("#SKU","",$file);

               /*Rename the old file accordingly*/
               rename("/path/to/images/" . $file,"/path/to/images/" . $newf);
          }
    }

    /*Close the handle*/
    closedir($handle);
}

回答by a.4j4vv1

You can also use this sample:

您还可以使用此示例:

$directory = 'img';
$gallery = scandir($directory);
$gallery = preg_grep ('/\.jpg$/i', $gallery);
// print_r($gallery);

foreach ($gallery as $k2 => $v2) {
    if (exif_imagetype($directory."/".$v2) == IMAGETYPE_JPEG) {
        rename($directory.'/'.$v2, $directory.'/'.str_replace("#SKU","",$v2));
    }
}