零填充重命名,例如图片(2).jpg->图片(002).jpg [英] Zero pad rename e.g. Image (2).jpg -> Image (002).jpg

查看:51
本文介绍了零填充重命名,例如图片(2).jpg->图片(002).jpg的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要重命名所有图像,如标题在所有子文件夹中所述.我正在考虑使用正则表达式提取括号内的数字,然后将其重命名.到处搜索时,我看到有 rename mmv 之类的工具,但是我无法让它们通过pad重命名jpg.

I need to rename all the images as the title explains in all sub-folders. I'm thinking of extracting with regex the number inside the parenthesis then renaming it. Searching around I saw there are tools like rename and mmv but I couldn't get them to pad-rename the jpgs.

如果您能解决我的问题,我将不胜感激.

I'll appreciate any advise to tackle my problem.

顺便说一句:是Windows操作系统,我有cygwin bash和perl.

BTW: is for Windows and I have cygwin bash and perl.

修改:
经过所有答案的实验后得出的结论.


Conclusions after experimenting with all the answers.

  1. Cygwin 重命名不好,我可以上班的那个不接受正则表达式,但是绝对是一个不错的选择,例如通过运行Linux VM并安装Win SharedFolder.
  2. 您可以使用rename 工具."nofollow">使用 sed 的此shell脚本.
  3. Windows的 pwd 等同于 cd .
  4. Hugmeir使用Perl 5.13+开发了一个很有前途的解决方案,这些版本是dev发行版,但是到您读这篇文章时,可能会很稳定.
  5. pad_left子元素可能不是printf语法的更好替代方法,但它可以工作.
  1. Cygwin rename is not good, the one I could get to work doesn't accept regex, but definitely a nice option, e.g. by running a Linux VM and mounting a Win SharedFolder.
  2. You can build a better rename tool for Cygwin with this shell script using sed.
  3. The Windows equivalent for pwd is simply cd.
  4. Hugmeir posted a promising solution using Perl 5.13+ which are dev releases, but by the time you may be reading this, probably that will be stable.
  5. The pad_left sub may not be a better alternative to printf syntax, but it works.

推荐答案

以下代码已在Linux上进行了测试;我不知道您可能需要修改什么才能使其在您的环境中工作.此外,我没有在.jpg文件的子目录中进行任何搜索.如果您希望我以这种方式修改代码,请告诉我.

The below code was tested on Linux; I don't know what you might have to modify to get it to work in your environment. Furthermore I haven't included any searching of sub-directories for .jpg files; if you'd like me to modify the code in that way, please let me know.

(用于子目录范围的搜索,请进一步查看程序版本)

( for a sub-directories-wide search, please see the program version further down)

use strict;
use warnings;

sub pad_left {
   my $num = shift;

   if ($num < 10) {
      $num = "00$num";
   }
   elsif ($num < 100) {
      $num = "0$num";
   }

   return $num;
}

my @files = glob "*.jpg";

my @padded_names = map {
                          my $name = $_;
                          $name =~ s/^([\w ]+\()(\d+)\)/$1 . &pad_left($2) .')'/e;
                          $name;
                       } @files;

foreach (0..$#files) {
   rename($files[$_], $padded_names[$_]);
   print "$files[$_] --> $padded_names[$_]\n";
}

上面的程序重命名文件并打印以下内容:

The above program renames the files and prints the following:

file(1).jpg --> file(001).jpg
file(2).jpg --> file(002).jpg
file(20).jpg --> file(020).jpg
file(200).jpg --> file(200).jpg

HTH

编辑:这是上述程序的改进版本-它现在还包含了搜索.jpg文件子目录的功能.我知道我的代码不再需要了,因为现在已经给出了解决子目录问题的答案,但是,嘿...:)

Here is an improved version of the above program - it now also incorporates searching of sub-directories for .jpg files. I know my code is no longer required as by now an answer addressing the sub-directory problem has been given, but hey... :)

use strict;
use warnings;
use File::Find;

sub pad_left {
   my $num = shift;

   if ($num < 10) {
      $num = "00$num";
   }
   elsif ($num < 100) {
      $num = "0$num";
   }

   return $num;
}

sub new_name {
   if (/\.jpg$/) {
      my $name = $File::Find::name;
      my $new_name;
      ($new_name = $name) =~ s/^(.+\/[\w ]+\()(\d+)\)/$1 . &pad_left($2) .')'/e;
      rename($name, $new_name);
      print "$name --> $new_name\n";
   }
}

chomp(my $localdir = `pwd`);# invoke the script in the parent-directory of the
                            # image-containing sub-directories

find(\&new_name, $localdir);

这篇关于零填充重命名,例如图片(2).jpg-&gt;图片(002).jpg的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆