MungPath( )

Purpose
Creates a shorter string from a long directory path.
 
Author Date of code (original) Updated on Date of page (original) Updated on
Bill Thoen
bthoen@ctmap.com
09Jan2001
09Jan2001
09Jan2001
29Jan2001
 
Restrictions on use
This module is distributed under the terms of the Lesser GNU General Public License. Restrictions on the use of this work in a commercial application or derivative work is described in the Lesser GNU General Public License page at: http://www.fsf.org/copyleft/lesser.html
 
Description
Sometimes there is limited space on a dialog or form to display a particularly long pathname, so MungPath can be used to intelligently shorten a pathname for display. The middle is replaced with an ellipsis (...) but the beginning and the end of the pathname are retained. Break points are always at a slash (\) boundary.

For example, a 56-character path like C:\Data\NorthAmerica\UnitedStates\Colorado\Counties.tab would be "munged" to C:\...\Colorado\Counties.tab if you called MungPath with it and iSize = 30.

 
External resources
none
 
Declare statement of sub_function. Include in your program. Copy/Paste if needed.
declare function MungPath (
   byval sPath0 as string,  'Path to be "munged"
   byval iSize as smallint  'Size limit of result
   ) as string
 
Returned value(s) (function only)
  String of "shortened" path
 
Other required declare statement(s). Include in your program. Copy/Paste if needed.
none
 
MapBasic Code. Copy/Paste if needed.
function MungPath (
   byval sPath0 as string,
   byval iSize as smallint)
   as string

dim sPath as string
dim i, j, j0 as smallint

   sPath = sPath0
   if Len(sPath) >= iSize then
      i = instr(1, sPath, "\\")
      if i = 0 then
         i = instr(1, sPath, "\")
      end if
      if i = Len(sPath) then
         goto Xit
      end if

      j0 = i
      j = instr(i+1, sPath, "\")
      do while Len(sPath) - j >= iSize-5
         j0 = j
         j = instr(j+1, sPath, "\")
      loop
      if j = Len(sPath) then
         j = j0
      end if
      sPath = left$(sPath, i) + "..." + right$(sPath, Len(sPath)-j+1)
   end if

Xit:
   MungPath = sPath
end function
 
Availability for download
Dir_File.zip
 
Example
dim sFilename, sMunged as string

   sFilename = "C:\Data\NorthAmerica\UnitedStates\Colorado\Counties.tab"
   sMunged = MungPath (sFilename, 30)
   
   'sMunged now contains "C:\...\Colorado\Counties.tab"
 
Comments

 
See also