0

Adding the correct code to iterate through the items based on arrays

Maid.rules do
search = ["Test01","Test02","Test03","Forever.US.","Castle.US."]
replace =["Item-1","Item-2","Item-3","Forever.2014.","Castle.2009."]
def rename_item(path, search, replace)
    Dir[path].each do |show|
        old = show
        new = show.gsub(search, replace)
        puts "Reanaming #{old} to #{new}"
        `mv #{old} #{new}`
    end
end
    rule '[TEST]' do    
       dir(['~/RubyTest/**/*.{txt}']).each do |shows|
           search.each_with_index {|item, idx| rename_item(shows,search[idx], replace[idx]) if shows.include?(item)}
       end
    end
end

This seems to work not sure if it is the best way to do this though

4
  • What is dir(['~/Downloads/TV/**/*.{mp4,mkv,m4v}'])? Commented Oct 9, 2014 at 10:47
  • update original post Commented Oct 9, 2014 at 10:50
  • You want to search and update more items in rename_item? Commented Oct 9, 2014 at 10:51
  • Yes that would be ideal give it a list of items to search along with rename items if it finds any of them then perform the action Commented Oct 9, 2014 at 10:52

1 Answer 1

1

Pass an array of search items and replace items.

def rename_item(path, search, replace)
    replace = search.zip replace
    Dir[path].each do |show|
        old = show
        replace.each {|item| show.gsub!(item[0], item[1])}
        new = show
        puts "Reanaming #{old} to #{new}"
        `mv #{old} #{new}`
    end
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.