using Ruby to rename files and edit their content

Recently at work, the web admin for the computer science department came into our lab and told us that my employer's site was broken. The admin had need to make all .php files not act as PHP scripts, and instead, all files with the extension .sphp would now run as PHP scripts. Since my employer's site was built using PHP, that meant all of its pages were showing the source code instead of actually executing. I had to whip up a quick Ruby script in order to:

  1. Rename all .php files to have .sphp as their extension;
  2. Replace all instances of ".php" within those files with ".sphp", to take care of include statements, links, etc.

Here's what I came up with:

Ruby

#!/usr/bin/env ruby
require 'find'
require 'fileutils'

# Used to go through, starting at the directory start_dir and working
# recursively, and rename all files that end in .php to end in .sphp
# because the CS admin got a wild hair.  Also goes through all .php
# and .sphp files and replaces all instances of ".php" in them with
# ".sphp".
def finder( start_dir )
  Find.find( start_dir ) do |path|
    if FileTest.file?( path )
      if path =~ /\.php$/i
        old_name = File.basename( path )
        new_name = old_name.gsub( /\.php$/, '.sphp' )
        dir = path.gsub( /#{old_name}$/, '' )
       
        if File.exists?( dir + old_name )
          puts "#{dir + old_name} to #{dir + new_name}\n"
         
          # Since the code is part of a Subversion repository, we use
          # the 'svn' command to let Subversion rename the file
          system( "svn mv #{dir + old_name} #{dir + new_name}" )
        end
      elsif path =~ /\.sphp$/i || path =~ /\.php$/i
        puts "Replacing instances of '.php' in #{path}\n"
        system( %Q{ruby -pe 'gsub(/\\.php/, ".sphp")' -i #{path} } )
      end
    end
  end
end

finder( '.' )

This entry was posted in Programming and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

4 Comments

  1. Posted 22 November 2007 at 3:25 AM | Permalink

    That looks so fun. I’d love to learn Ruby except… My server doesn’t support it. :(

  2. Posted 31 January 2008 at 7:59 AM | Permalink

    This line was very useful for me, thanks! I was wondering how to rename files and deal with svn in a migration, it never occurred to me to simply speak to svn from inside the migration. It’s a nice easy way to rename as well :)

    system( “svn mv #{dir + old_name} #{dir + new_name}” )

  3. Posted 13 March 2008 at 9:28 PM | Permalink

    This looks helpful, but I don’t understand something. When does the line elsif path =~ /\.sphp$/i || path =~ /\.php$/i
    get called? I don’t understand an elsif that checks the same condition as the if. Am I missing something?

  4. Posted 14 March 2008 at 1:36 PM | Permalink

    Er, Luke, good point. I don’t know why I did the elsif with one of the same conditions as the if.

3 Trackbacks

  1. By Edit Filenames and Content with Ruby. | Menekali on 12 February 2008 at 2:50 PM

    [...] 3Till7 addthis_url = ‘http%3A%2F%2Fwww.menekali.com%2Fedit-filenames-and-content-with-ruby%2F’; [...]

  2. [...] found a nice script from Sarah [...]

  3. [...] I use Ruby because I am most comfortable in it. It’s one of the few languages in which I find myself struggling with the logic of my problem, rather than the syntax or restrictions of the language. Compare this to C++ or Perl, in which I get frustrated over pointers and sigils. I find recursive directory traversal and running system commands very easy to do in Ruby, e.g. using Ruby to rename files and edit their content. [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>