如何按順序批量重新編號文件?

[英]How do I batch renumber files in sequence?


I have a sequence of image files that look like this:

我有一系列圖像文件,如下所示:

image-149454.jpg
image-149455.jpg

I have some other images that I want to append to the end of the sequence where it left off but currently they are numbered from 0 (i.e. image-000000 to image-010000).

我有一些其他的圖像,我想要附加到它停止的序列的末尾,但是目前它們從0開始編號(即圖像-000000到圖像-010000)。

What's a script I could use to rename those new images starting from a certain number and going on, in this case 149456 and onwards?

我可以使用什么腳本重命名這些新圖像從一定數字開始並繼續,在這種情況下149456及以后?

3 个解决方案

#1


0  

I would try something like this. You just have to adjust the offset and add a string in front of the name, just as you need it.

我會嘗試這樣的事情。您只需調整偏移量並在名稱前添加一個字符串,就像您需要的那樣。

x=1
for i in *.jpg; do
    temp=$(printf "%08d.jpg" ${x}) #padding since you seem to want it
    mv ${i} ${temp}
    let x=x+1
done

#2


0  

Here's an untested solution in Python. This should change all of the files in a directory to have sequentially higher numbers than the other.

這是Python中未經測試的解決方案。這應該更改目錄中的所有文件,使其數字順序高於另一個。

To use it enter: script.py dir1 dir2

要使用它,請輸入:script.py dir1 dir2

Assuming the original files have the higher number (149455) are in dir1 and the new files start from 000000 are in dir2:

假設原始文件的數字(149455)在dir1中較高而新文件從000000開始在dir2中:

import os, sys, re

max_image = 0
# check if current (with higher numbers) and other (with lower) directory is given
if len(sys.argv) == 3:
    for files in os.listdir(sys.argv[1]): # first dir is current
        if files.endswith(".jpg"):
            #for all jpgs get the max
            m = re.search("\-(\d+)", files)
            number = int(m.group(1))
            if max_image < number:
                max_image = number

    for files in os.listdir(sys.argv[2]): # second dir is other
        if files.endswith(".jpg"):
            #get current start point
            m = re.search("\-(\d+)", files)
            number = int(m.group(1))
            os.rename(files, "image-" + str(number + max_image)+".jpg") # add the max from current folder

#3


0  

Here's a pure bash solution which uses the last image sequence as your starting point (per your request):

這是一個純粹的bash解決方案,它使用最后一個圖像序列作為起點(根據您的要求):

#!/bin/bash
last_seq=$(ls image-* | tail -1 | sort -n | cut -c7-) # grab the sequence number
last_seq=${last_seq%.jpg} # remove the trailing .jpg
if [ -z $last_seq ] ; then
  echo "Unable to obtain the last image sequence number"
  exit 1
fi
for image in image-0*.jpg ; do
   [ -f ${image} ] || break  # In case no files match image-0*.jpg
   let last_seq=last_seq+1
   mv -v ${image} image-$(printf "%06d.jpg" ${last_seq})
done

Here's how it worked locally:

以下是它在本地的工作方式:

before the run:

跑步前:

~/tmp › ls -l
total 80
-rw-r--r-- 1 dyoung staff     0 Dec 12 10:20 image-00000.jpg
-rw-r--r-- 1 dyoung staff     0 Dec 12 10:20 image-01000.jpg
-rw-r--r-- 1 dyoung staff     0 Dec 12 10:05 image-14908.jpg
-rw-r--r-- 1 dyoung staff     0 Dec 12 10:05 image-14909.jpg
-rw-r--r-- 1 dyoung staff     0 Dec 12 10:05 image-14910.jpg
-rw-r--r-- 1 dyoung staff   418 Dec 12 10:24 testh.sh

during the run:

在運行期間:

~/tmp › sh ./testh.sh
image-00000.jpg -> image-14911.jpg
image-01000.jpg -> image-14912.jpg

after the run:

跑完后:

~/tmp › ls -l
total 80
-rw-r--r-- 1 dyoung staff     0 Dec 12 10:05 image-14908.jpg
-rw-r--r-- 1 dyoung staff     0 Dec 12 10:05 image-14909.jpg
-rw-r--r-- 1 dyoung staff     0 Dec 12 10:05 image-14910.jpg
-rw-r--r-- 1 dyoung staff     0 Dec 12 10:20 image-14911.jpg
-rw-r--r-- 1 dyoung staff     0 Dec 12 10:20 image-14912.jpg
-rw-r--r-- 1 dyoung staff   429 Dec 12 12:25 testh.sh

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/12/12/443c611e6d5b1f99c5aa4d5d38f2afef.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com