Guest User

Untitled

a guest
Jun 22nd, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. require 'FileUtils'
  4. require 'Pathname'
  5. require 'ftools'
  6.  
  7. delay = 7 # minutes
  8. path = "/Users/tycale/Downloads/" # With / at the end
  9. todo = []
  10.  
  11. def time_block
  12. start_time = Time.now
  13. yield
  14. return Time.now - start_time
  15. end
  16.  
  17. def repeat_every(seconds)
  18. while true do
  19. sleep( seconds - time_block { yield } )
  20. end
  21. end
  22.  
  23.  
  24. repeat_every(60.0*delay) do
  25. p todo
  26.  
  27. Dir.foreach(path) { |file|
  28. if !File.directory?(path+file) and file !~ /^\./
  29. ext = File.extname(path+file)
  30. name = File.basename(path+file, ext)
  31.  
  32. if todo.include?(name)
  33. rep = case ext
  34. when ".dmg" then "dmg"
  35. when ".jpg" then "images"
  36. when ".png" then "images"
  37. when ".tiff" then "images"
  38. when ".gif" then "images"
  39. when ".pdf" then "pdf"
  40. when ".tar" then "tar"
  41. when ".exe" then "exe"
  42. when ".zip" then "zip"
  43. when ".bz2" then "bz2"
  44. when ".html" then "web"
  45. when ".htm" then "web"
  46. else "autres"
  47. end
  48.  
  49. Dir.mkdir(path+rep) if !Pathname.new(path+rep).exist?
  50. File.move(path+file, path+rep+"/"+file, :verbose => true)
  51.  
  52.  
  53. end
  54.  
  55. todo += [name]
  56. end
  57. }
  58. end
Add Comment
Please, Sign In to add comment