Ruby 重命名文件 [英] Ruby to rename files

查看:69
本文介绍了Ruby 重命名文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本可以重命名 .csv 文件中的文件,该文件运行良好,但我希望对其进行更多编辑,以便对我有更多帮助.

I have a script that renames files from a .csv file, this file works fine but am looking to edit it a little bit more so that it help me even more.

场景是我有 8,000 张带有 CID 代码的图像,其中一些带有 CID 代码,其中一些带有额外的文本,并且是同一 CID ID 下的多个图像,即 17263.jpg17263a.jpg, 17623b.jpg,然后我需要使用产品代码重命名 CID 图像以在网站上工作,并添加相关的 abc 后面的字母,如果有多个图像,我在 .csv 列的左侧有文件 CID 文件,但我没有有一个简单的方法将这两个代码匹配在一起,因为有很多.

The scenario is I have 8,000 images that have a CID code some of them have the CID code some of them have extra text with it and are multiple images under the same CID ID i.e 17263.jpg 17263a.jpg, 17623b.jpg, I then need to rename the CID images with product codes to work on a website, as well as adding the relevant a or b or c letter after it, if there are multiple images I have the files CID files in the left hand of the .csv column but i don't have a easy way of matching the two code up together because there are so many.

我想知道是否有一种方法可以编写 ruby​​ 脚本来搜索文件名中的相关 cid 代码并将其重命名为相关产品编号?

I was wondering if there was a way of scripting ruby to search for the relevant cid code within the file name and rename it as the relevant product number?

这是我下载脚本的链接,我已经对其进行了更改,因此它可以与新的 ruby​​ 一起使用

Here is a link to where i downloaded the Script, i have changed it so it works with new ruby

http://roguepenguin.net/PFID2SKU/

下面列出的一些文件名只需要5位数的代码和一个b或c等,如果有这个产品的多张图片

Some of the file names are listed below just need the 5 digit code and a b or c etc if there are multiple images of this product

15144 a.jpg
15144 inset a.jpg
15144 inset b.jpg
15144 inset c.jpg
15144 inset d.jpg
15144 inset e.jpg
15144 inset f.jpg
15144 inset g.jpg
15144 inset h.jpg
15144 inset i.jpg
15155.jpg
15178.jpg
15233 a.jpg
15233 b.jpg
15233 box.jpg
15270.jpg
15321.jpg
15333 a.jpg
15333.jpg
15414 2010.jpg
15458 - 2632.jpg

抱歉本周早些时候的帖子搞砸了

Sorry for the mess up with the post earlier this week

希望你能理解.

感谢您的帮助.

推荐答案

Ruby 文件类有一个 rename() 方法.因此,您可以搜索要重命名的文件,然后使用 File.rename() 方法重命名所有文件.

The Ruby file class has a rename() method. So you could search for the files you want to rename then use the File.rename() method to rename all of them.

示例:

product_name = "Some String";
id = 0;

filenames = Dir.glob("*.jpg")

filenames.each do |filename|
    File.rename(filename, product_name + id.to_s)
    id += 1
end

这篇关于Ruby 重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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