ブログ引っ越し関連(つづき)

昨日の件、URLショートカットのファイル群:

画像
に対して、

require 'fileutils'

def crawl(shortcut_path)
 open(shortcut_path, 'r') do |source|
  contents = source.read
  if contents.sub!('https://gendai.ismedia.jp/', 'https://gendai.media/')
   puts shortcut_path
   tmp_path = 'tmp_' + shortcut_path
   dir = tmp_path.split('/')[0..-2].join('/')
   FileUtils.mkdir_p(dir) unless FileTest.exist?(dir)
   open(tmp_path, 'w') do |tmp|
    tmp.write(contents)
   end
   File::utime(File::atime(shortcut_path), File::mtime(shortcut_path), tmp_path)
  end
 end
end

Dir.glob('bookmarks/**/*.*') do |path|
 next unless path =~ /^(.+)\.(url|website)$/i
 crawl(path)
end

のような感じのスクリプトでタイムスタンプを保持したまま
URL を変えたショートカットファイルを生成して、内容確認
の上、もとのディレクトリに上書きするという手順で更新し、
GitHub の方も連動して commit しておきました。

公開対象外も含めると修正した URL は約1400でした。

この記事へのコメント