Results 1 to 9 of 9

Thread: [RESOLVED] Cleanest way to get only the the drive letter or network name from a filepath

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    211

    Resolved [RESOLVED] Cleanest way to get only the the drive letter or network name from a filepath

    Lets say a user inputs a file path. Could be any style:

    C:\Program Files
    C:\
    G:\temp
    \\netdrv10\c$\windows
    \\srcdrv22\d$\temp

    I'd like to get only the drive or network name from the path (which I've bolded)

    Whats the cleanest way to handle both local and network paths like this?
    Last edited by DssTrainer; Dec 18th, 2008 at 03:09 PM.

  2. #2
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,210

    Re: Cleanest way to get only the the drive letter or network name from a filepath

    Your examples show Folder Paths rather than File Paths however maybe;

    RequiredString$ = Left$(SomePath$,InstrRev(SomePath$,"\"))

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    211

    Re: Cleanest way to get only the the drive letter or network name from a filepath

    Quote Originally Posted by Magic Ink
    Your examples show Folder Paths rather than File Paths
    Oh same thing... Fine:
    C:\Program Files\text.txt
    C:\text.txt
    G:\temp\text.txt
    \\netdrv10\c$\windows\text.txt
    \\srcdrv22\d$\temp\text.txt

    Happy?!!?


    Quote Originally Posted by Magic Ink
    RequiredString$ = Left$(SomePath$,InstrRev(SomePath$,"\"))
    That only gives me the "folderpaths" I want just the drives

  4. #4
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Cleanest way to get only the the drive letter or network name from a filepath

    Code:
    Option Explicit
    
    Public Function DrivePath(ByRef Path As String) As String
        Dim lngPos As Long
        lngPos = InStr(Path, "$\")
        If lngPos = 0 Then lngPos = InStr(Path, ":\")
        If lngPos Then DrivePath = Left$(Path, lngPos + 1)
    End Function
    
    Private Sub Form_Load()
        Debug.Print DrivePath("C:\Program Files\text.txt")
        Debug.Print DrivePath("C:\text.txt")
        Debug.Print DrivePath("G:\temp\text.txt")
        Debug.Print DrivePath("\\netdrv10\c$\windows\text.txt")
        Debug.Print DrivePath("\\srcdrv22\d$\temp\text.txt")
    End Sub
    c$ and d$ however may also represent something else than drives, just getting the \\COMPUTER\ part may be generally better, but this may be well enough for your needs.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    211

    Re: Cleanest way to get only the the drive letter or network name from a filepath

    Perfect!
    Much cleaner than my rat's nest of Mids and rights and while loops

    Thanks!

  6. #6
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: [RESOLVED] Cleanest way to get only the the drive letter or network name from a filepath

    Just another way about it
    Code:
    Public Function DrivePath(ByRef Path As String) As String
    Dim strSearch As String
    
        If InStr(Path, "$\") Then
            strSearch = "$\"
        Else
            strSearch = ":\"
        End If
        
        DrivePath = Split(Path, strSearch)(0) & strSearch
        
    End Function
    
    Private Sub Form_Load()
        Debug.Print DrivePath("C:\Program Files\text.txt")
        Debug.Print DrivePath("C:\text.txt")
        Debug.Print DrivePath("G:\temp\text.txt")
        Debug.Print DrivePath("\\netdrv10\c$\windows\text.txt")
        Debug.Print DrivePath("\\srcdrv22\d$\temp\text.txt")
    End Sub

  7. #7
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: [RESOLVED] Cleanest way to get only the the drive letter or network name from a filepath

    MarkT: you forgot to think what happens when there are errors or invalid input is given, such as an empty string. This can happen if for some reason the source where the path is gotten happens to have an error condition of it's own and the path can't be resolved

  8. #8
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: [RESOLVED] Cleanest way to get only the the drive letter or network name from a filepath

    Your problem has been solved but I want to give my input that may benefit others:

    "netdrv10" and "srcdrv22" are server names,
    "c$" and "d$" are drive names on a server, these names may not ended with "$", it may be "global", "common", "anything", ...

    If a server drive name does not end with "$", such as "\\netdrv10\common" then Merri's code will return an empty string.

    There are many ways to extract the proper drive name from a file path. Below are some of them:
    These functions return a drive name without a trailing "\", and assumed that the input sFilePath is valid.

    Option 1
    Code:
    Function DriveName(sFilePath As String) As String
       DriveName = Left$(sFilePath, 2)
       If DriveName = "\\" Then
          DriveName = Left$(sFilePath, InStr(InStr(3, sFilePath, "\") + 1, sFilePath, "\") - 1)
       End If
    End Function
    Option 2
    Code:
    Function DriveName(sFilePath As String) As String
       Dim st() As String
    
       st = Split(sFilePath, "\")
       If Len(st(0)) Then
          DriveName = st(0)
       ElseIf UBound(st) > 2 Then
          DriveName = "\\" & st(2) & "\" & st(3)
       End If
    End Function
    Option 3: If you don't mind to use FileSystemObject then this is the cleanest and the best way. If input path is invalid it will return an empty string.
    Code:
    Function DriveName(sFilePath As String) As String
       DriveName = CreateObject("Scripting.FileSystemObject").GetDriveName(sFilePath)
    End Function
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  9. #9
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: [RESOLVED] Cleanest way to get only the the drive letter or network name from a filepath

    Good point. A couple more lines takes care of that.
    Code:
    Public Function DrivePath(ByRef Path As String) As String
    Dim strSearch As String
    
        If InStr(Path, "$\") Then
            strSearch = "$\"
        ElseIf InStr(Path, ":\") Then
            strSearch = ":\"
        Else
            Exit Function
        End If
        
        DrivePath = Split(Path, strSearch)(0) & strSearch
        
    End Function
    
    Private Sub Form_Load()
        Debug.Print DrivePath("C:\Program Files\text.txt")
        Debug.Print DrivePath("C:\text.txt")
        Debug.Print DrivePath("G:\temp\text.txt")
        Debug.Print DrivePath("\\netdrv10\c$\windows\text.txt")
        Debug.Print DrivePath("\\srcdrv22\d$\temp\text.txt")
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width