|
Visual Basic (VB and VBA) |
|
Copyright 1999-2001 Christopher Greaves. All rights reserved. Home Page and email to [email protected] |
| If in doubt, record a macro and inspect the entrails! |
Please read the DISCLAIMER.
Here is an INDEX to all the procedures.
You will probably need one copy of my GLOBAL DECLARATIONS.
Public Function strGetFilename(strFileName As String, intType As Integer)
' Procedure : strGetFilename
' Description: This code attempts to return the specified portion of a file path and name.
' Useful in seeing (1) if there is the possibility of a valid file in a string and
' (2) obtaining the component parts.
' Copyright: Chris Greaves Inc.
' Credits: Chris Cubley inspired me with HIS GetFileName
' Chris Cubley's code can be found at www.planet-source-code.com.
' Inputs: A string, vaguely representing a file path and name.
' An integer specifying the type of string to be returned.
' Returns: A string.
' intType = 0 -- Returns the drive letter
' intType = 1 -- Returns the directory path
' intType = 2 -- Returns the filename (without the extension)
' intType = 3 -- Returns the extension
' intType = 4 -- Returns the filename and extension
' intType = 5 -- Returns the drive and path
' Assumes: Standard drive letter and path and extent separators (:\.) defined here as constants.
' Since we have little control over the incoming string, we separate our job into tasks:
' Task 1: isolate a string that contains only valid DOS filename characters
' Task 2: analyse this sub-string for conformity, breaking out parts of the filename as we go.
' Side Effects: None.
' Tested: By the calls shown below.
' Task 1: Determine the leftmost character of strfilename that is in our character set, then
' determine the rightmost character of strfilename that is NOT in our character set.
' That will bound our essay into the analysis of a filename.
Dim lngLHS As Long
Dim lngRHS As Long
' Note: the range of allowable characters includes the high-ascii (above ASC(128)), but *I* ignore them here.
lngLHS = lngFirst(1, True, strFileName, strcAlpha & strcDigits & strcPunctuation & strcDriveSeparator & Application.PathSeparator & strcExtentSeparator)
If lngLHS > 0 Then ' we have at least one character from the set
lngRHS = lngFirst(lngLHS, False, strFileName, strcAlpha & strcDigits & strcPunctuation & strcDriveSeparator & Application.PathSeparator & strcExtentSeparator)
If lngRHS > lngLHS Then ' We found a string
' We now travel a State transition table (see appendix)
strGetFilename = strBreakFileString(Mid(strFileName, lngLHS, lngRHS - lngLHS + 1), intType)
Else
strGetFilename = ""
End If
Else
strGetFilename = ""
End If
End Function
| We all knew nothing when we started … |
|
Home Page and Contact Information Send email to [email protected]. This page was last updated Thursday, November 15, 2001 |