Guest User

Untitled

a guest
Jun 21st, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. #!/usr/bin/ruby
  2. require 'fileutils'
  3. require 'find'
  4.  
  5. usage_args = ['--usage', '--help', '-u', '-h']
  6.  
  7. if ARGV.length.eql? 0 or ARGV.length.eql? 1 and usage_args.include? ARGV[0]
  8. puts "Usage: #{__FILE__} string_to_strip [directory]"
  9. exit 0
  10. end
  11.  
  12. # If the users specifies a directory, use it, otherwise default to the current directory
  13.  
  14. if(ARGV[1])
  15. if(File.exists? ARGV[1])
  16. Dir.chdir(ARGV[1])
  17. else
  18. puts "#{__FILE__}: Directory \"#{ARGV[1]}\" does not exist"
  19. exit 0
  20. end
  21. end
  22.  
  23. wd = Dir.getwd
  24.  
  25. puts "#{__FILE__}: In: #{wd}"
  26.  
  27. files = []
  28.  
  29. Find.find(wd) do |path|
  30. files << path
  31. end
  32.  
  33. FileUtils.cd(wd)
  34.  
  35. count = 0
  36.  
  37. files.each do |file|
  38. unless(file.gsub(ARGV[0], '').eql? file)
  39. FileUtils.mv(file, file.gsub(ARGV[0], ''), :force => true, :verbose => false)
  40. puts "#{__FILE__}: #{file} => #{file.gsub(ARGV[0], '')}"
  41. count += 1
  42. end
  43. end
  44.  
  45. puts "#{__FILE__}: Complete: #{count} " + ((count.eql? 1) ? 'file' : 'files') + " striped of \"#{ARGV[0]}\""
  46.  
  47. exit 0
Add Comment
Please, Sign In to add comment