file_creation.rb 811 B

12345678910111213141516171819202122232425262728293031323334
  1. module FileCreation
  2. OLDFILE = "old"
  3. NEWFILE = "new"
  4. def create_timed_files(oldfile, *newfiles)
  5. return if (File.exist?(oldfile) &&
  6. newfiles.all? { |newfile|
  7. File.exist?(newfile) && File.stat(newfile).mtime > File.stat(oldfile).mtime
  8. })
  9. now = Time.now
  10. create_file(oldfile, now - 60)
  11. newfiles.each do |newfile|
  12. create_file(newfile, now)
  13. end
  14. end
  15. def create_dir(dirname)
  16. FileUtils.mkdir_p(dirname) unless File.exist?(dirname)
  17. File.stat(dirname).mtime
  18. end
  19. def create_file(name, file_time=nil)
  20. create_dir(File.dirname(name))
  21. FileUtils.touch(name) unless File.exist?(name)
  22. File.utime(file_time, file_time, name) unless file_time.nil?
  23. File.stat(name).mtime
  24. end
  25. def delete_file(name)
  26. File.delete(name) rescue nil
  27. end
  28. end