File Manipulation In Action

September 23rd, 2007

Ok, time to roll up the sleeves and get started with that previous need. So that first part:

  • Find all files in a remote directory that match ~somepathhere/*htm/printable

Got it on the first try, thanks to Why’s (poignant) guide:

p Dir['*.htm/printable']

I think i’ll bust up the order and maybe work on changing the filenames next, cause I have an inkling of how to do that. More in a bit.

Ok, next I’m working on:

  • rename each from /printable to /index.html

I tried this:

Dir['*.htm/printable'].each do |file_name|
File::rename(file_name, 'index.html')
end

but that ended up ignoring the paths and saving all the files on top of each other as index.html where I ran the script. So now I’m trying:

Dir['*.htm/printable'].each do |old_file_path|
new_file_path = old_file_path.sub('printable','index.html')
File::rename(old_file_path, new_file_path)
end

looks like it worked, except it choked at the “p“‘s where it looks like there’s an actual file name that contains “printable” but does not follow this pattern. Ok, just need to adjust the script to skip that case.

I ended up making the pattern more precise by looking for /printable instead of just printable:

Dir['*.htm/printable'].each do |old_file_path| 
new_file_path = old_file_path.sub('/printable','/index.html')
File::rename(old_file_path, new_file_path)
end

Success!

Next up is:

  • insert into the base href=”” my new desired base href

and

  • modify the path to the stylesheet in each to replace “../../” with the full path

This should be fun. Some file readin’ and file writin’. Back after a short break.

Ok, I cheated a little. I couldn’t get it rocking with my own purely hand-written code. I think that’s because file.read() must send you the contents of the file as an array of lines, and I was treating it as one big string. I think I was close. But I ended up seeking help on the internet and adapting the recipe I found over here under “Modifying a File in Place Without a Temporary File”, like so:

old_base = 'BASE HREF="it was blank"'
new_base = 'BASE HREF="my new base href"'
old_style = 'HREF=" path to the old stylesheet'
new_style = 'HREF="path to the new stylesheet"'
Dir['*.htm/index.html'].each do |file_to_change|
  File.open(file_to_change,'r+') do |f|
    lines = f.readlines
    print lines
    lines.each do |it|
#make the substitutions
      it.gsub!(old_base, new_base)
      it.gsub!(old_style, new_style)
    end
#returns to line 0
    f.pos = 0
#writes the modified lines
    f.print lines
    f.truncate(f.pos)
    end
  end

I threw in “print lines” so I could see the code frantically crossing my terminal window. It all worked.

Task completed, case closed. And I’m sure the need will arise again in the future for a batch search and replace.

Sorry, comments are closed for this article.