How to read files from dir and generate html output


When creating videos, I take snapshots of keynote, upload to website, and then manually handcraft the html to include in the show notes. It’s slow, tedious, and not a lot of fun.

So today I created a ruby script to do this for me.


date = ARGV[0] # 2013/06
alt = ARGV[1] # iteration mechanics
basedir = "/Users/jrasmusson/Desktop"
files = Dir.glob(basedir + "/*.png")
files.each do |k|   puts "<img src=\"http://xxx/" + date + "/" + File.basename(k) + "?w=500\" alt=\"" + alt + "\" /><hr>"

> ruby imageconverter.rb 2013/06 “iteration mechanics” | pbcopy

<img src="http://xxx/2013/06/im-analysis.png?w=500" alt="iteration mechanics" /><hr>
<img src="http://xxx/2013/06/im-check-the-work.png?w=500" alt="iteration mechanics" /><hr>
<img src="http://xxx/2013/06/im-do-the-work.png?w=500" alt="iteration mechanics" /><hr>

To see the output on screen, run without the pbcopy mac command at the end which copies to clip board.

The only thing I haven’t been able to do is make this ruby script globally available from any directory (like a bash script).

If anyone has any ideas on how to do that I would be very grateful.

A simple ruby script for reading the contents of a file and generating some output

1 Comment

Here’s a handy ruby script that will read the contents of a file

AB Aberdeen
AL St. Albans
B Birmingham
BA Bath

and generating some output (in this case sql) based on the content:


#!/usr/bin/env ruby
class SqlGenerator
  def GenerateInsert

    file ="regions.txt")
    contents = ""

    file.each {|line|

        tokens = line.split(" ")
        code = tokens[0]
        description = tokens[1]

        # handle descriptions with space
        if tokens.count > 2
            description = tokens[1] + " " + tokens[2]

        insertStatement =  "INSERT INTO [Province]( [CountryId], [Code], [Description] ) VALUES (3, '#{code}', '#{description}');\n"
        contents << insertStatement
        my_file ="inserts.sql", "w")
        my_file.puts contents


if __FILE__ == $0
  sql =

Run from the command line as:

> ruby sqlgen.rb

How to create a UNIX bash script on a mac


The ability to create and make your own custom bash scripts is on of the biggest advantages to working off a unix based operating system (like the mac).

You can take just about any mundane, or complicated task, and easily automated it by learning how to create your own bash scripts.

In this post I am going to show you how to create a bash script called:

(which recursively search all files for text containing xxx (very handy)) and call it from anywhere on your computer.

Setup your environment

Go to your home directory:

$ cd ~

and make a directory called scripts (this is where you will store your scripts):

$ mkdir scripts

Then if you don’t have one already, create a .bash_profile file (in your root directory) and add the following line:

export PATH=$PATH:~/scripts

This puts the directory you just created into the unix $PATH variable which lets you call your scripts from anywhere on your machine.

To register this new path you may need to go:

$ source .bash_profile

You can then check your new path by typing:

$ echo $PATH

If all is well you should see your new directory in the output.

OK. We are now ready to create your script.

Create your script

To create your script go back to that scripts directory your just created and using your favorite text editor create a file called ‘’

echo "Searching for text:" $1
find . -type f -exec grep -il $1 {} \;

And give yourself permission to execute it using the following command:

$ chmod 711

That’s it! At this point you are basically done.

You should now be able to run this script from anywhere on your box.

To test it out, navigate to some directory containing a lot of files and text and search for something.

Here is the output I get when I search my rails app for ‘README’

Searching for text: README

Add your aliases

You can also add aliases to your .bash_profile to help make your life easier.

alias dev='cd /Users/jr/Documents/dev/rails'

Now I can type ‘dev’ and instant be taken to my development directory.

For more information on bash and scripting click here.

%d bloggers like this: