Advertisement
Narzew

Multifile replacer by Narzew

Dec 30th, 2016
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.10 KB | None | 0 0
  1. # Multifile replacer v 1.00 by Narzew
  2. # Usage:
  3. # replace.rb is name of this script
  4. # replace.rb directory oldphrase newphrase
  5. # for example:
  6. # replace.rb dira lorem kek
  7. # will replace all occurences of word 'lorem' in all files in 'dira' directory by word 'kek'
  8. # Make backup of files in targetting directory before using this script, because it will modify all files within that directory!
  9. # (C) Narzew, 30.12.2016
  10. # Share where you want but keep original credits!
  11. require 'find'
  12. dir = ARGV[0]
  13. oldphrase = ARGV[1]
  14. newphrase = ARGV[2]
  15. unless Dir.exist?(dir) || ARGV.size != 3
  16.     print "Multifile replacer by Narzew\nUsage:\nreplace.rb directory oldphrase newphrase\ndirectory - directory of all files\noldphrase - old phrase which be replaced\nnewphrase - this will be in place of oldphrase\n"
  17.     exit
  18. else
  19.     print "Multifile replacer by Narzew\n"
  20. end
  21. Find.find(dir).each{|x|
  22.     next if x == "." || x == ".." || File.directory?(x)
  23.     lambda {
  24.         data = File.read(x)
  25.         File.open(x,'wb'){|w| w.write(data.gsub(oldphrase, newphrase)) }
  26.         print "#{x} processed.\n"
  27.     }.call rescue print "#{x} process failed.\n"
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement