home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 24 / CD_ASCQ_24_0995.iso / vrac / homonlib.zip / FILEPART.BAS < prev    next >
BASIC Source File  |  1995-04-13  |  4KB  |  101 lines

  1. DEFINT A-Z
  2.  
  3. DECLARE FUNCTION FileParts$ (filespec$, operation$)
  4.  
  5.  
  6. 'External procedures:
  7.  
  8. DECLARE FUNCTION Rinstr (start, search$, lookfor$)
  9.  
  10. FUNCTION FileParts$ (filespec$, operation$)
  11. '****************************************************************************
  12. 'Returns a specified part of an extended filename or other filespec type
  13. ' string.
  14. '
  15. 'The return value depends upon the value of the operation$ argument:
  16. '
  17. 'Example: filespec$ = "C:\GAMES\SAVEGAME.001"
  18. '         FileParts$(filespec$,"P")  -->  "C:\GAMES\"       (Path)
  19. '         FileParts$(filespec$,"F")  -->  "SAVEGAME.001"    (Filename)
  20. '         FileParts$(filespec$,"E")  -->  "001"             (Extension)
  21. '         FileParts$(filespec$,"N")  -->  "SAVEGAME"        (fileName)
  22. '         FileParts$(filespec$,"D")  -->  "C:"              (Drive)
  23. '
  24. '         filespec$ = "HOMEWORK.TXT"
  25. '         FileParts$(filespec$,"P")  -->  ""   Returns null if the requested
  26. '         FileParts$(filespec$,"D")  -->  ""   info is not part of filespec$
  27. '
  28. 'Paths are returned with a trailing backslash.  Drive letters are returned
  29. ' with a trailing colon.  Extensions are returned without a leading period.
  30. '
  31. 'Quirks:  FileParts$() assumes that all pathnames end in a backslash.  If you
  32. ' pass the function one that does not, it will think that it is a filename:
  33. '
  34. ' Example: "C:\GAMES" will be interpreted as a file called "GAMES" in the
  35. '           root directory of C:.  "C:\GAMES\" would be Ok.
  36. '
  37. 'Note: The letter "X" is accepted as well as "E" to get the extension.
  38. '
  39. '****************************************************************************
  40.  
  41. fspec$ = LTRIM$(RTRIM$(filespec$))      'Trim any spaces off filespec$
  42.  
  43. IF fspec$ = "" THEN                     'If the filespec is a null string,
  44.      FileParts$ = ""                    'return a null string.
  45.      EXIT FUNCTION
  46. END IF
  47.  
  48. bs$ = "\"                               'For optimization.
  49. part$ = ""                              'Initialize to null.
  50.  
  51. dot = INSTR(fspec$, ".")                'Find a dot, if any.
  52. colon = INSTR(fspec$, ":")              'Find a colon, if any.
  53. lastslash = Rinstr(0, fspec$, bs$)      'Find the last backslash, if any.
  54.  
  55. SELECT CASE UCASE$(LEFT$(operation$, 1))
  56.      CASE "F"                                                    'Filename
  57.           IF lastslash THEN             'Find the left boundary   (w/ ext.)
  58.                l = lastslash + 1
  59.           ELSEIF colon THEN
  60.                l = colon + 1
  61.           ELSE
  62.                l = 1          'It must be a filename only.
  63.           END IF
  64.           part$ = MID$(fspec$, l)
  65.      CASE "N"                                                    'fileName
  66.           IF lastslash THEN             'Find the left boundary   (w/o ext.)
  67.                l = lastslash + 1
  68.           ELSEIF colon THEN
  69.                l = colon + 1
  70.           ELSE
  71.                l = 1
  72.           END IF
  73.           IF dot THEN                   'Find the right boundary
  74.                r = dot - 1
  75.                part$ = MID$(fspec$, l, (dot - l))
  76.           ELSE
  77.                part$ = MID$(fspec$, l)
  78.           END IF
  79.      CASE "E", "X"                                               'EXtension
  80.           IF dot THEN
  81.                part$ = MID$(fspec$, (dot + 1))
  82.           END IF
  83.      CASE "P"                                                    'Path
  84.           IF lastslash THEN                      'Regular pathname
  85.                part$ = LEFT$(fspec$, lastslash)
  86.           ELSEIF colon THEN                      'Drive letter only
  87.                part$ = LEFT$(fspec$, colon) + bs$
  88.           END IF
  89.      CASE "D"                                                    'Drive
  90.           IF colon THEN
  91.                part$ = LEFT$(fspec$, colon)
  92.           END IF
  93.      CASE ELSE                                                   'What???
  94.           part$ = ""
  95. END SELECT
  96.  
  97. FileParts$ = part$                      'Return the requested information.
  98.  
  99. END FUNCTION
  100.  
  101.