Link to home
Start Free TrialLog in
Avatar of Richard Teasdale
Richard TeasdaleFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Reusing code Ruby

Now this is IT learning day one but I am self taught so apologies. I have a nice Ruby program that backs up a directory - "G:/Sage50" in the file copied below. I need to copy other folders - eg "G:\Sage Export" so had the idea of changing the text to an array ('Source') then reusing the code with different parameters. It should be so simple but i am foxed.
Could somebody help me please, encapsulating the code into a block that can be called on many occasions, with the folders set as parameters with each run? - with exit at the very end, of course!

#!/usr/bin/env ruby

require 'fileutils'

require 'date'
require 'time'
tooday = Date.today
tomonth =Date.today.strftime('%B')
nameday = tooday.strftime('%a')
namemonth = tomonth
puts nameday
puts namemonth



Source = "G://Sage50/"
replacements = {"://" => "/"}
Sourcery = Source.gsub(Regexp.union(replacements.keys), replacements)
puts Source
lister = Dir.glob Source + "**/"

for i in lister
  sister = i.to_s.gsub(/\"/, "/").gsub(/[\[\]]/, '')
  mister = sister.slice! Source
  puts sister
  Dir.mkdir("H://"  + nameday + "/" + Sourcery + sister) unless File.exists?("H://"  + nameday + "/" + Sourcery + sister)
end

filer = Dir.glob(Source  + "**/*").select{ |e| File.file? e }
#puts filer
for i in filer
  tooday =  File.mtime(i)
  tonow = Time.now-9900000
  if tooday >  tonow
   
    siler = i.to_s.gsub(/\"/, "/").gsub(/[\[\]]/, '')
    miler = siler.slice! Source
    tiler =  "H://" +  nameday + "/"  + Sourcery
    piler = File.dirname( tiler + siler )
    riler = File.dirname(i) +'/' + File.basename(i)
    begin
    FileUtils.cp(riler,piler)
    rescue
    puts "File Not Copied " + riler
    end
  end
end

exit
ASKER CERTIFIED SOLUTION
Avatar of Richard Teasdale
Richard Teasdale
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial