Code: Simple class for parsing file names

In the developerWorks forum for Notes 8, a user asked about how to check if a file (in this particular instance attached to a Rich Text Lite field) is an Adobe PDF file. The easiest (but of course not fool proof) way is to simply check the extension of the file name.

That reminded me that I have a simple Lotusscript class with some file functions that would simplify the parsing of a filename, if you want to get the path, the file name or just the extension. I thought that perhaps more people could use this, so I am posting it below. Thanks to Andre Guirard for the code to create directories. That is a function I sometimes need when working with files, so I added that to the class for my convenience.

Option Public
Option Declare

Class FileObject
  Private p_FileName As String
  Private p_FilePath As String
  Private p_Extension As String

  Public Sub New()

  End Sub

  Public Property Set FileName As String
    p_FileName = FileName
    p_Extension = StrRightBack(FileName,".")
  End Property

  Public Property Get FileName As String
    FileName = p_FileName	
  End Property

  Public Function Extension() As String
    Extension = p_Extension	
  End Function

  Public Property Set FilePath As String
    p_FilePath = FilePath	
    If Right(p_FilePath,1)<>"\" Then
      p_FilePath = p_FilePath & "\"
    End If
  End Property

  Public Property Get FilePath As String
    FilePath = p_FilePath	
  End Property

  Public Property Set FullPathName As String
    Me.FilePath = StrLeftBack(FullPathName,"\")
    Me.FileName = StrRightBack(FullPathName,"\")
  End Property

  Public Property Get FullPathName As String
    FullPathName = p_FilePath & p_FileName	
  End Property

  Public Sub MakeDir(Byval strWhere As String)     
    ' *** This code by Andre Guirard @ IBM
    ' ***
    ' *** Using an iterative method instead of recursive due to stack issues (see link above)
    On Error 76 Goto parentDoesNotExist 
    Dim stack$ 
    Const NL = { 
      Mkdir strWhere 
      On Error Goto 0 ' first success, stop trapping errors; avoid infinite loop. 
      strWhere = Strleft(stack, NL) ' "pop" a path for next iteration 
      stack = Mid$(stack, Len(strWhere)+2) 
    Loop Until strWhere = "" 
    Exit Sub 
    ' This error code can indicate other problems, but assume missing parent. 
    ' If not, we get a different error (75) later when trying to create the parent. 
    Dim fpath$, fname$ 
    SplitFilepath strWhere, fpath, fname 
    If fpath = "" Then Error 76, "Invalid path: '" & strWhere & "'" 
    stack = strWhere & NL & stack ' "push" onto stack to retry later. 
    strWhere = fpath ' try a path one step shorter. 
    Resume failed 
  End Sub 

  ' ===== Private Supporting Functions =====

  Private Sub SplitFilePath(Byval fullpath$, dirpath$, filename$) 
    ' *** This subroutine by Andre Guirard @ IBM
    ' ***
    ' *** Called from MakeDir()		
    Const DELIMS = {/\:} 
    While Instr(DELIMS, Right$(fullPath, 1)) ' discard final delimiter character... 
      fullpath = Left$(fullpath, Len(fullpath)-1) 
    Dim candidate$, i% 
    filename = Strtoken(fullpath, Left$(DELIMS, 1), -1) 
    For i = 2 To Len(DELIMS) 
      candidate = Strtoken(fullpath, Mid$(DELIMS, i, 1), -1) 
      If Len(candidate) < Len(filename) Then  				
        filename = candidate
      End If
    Dim fplen%
    fplen = Len(fullpath)-Len(filename)
    If fplen > 0 Then fplen = fplen - 1 
    dirpath = Left$(fullpath, fplen) 
  End Sub
End Class

This Post Has One Comment

  1. Karl-Henry Martinsson

    After posting this code, I got inspired to improve it, something I actually need for an upcoming project. I hope to post the new code shortly.

Leave a Reply