|
-
Dec 18th, 2008, 02:59 PM
#1
Thread Starter
Addicted Member
[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.
-
Dec 18th, 2008, 03:33 PM
#2
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$,"\"))
-
Dec 18th, 2008, 03:44 PM
#3
Thread Starter
Addicted Member
Re: Cleanest way to get only the the drive letter or network name from a filepath
 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?!!?
 Originally Posted by Magic Ink
RequiredString$ = Left$(SomePath$,InstrRev(SomePath$,"\"))
That only gives me the "folderpaths" I want just the drives
-
Dec 18th, 2008, 04:10 PM
#4
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.
-
Dec 18th, 2008, 04:17 PM
#5
Thread Starter
Addicted Member
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!
-
Dec 18th, 2008, 05:04 PM
#6
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
-
Dec 18th, 2008, 05:25 PM
#7
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
-
Dec 18th, 2008, 05:50 PM
#8
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
-
Dec 18th, 2008, 05:54 PM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|