Skip to content

Instantly share code, notes, and snippets.

@patmaddox
Created June 1, 2011 17:50
Show Gist options
  • Select an option

  • Save patmaddox/1002863 to your computer and use it in GitHub Desktop.

Select an option

Save patmaddox/1002863 to your computer and use it in GitHub Desktop.
A script that will rename files and update hrefs based on a meta tag
#!/usr/bin/env ruby
require 'rubygems'
require 'nokogiri'
require 'fileutils'
site_root = File.expand_path('~/Sites/v42')
Dir[site_root + '/**/*'].each do |full_path|
next unless File.file?(full_path)
old_name, extension = File.basename(full_path).split(".")
doc = Nokogiri.parse(File.read(full_path))
next unless (long_path_meta_tag = (doc / 'meta[name=long_path]').first)
long_path = long_path_meta_tag.attribute('content').value
`find #{site_root} -type f | xargs perl -pi -e 's/#{old_name}\.#{extension}/#{long_path}\.#{extension}/g'`
new_location = File.expand_path(File.join(File.dirname(full_path), long_path + '.' + extension))
FileUtils.mv(full_path, new_location) unless full_path.downcase == new_location.downcase
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment