开发者

Re-Post - Ruby on Rails Script to Rename Files

I have reposted this because i have added to it

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.

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.

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?

Here is a link to where i downloaded the Script, i have changed it so i开发者_开发知识库t works with new ruby

http://roguepenguin.net/PFID2SKU/

Sorry for the mess up with the post earlier this week

Hope you can understand.

Thanks for the help.


I do not understand the ruby file, and I did not try to do it. I think you have two questions:

  1. How to search for relevant CID code inside file name?
  2. How to rename the file with the replaced name.

Here my try:

  1. Finding the CID code

    • Learn how to use reg-expressions in Ruby.
    • Use the String function match to see if the CID matches the filename
    • Use the function sub to replace the CID with the new value. You then have the new filename.
  2. Renaming the file

    • There is a library fileutils in Ruby, which you have to require.
    • then use the function mv

Hope that helps ...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜