批量替換,一個文件夾中, 所有文件中的某個特定字符串(For Lync Update)

因爲Office升級, 需要對所有的腳本內容, 做如下字符串替換.

 

替換前
"C:\Program Files (x86)\Microsoft Lync\communicator.exe"

替換後
"C:\Program Files (x86)\Microsoft Office\Office15\lync.exe"

 

試了試下面的shell命令, 有些文件可以進行替換, 但是有些文件sed就是說找不到.

http://offbytwo.com/2011/06/26/things-you-didnt-know-about-xargs.html

http://www.linuxask.com/questions/how-to-adjust-the-argument-position-in-xargs

 

grep -rl communicator . | xargs -0 sed -i 's/C:\\Program Files (x86)\\Microsoft Lync\\communicator.exe/C:\\Program Files (x86)\\Microsoft Office\\Office15\\lync.exe/g'

 

 

試了試, 未果, 於是切換到下面的ruby腳本. 還是這樣更快些, 對我而言.

 

require "fileutils"
require "pathname"

target_dir = 'C:/MyProgram/Launchy/Utilities/ShortCuts/Lync'


Dir.glob("#{target_dir}/**/*bat") do |filename|
  next if filename == '.' or filename == '..'
  next if File.directory? filename

  puts filename

  text = File.read(filename)
  new_contents = text
  target_str = "C:\\Program Files (x86)\\Microsoft Office\\Office15\\lync.exe"
  new_contents = new_contents.gsub(/C:\\Program Files \(x86\)\\Microsoft Lync\\communicator.exe/, target_str)
  new_contents = new_contents.gsub(/C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Microsoft Lync\\Microsoft Lync 2010.lnk/, target_str)
  new_contents = new_contents.gsub(/C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Microsoft Office 2013\\Lync 2013.lnk/, target_str)

  # code for update from lync 15 (2013) to scyper
  new_contents = new_contents.gsub(/C:\\Program Files \(x86\)\\Microsoft Office\\Office15\\lync.exe/, target_str)

  # To merely print the contents of the file, use:
  puts new_contents

  # To write changes to the file, use:
  File.open(filename, "w") {|file| file.puts new_contents }
end

 

----

[The End]

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章