soledad penadés
repeat 4[fd 100 rt 90]

File system

These examples are to be taken as examples! They could be shorter I think but I have just left them as they are for the sake of clarity.

Prepend a string to files

require 'FileUtils'

torename = Dir['*.gif']
prepend = 'abc_';

torename.each do |f|
        begin
                p f
                FileUtils.mv(f, prepend+f)
        rescue
                p 'one error in f' + f
        end
end

Replace some text in the file names

require 'FileUtils'

files = Dir['ABC*']
search = 'ABC'
replace = '123'

files.each do |f|
        begin
                p f
                f2 = f.gsub(search, replace)
                p f2
                FileUtils.mv(f, f2)
        rescue
                p 'one error in f' + f
        end
end

Feel free to leave a reply

Comments are moderated: Rude and offtopic ones are out!