Ruby to rename files

前端 未结 2 738
不知归路
不知归路 2021-01-14 10:00

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

相关标签:
2条回答
  • 2021-01-14 10:34

    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.

    Example:

    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
    
    0 讨论(0)
  • 2021-01-14 10:58

    Here's a little tidbit for you. Maybe it will help out. http://rtdptech.com/2010/08/ruby-shell-script-to-rename-files-in-a-folder/

    I used something like this for a quickie because I wanted to use the rdoc/ri files to make little examples. I copied some directories to another location and the wanted to change the extension from *.ri to *.rb so I could put my runnable ruby code in.

    here's the command line version

    $ ruby -e "Dir.glob('*/*.ri').each {|i| File.rename(i, i.gsub('ri', 'rb'))}"
    

    and here is file version

    Dir.glob('*/*.ri').each {|i| File.rename(i, i.gsub('ri', 'rb'))}
    
    0 讨论(0)
提交回复
热议问题