通过批量用零填充重命名顺序文件 [英] Batch rename sequential files by padding with zeroes

查看:134
本文介绍了通过批量用零填充重命名顺序文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆名为像这样的文件:

  output_1.png
output_2.png
...
output_10.png
...
output_120.png

什么是重命名这些匹配公约的最简单的方法,例如与最多四个小数,使得文件被命名为:

  output_0001.png
output_0002.png
...
output_0010.png
output_0120.png

这应该是很容易的在Unix / Linux / BSD,虽然我也有机会到Windows。任何语言是好的,但我感兴趣的是一些非常整洁的单行(如果有的话?)。


解决方案

的Python

 导入OS
路径='/路径/要/文​​件/
在os.listdir(路径)文件名:
    preFIX,NUM =文件名[: - 4] .split(_)
    NUM = num.zfill(4)
    new_filename = preFIX +_+ NUM +巴纽
    os.rename(os.path.join(路径,文件名),os.path.join(路径,new_filename))

您可以编译有效的文件名假定以output_开始,以的.png结尾的所有文件都有效的文件清单:

  L = [(X,输出+ X [7:-4] .zfill(4)+巴纽)在os.listdir(路径)x如果x.startswith(output_)和x.endswith(PNG)]对于使用oldName,NEWNAME在L:
    os.rename(os.path.join(路径,使用oldName),os.path.join(路径,NEWNAME))


猛砸

(来源: http://www.walkingrandomly.com/?p=2850

在换句话说,我替换file001.png file1.png和file020.png file20.png等。以下是如何做到这一点在bash

 #!/斌/庆典
NUM =`expr的比赛$ 1'[^ 0-9] * \\([0-9] \\ + \\)。*`
paddednum =`printf的%03D$ num`
回声$ {1 / $ NUM / $ paddednum}

以上保存到一个名为 zeropad.sh 文件,然后执行以下命令来使其可执行

 搭配chmod + X ./zeropad.sh

您可以再使用 zeropad.sh 脚本如下:

  ./ zeropad.sh frame1.png

这将返回的结果。

  frame001.png

剩下的工作就是使用这个脚本的所有.png文件重命名的当前目录中,使得它们zeropadded。

 为我在*。PNG;做MV $`我$ ./zeropad.sh i`; DONE


的Perl

(来源:零垫命名如图片(2).JPG - >图像(002).JPG

 使用严格的;
使用警告;
使用文件::查找;子pad_left {
   我的$ NUM =转变;   如果($ NUM小于10){
      $ NUM =00 $ NUM
   }
   ELSIF($ NUM< 100){
      $ NUM =0 $ NUM
   }   返回$ num个;
}子NEW_NAME {
   如果(/\\.jpg$/){
      我的名字$ = $文件::查找::名称;
      我的$ NEW_NAME;
      ($ NEW_NAME = $名)=〜S / ^(。+ \\ / [\\ W] + \\()(\\ d +)\\)/ $ 1 &安培; pad_left($ 2)')'/ E。
      重命名($名称,$ NEW_NAME);
      打印$名字 - > $名称\\ N的;
   }
}格格(我的$ LOCALDIR =`pwd`);#调用脚本中的父目录
                            含图片子目录#找到(\\&安培; NEW_NAME,$ LOCALDIR);


重命名

另外从上面的回答:

 重命名的/ \\ D + / sprintf的(%04D,$&安培;)/ E'*。PNG

I have a bunch of files named like so:

output_1.png
output_2.png
...
output_10.png
...
output_120.png

What is the easiest way of renaming those to match a convention, e.g. with maximum four decimals, so that the files are named:

output_0001.png
output_0002.png
...
output_0010.png
output_0120.png

This should be easy in Unix/Linux/BSD, although I also have access to Windows. Any language is fine, but I'm interested in some really neat one-liners (if there are any?).

解决方案

Python

import os
path = '/path/to/files/'
for filename in os.listdir(path):
    prefix, num = filename[:-4].split('_')
    num = num.zfill(4)
    new_filename = prefix + "_" + num + ".png"
    os.rename(os.path.join(path, filename), os.path.join(path, new_filename))

you could compile a list of valid filenames assuming that all files that start with "output_" and end with ".png" are valid files:

l = [(x, "output" + x[7:-4].zfill(4) + ".png") for x in os.listdir(path) if x.startswith("output_") and x.endswith(".png")]

for oldname, newname in l:
    os.rename(os.path.join(path,oldname), os.path.join(path,newname))


Bash

(from: http://www.walkingrandomly.com/?p=2850)

In other words I replace file1.png with file001.png and file20.png with file020.png and so on. Here’s how to do that in bash

#!/bin/bash
num=`expr match "$1" '[^0-9]*\([0-9]\+\).*'`
paddednum=`printf "%03d" $num`
echo ${1/$num/$paddednum}

Save the above to a file called zeropad.sh and then do the following command to make it executable

chmod +x ./zeropad.sh

You can then use the zeropad.sh script as follows

./zeropad.sh frame1.png

which will return the result

frame001.png

All that remains is to use this script to rename all of the .png files in the current directory such that they are zeropadded.

for i in *.png;do mv $i `./zeropad.sh $i`; done


Perl

(from: Zero pad rename e.g. Image (2).jpg -> Image (002).jpg)

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);


Rename

Also from above answer:

rename 's/\d+/sprintf("%04d",$&)/e' *.png

这篇关于通过批量用零填充重命名顺序文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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