[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?
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$,"\"))
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?!!?
:afrog: :p :D
Quote:
Originally Posted by Magic Ink
RequiredString$ = Left$(SomePath$,InstrRev(SomePath$,"\"))
That only gives me the "folderpaths" I want just the drives
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.
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!
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
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 :)
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
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