×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Visual Basic (Classic) FAQ

Folders and Files

A 'SplitPath' for VBasic, splitting file name and path. by jnicks
Posted: 21 Aug 02

Many BASIC programmers have to examine a file name string and separate file name drom path. The routine splitpath is common in many languages but missing from in Visual Basic.

The usual way to do this in VB is to start at the far end of the string end and look for a reverse solidus, '\', or backslash. This is a slow and more complex than necessary.

Our process is simple, we get the correct full path\name and subtract the file name.


  • DIR returns the long file name.
  • GetFullPathName is a win 32 function that returns long path and file name.
  • If we remove the number of characters at the end of the full path and file name from GetFullPathName we have the path, and we already have the file name, from DIR
  • Return path and file name separately

Done.

No loops.  Fast.



Attribute VB_Name = "Module1"
DefLng A-Z
Declare Function GetFullPathName Lib "kernel32" Alias "GetFullPathNameA" (ByVal lpFileName As String, ByVal nBufferLength As Long, ByVal lpBuffer As String, ByVal lpFilePart As String) As Long
Sub main()
  ret = splitpath("c:\autoexec.bat", path$, longfilnam$)
  MsgBox path$
End Sub


Function splitpath(fil$, path$, longfilnam$)
Static buf As String * 261, dummy As String * 20

' returns -1 as Long for error

  path$ = "": splitpath = -1
  
  longfilnam$ = Dir$(fil$)

  If 0 = Len(longfilnam$) Then Exit Function
  
  k = GetFullPathName(fil$ + Chr$(0), 260, buf, dummy)

  If k Then _
    path$ = Left(buf, k - Len(longfilnam$)): _
    splitpath = 0

End Function



Back to Visual Basic (Classic) FAQ Index
Back to Visual Basic (Classic) Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close