Site Tools

Splitting a File Path String

Developer: RhinoScript
Summary: How to break a file path string in to its components.


When writing C/C++ applications, the C-runtime library provides the _splitpath function to break a path string into its four component pieces. It is possible to write a similar function in VBScript with some help from scripting's FileSystemObject.


The following VBScript subroutine can be used to break a file path string into its components.

 Sub SplitPath(ByVal sPath, ByRef sDrive, ByRef sDir, ByRef sFname, ByRef sExt)
   Dim fso
   Set fso = CreateObject("Scripting.FileSystemObject")
   sDrive = fso.GetDriveName(sPath)
   sDir = Mid(fso.GetParentFolderName(sPath), Len(sDrive)+1) & "\"
   sFname = fso.GetBaseName(sPath)
   sExt = "." & fso.GetExtensionName(sPath)
   Set fso = Nothing
 End Sub

The subroutine can be used as follows:

 Sub Test()
   Dim sPath, sDrive, sDir, sFname, sExt
   sPath = Rhino.DocumentPath & Rhino.DocumentName
   SplitPath sPath, sDrive, sDir, sFname, sExt
   MsgBox sDrive & VbCrLf & sDir & VbCrLf & sFname & VbCrLf & sExt
 End Sub

developer/scriptsamples/splitpath.txt ยท Last modified: 2015/09/14 (external edit)