Visual Basic File API Routines
PathFindNextComponent: Parse String to Obtain Path Components
     
Posted:   Wednesday April 25, 2001
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows 2000
OS restrictions:   Windows XP
Windows 2000 (or Windows NT 4.0 with Internet Explorer 4.0 or later)
Windows 98 (or Windows 95 with Internet Explorer 4.0 or later)
Author:   VBnet - Randy Birch
     

Related:  

PathMatchSpec: Searches a String Using MS-DOS Wild-Card Match
PathFindExtension: File Extension from Local/Remote/IP/UNC Filename

PathFindFileName: File Path from Local/Remote/IP/UNC Filename

PathFindNextComponent: Parse File Path Components

PathFindNextComponent: Parse String to Obtain Path Components

PathRemoveExtension: Returns Filename with Extension Removed

PathStripPath: Removes Path Portion of Fully-qualified Path/Filename

PathUnquoteSpaces: Removes Wrapping Quotes from Paths/Filenames

PathStripToRoot: Remove All Parts of a Path Except for Root Information

PathSkipRoot: Parses Path Ignoring Drive or UNC Path

PathStripToRoot: Determine if a Path Contains a Valid Drive Syntax

PathIsSameRoot: Determine if Paths Share Common Root

PathQuoteSpaces: Returns Quoted Path String if Spaces Found
     
 Prerequisites
Version 4.71 and later of Shlwapi.dll.

Windows Shlwapi.dll exposes a  great number of path-oriented functions designed for use by the Windows shell. For more information on the shell APIs please see PathFileExists: A Local/Network File/Folder/Drive Exists Routine.

This page demonstrates using PathFindNextComponent to recursively parse a path to extract each element from the path, beginning after the drive designation. Since PathFindNextComponent parses a path for the next path component, by passing the result of the previous call each path value returned represents the remainder of the path, minus one element. Note that the call works from the left, so the last item returned is the file and extension.

Note this API has no effect on IP or URL paths.

In order to show a complete demo using a combination of path and file strings representing typical application needs, each demo uses the same 26 strings as its test bed. This provides the opportunity to see exactly how each category of path information is handled by a specific call, as well as provide a single test bed upon which you can reuse the form while building a BAS module that contains just the functions you need.

This demo uses a modification of the standard strings found in the previous demo. The code to align the label with the textboxes is hard-coded for positions designed for systems with small fonts. The form code below creates and labels the controls appropriate for this demo by just placing a single text box and label on the form, and setting the index of each to 0 in order to create a control array.

 BAS Module Code
Place the following code into the general declarations area of a bas module:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright 1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Declare Function PathFindNextComponent Lib "shlwapi" _
   Alias "PathFindNextComponentA" _
  (ByVal pPath As String) As Long
  
Private Declare Function lstrcpyA Lib "kernel32" _
  (ByVal RetVal As String, _
   ByVal Ptr As Long) As Long
                        
Private Declare Function lstrlenA Lib "kernel32" _
  (ByVal Ptr As Any) As Long
       
 
Public Function GetNextPathElement(ByVal sPath As String) As String

  'Parses a path for the next path component
  'after the root. Paths are delimited by
  'backslashes or by the NULL at the end of
  'the path.
   
   GetNextPathElement = GetStrFromPtrA(PathFindNextComponent(sPath))
   
End Function


Private Function GetStrFromPtrA(ByVal lpszA As Long) As String

  'Given a pointer to a string, return the string
   GetStrFromPtrA = String$(lstrlenA(ByVal lpszA), 0)
   Call lstrcpyA(ByVal GetStrFromPtrA, ByVal lpszA)
   
End Function
 Form Code
Add one label (Label1), one text box (Text1) to a form. Set the index property of each to 0 to create control arrays; the demo load event code will create and label the needed controls. Add a command button (Command1), along with the following code:

Option Explicit

Private Form_Load 

  Dim cnt As Long
   
   For cnt = 0 To 22
   
      If cnt > 0 Then Load Text1(cnt)
      
      With Text1(cnt)
         .Left = 3700
         .Top = 210 + (300 * cnt)
         .Width = 3000
         .Visible = True
      End With
            
   Next
   
   For cnt = 0 To 4
   
      If cnt > 0 Then Load Label1(cnt)
      With Label1(cnt)
         .Left = 120
         .Visible = True
      End With
  Next
         
   Label1(0).Top = 300
   Label1(1).Top = 1450
   Label1(2).Top = 3000
   Label1(3).Top = 4150
   Label1(4).Top = 5650
         
   With Command1
      .Left = 300
      .Top = Text1(22).Top + (Text1(18).Height * 1.5)
      .Width = 1935
      .Caption = "GetNextPathElement"
   End With
   
  'display the strings that will be used in the call
   
  'absolute path
   Label1(0).Caption = "c:\path1\path2\path3\somefile.txt"
   
  'relative path
   Label1(1).Caption = "..\..\path1\path2\path3\somefile.txt"
   
  'mapped drive
   Label1(2).Caption = "m:\path1\path2\path3\somefile.txt"
  
  'UNC
   Label1(3).Caption = "\\workstation\path1\path2\path3\somefile.txt"
  
  'IP address
   Label1(4).Caption = "\\192.168.1.100\path1\path2\path3\somefile.txt"
      
End Sub


Private Sub Command1_Click()

  'Parses a path for the next path component
  'after the root. Paths are delimited by
  'backslashes or by the NULL at the end of
  'the path.
  
   Dim tmp As String
   Dim cnt As Long
   
   tmp = Label1(0).Caption
   For cnt = 0 To 3

      tmp = GetNextPathElement(tmp)
      Text1(cnt).Text = tmp

   Next
   
   
   tmp = Label1(1).Caption
   For cnt = 4 To 8
      
      tmp = GetNextPathElement(tmp)
      Text1(cnt).Text = tmp
   
   Next
   
   
   tmp = Label1(2).Caption
   For cnt = 9 To 12
      
      tmp = GetNextPathElement(tmp)
      Text1(cnt).Text = tmp
   
   Next
   
  
   tmp = Label1(3).Caption
   For cnt = 13 To 17
      
      tmp = GetNextPathElement(tmp)
      Text1(cnt).Text = tmp
   
   Next
   
   tmp = Label1(4).Caption
   For cnt = 18 To 22
      
      tmp = GetNextPathElement(tmp)
      Text1(cnt).Text = tmp
   
   Next

   
End Sub
 Comments

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright 1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter