Click here to Skip to main content
Click here to Skip to main content

The Poetry of Ruby - Jumping to an Outlook Folder from the DOS Console

, 17 Aug 2007
Rate this:
Please Sign up or sign in to vote.
The poetry of Ruby - jumping to an Outlook folder from the DOS console.

Introduction

This article is offered to you as a short simple poem written in Ruby. It is by no means a masterpiece, but utility in the smallest of circumstances could cause a shift of balance in popular, everyday life.

The Purpose

Imagine you work with the Outlook program running on Windows. You might have a lot of folders in your personal store because you respect yourself as an organized and thorough professional? Perhaps you are so desperate sometimes to find these folders that you are even willing to open a command line "DOS" console if someone suggested it might help?

The Script

Running the following little Ruby program from the command line and passing at least a part of the folder name you would like to locate as an argument, should find the first folder with such a name. If so, it will ask Outlook to jump to the folder and display itself.

Sample image

# //////////////////////////////////////////////////////////////////////////////
# // file: pgog.rb
# // desc: pgog 'perfectly good outlook grep' - outlook folder scanning utility
# // auth: pieter greyling [-pg-] <a href="mailto:pieter@demonsource.com">pieter at demonsource dot com
</a># // copy: pieter greyling<a href="mailto:pieter@demonsource.com">
</a># // free/public domain/credit mention mandatory for all uses
# // date: 2006.07
# // hist: [-pg-]/2006.07/created
# // like: 
# // > pgog inbox
# // -- find folder : inbox
# // -- outlook version : 11.0.0.6568
# // -- start folder : PGREYLING
# // -- found [inbox] in [Inbox]
# // ...OR...
# // > pgog specs
# // -- find folder : specs
# // -- outlook version : 11.0.0.6568
# // -- start folder : PGREYLING
# // -- found [specs] in [__SPECS__]
# // ...OR...
# // > pgog toolatetonight
# // -- find folder : toolatetonight
# // -- outlook version : 11.0.0.6568
# // -- start folder : PGREYLING
# // -- folder "toolatetonight" not found
# //////////////////////////////////////////////////////
require 'win32ole'
# //-- members in the main namespace--------------------
@outlook_server = "Outlook.Application"
# //-- globals -----------------------------------------

module Outlook  
    OlFolderCalendar = 9
    OlFolderContacts = 10
    OlFolderDeletedItems = 3
    OlFolderDrafts = 16
    OlFolderInbox = 6
    OlFolderJournal = 11
    OlFolderNotes = 12
    OlFolderOutbox = 4
    OlFolderSentMail = 5
    OlFolderTasks = 13
    VbBinaryCompare = 0
    VbTextCompare = 1
end

# //----------------------------------------------------
# // get parent of inbox folder

def getoutlook
    begin
        WIN32OLE.connect(@outlook_server)
    rescue
        WIN32OLE.new(@outlook_server)
    end
end

# //----------------------------------------------------
# // mapi namespace

def getmapinamespace
    getoutlook.GetNameSpace('mapi')
end

# //----------------------------------------------------
# // root folder of personal folders

def getparentfolder_of_personalfolders
    getmapinamespace.folders('Personal Folders')
end

# //----------------------------------------------------
# // get parent of inbox folder

def getparentfolder_of_inbox
    getmapinamespace.GetDefaultFolder(<a href="outlook::OlFolderInbox%29.Parentend">Outlook::OlFolderInbox).Parent
end

</a># //----------------------------------------------------
# // recursively dump all sub-folder names

def dumptree(oparentfolder)
    ofolders=oparentfolder.Folders
    ifolders=ofolders.count
    ofolder=ofolders.getFirst
        1.upto(ifolders) do |i|
            puts '-- ' + i.to_s + ': ' + ofolder.Name
            if 0 < ofolder.folders.count then dumptree(ofolder) end
            ofolder=ofolders.getNext
        end
end

# //----------------------------------------------------
# // recursively search sub-folder names

def searchtree(oparentfolder, sfind)

    sfolder='' #// folder name
    tfolder=nil #// work folder
    ffolder=nil #// found folder must be nothing to begin
    sfind=sfind.downcase #// no case
    ofolders=oparentfolder.Folders
    ifolders=ofolders.count
    ofolder=ofolders.getFirst

    1.upto(ifolders) do |i|
        sfolder=ofolder.Name.downcase #// no case
        if not sfolder.index(sfind) #// not this one?
            if 0 < ofolder.folders.count #// search the children, if we have any
                tfolder=searchtree(ofolder,sfind) 
                if tfolder #// found a match, break out
                    ffolder=tfolder
                    return ffolder
                end
            end
        else #// found a match, break out
            ffolder=ofolder
            return ffolder
        end
        ofolder=ofolders.getNext #// keep going...
    end
    return ffolder
end

# //----------------------------------------------------
# // try to determine where to start recursing

def choosestartfolder() 
    begin
        getparentfolder_of_personalfolders()
    rescue
        getparentfolder_of_inbox()
    else
        nil
    end
end

# //-- MAIN --------------------------------------------

folder_to_find = ARGV[0]
otl = getoutlook
puts '-- find folder : ' + folder_to_find
puts '-- outlook version : ' + otl.version
puts '-- start folder : ' + choosestartfolder.name
if tfolder = searchtree( choosestartfolder(), folder_to_find )
    puts '-- found [' + folder_to_find + '] in [' + tfolder.Name + ']'
    otl.ActiveExplorer.CurrentFolder = tfolder;
else
    printf '-- folder "%s" not found', folder_to_find
end

# //////////////////////////////////////////////////////
# // ends: pgog.rb
# //////////////////////////////////////////////////////

The Result

Should be a hop to the first folder with the string "_of_ruby" in its name.

Sample image

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Pieter H. C. Greyling
Chief Technology Officer ChileServe
Chile Chile
No Biography provided

Comments and Discussions

 
Questionhow can I identify that an email is added to a folder PinmemberEinas3-Mar-08 4:26 
GeneralExcellent Pinmemberjantzeno9-Aug-07 11:50 
GeneralRe: Excellent PinmemberPieter Greyling - DemonSource10-Aug-07 9:10 
GeneralIt would be nice ... PinmemberRoland Pibinger1-Nov-06 23:08 
GeneralRe: It would be nice ... PinmemberPieter Greyling - DemonSource5-Nov-06 6:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140814.1 | Last Updated 17 Aug 2007
Article Copyright 2006 by Pieter H. C. Greyling
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid