up home page bottom

Add a comment

French German version Spanish version Italian version

Edit Filenames and Content with Ruby.

1 Star5 Stars (No Ratings Yet)
Loading ... Loading ...

Very interesting piece of code here. Essentially this will take all .php files, and change their extensions to a .sphp file extension. Then it will replace all instances of ".php" in the files, and replace them with ".sphp" for purposes of linking, and inclusion. Very neat script.

RUBY:
  1. <p style="font-weight: normal; font-family: 'Andale Mono', 'Courier New', Courier, monospace">#!/usr/bin/env ruby</p>
  2. require 'find'
  3.  
  4. require 'fileutils'
  5. <p style="font-weight: normal; font-family: 'Andale Mono', 'Courier New', Courier, monospace"># Used to go through, starting at the directory start_dir and working</p>
  6. <p style="font-weight: normal; font-family: 'Andale Mono', 'Courier New', Courier, monospace"># recursively, and rename all files that end in .php to end in .sphp</p>
  7. <p style="font-weight: normal; font-family: 'Andale Mono', 'Courier New', Courier, monospace"># because the CS admin got a wild hair.  Also goes through all .php</p>
  8. <p style="font-weight: normal; font-family: 'Andale Mono', 'Courier New', Courier, monospace"># and .sphp files and replaces all instances of ".php" in them with</p>
  9. <p style="font-weight: normal; font-family: 'Andale Mono', 'Courier New', Courier, monospace"># ".sphp".</p>
  10. def finder( start_dir )
  11.  
  12.   Find.find( start_dir ) do |path|
  13.  
  14.     if FileTest.file?( path )
  15.  
  16.       if path =~ /\.php$/i
  17.  
  18.         old_name = File.basename( path )
  19.  
  20.         new_name = old_name.gsub( /\.php$/, '.sphp' )
  21.  
  22.         dir = path.gsub( /#{old_name}$/, '' )
  23.  
  24.        
  25.  
  26.         if File.exists?( dir + old_name )
  27.  
  28.           puts "#{dir + old_name} to #{dir + new_name}\n"
  29.  
  30.          
  31.  
  32.           # Since the code is part of a Subversion repository, we use
  33.  
  34.           # the 'svn' command to let Subversion rename the file
  35.  
  36.           system( "svn mv #{dir + old_name} #{dir + new_name}" )
  37.  
  38.         end
  39.  
  40.       elsif path =~ /\.sphp$/i || path =~ /\.php$/i
  41.  
  42.         puts "Replacing instances of '.php' in #{path}\n"
  43.  
  44.         system( %Q{ruby -pe 'gsub(/\\.php/, ".sphp")' -i #{path} } )
  45.  
  46.       end
  47.  
  48.     end
  49.  
  50.   end
  51. <p style="font-weight: normal; font-family: 'Andale Mono', 'Courier New', Courier, monospace">end</p>
  52. finder( '.' )

Source: 3Till7

Spread the Love: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • TwitThis
  • Technorati
  • del.icio.us
  • Facebook
  • Sphinn
  • Mixx
  • Google
  • Live
  • YahooMyWeb
  • blogmarks
  • BlogMemes
  • co.mments
  • Fark
  • feedmelinks
  • Gwar
  • Linkter
  • Netvouz
  • Smarking
  • Socialogs
  • Taggly
  • Yigg

1 Comment so far »

  1. Edit Filenames and Content with Ruby. « PHP Net said,

    Wrote on February 12, 2008 @ 3:00 pm

    [...] Filenames and Content with Ruby. This entry was written by Menekali. Bookmark the permalink. Follow any comments here with the RSS feed for this post.Content related [...]

Comment RSS · TrackBack URI

Leave a Comment

Leave a comment or send a note
  1. (required)
  2. (valid email required)
  3. (required)
  4. Send
 

cforms contact form by delicious:days