批量替换,一个文件夹中, 所有文件中的某个特定字符串(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]

 

你可能感兴趣的:(批量替换,一个文件夹中, 所有文件中的某个特定字符串(For Lync Update))