|
-
Jan 6th, 2020, 08:42 PM
#1
Thread Starter
PowerPoster
[RESOLVED] How to get just the pathway from a pathway and a filename? :)
Hi there folks! I am working on a game for the classroom. I have a pathway and a filename, and I just need the pathway from it. I know about using Ubound to split the path and filename... like this...
VB Code:
Dim strPath as string
Dim strFile as string
Dim UB as string
strPath = LBLPath.Caption
strFile = Split(strPath, "")
UB = UBound(strFile)
MsgBox strFile(UB)
End Sub
Now that will def give me the filename, but what I want is the path for the file. Is there a different way to split? Thanks!!
-
Jan 6th, 2020, 09:13 PM
#2
Fanatic Member
Re: How to get just the pathway from a pathway and a filename? :)
You could use:
1 - InStrRev(s, "\")
2 - Call the shell function PathRemoveFileSpec().
Search the forum for these if you need more help or examples.
-
Jan 6th, 2020, 11:24 PM
#3
Re: How to get just the pathway from a pathway and a filename? :)
Code:
Option Explicit
Private Sub Form_Click()
Dim sPathAndFile As String
Dim sFile As String
Dim sPath As String
sPathAndFile = App.Path & "\" & "SomeFile.txt"
Me.Print sPathAndFile
sPath = ReturnThePath(sPathAndFile)
Me.Print sPath
End Sub
Private Function ReturnThePath(sIN As String) As String
Dim sPathAndFile As String
Dim sPath As String
Dim sFile As String
Dim arrPathAndFile() As String
sPathAndFile = sIN
arrPathAndFile = Split(sPathAndFile, "\")
'Place file name into sFile
sFile = arrPathAndFile(UBound(arrPathAndFile))
'Remove the file name from the input string, thus allowing us to return just the path
sPath = Replace(sPathAndFile, sFile, "")
ReturnThePath = sPath
End Function
-
Jan 7th, 2020, 12:18 AM
#4
Re: How to get just the pathway from a pathway and a filename? :)
Bobbles
but what will happen if the path is C:\GAMES\SOMEDATA\GAME
and the filename is GAME?
will the result be:
C:\S\SOMEDATA\
I think its better to use the reverse instr.
-
Jan 7th, 2020, 11:48 AM
#5
Re: How to get just the pathway from a pathway and a filename? :)
I've always just used InStrRev(s, "\") for years, and I've never had a problem with it. However, I'll admit that I'm always very careful to only use it when I know I've got a full specification string (path and filename). I've sometimes wondered what would happen if I had a full-spec with a file in the root, maybe something like "C:MyFile.txt", but I just never do that. And, that would be better specified as "C:\MyFile.txt", in which case the InStrRev(s, "\") would work.
If you want something that will work even when the filename may not be part of the string, then you've got other problems: 1) is terminating back-slash present?, 2) is it a filename without an extension?, 3) would it be possible to check to see if the path exists as part of the function?, 4) is speed important?
Last edited by Elroy; Jan 7th, 2020 at 11:51 AM.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Jan 7th, 2020, 08:12 PM
#6
Thread Starter
PowerPoster
Re: How to get just the pathway from a pathway and a filename? :)
Thank you all for your help!
-
Jan 7th, 2020, 09:50 PM
#7
Re: [RESOLVED] How to get just the pathway from a pathway and a filename? :)
Just to throw it out there you could use the method in the OP and then check the length of the returned filename and use Left$() to capture up to that position giving you everything but the filename.
You could also rebuild the string from your split array and just omit the last element
That said I would use the InstrRev() method myself.
-
Jan 7th, 2020, 10:09 PM
#8
Re: [RESOLVED] How to get just the pathway from a pathway and a filename? :)
In the original Dirty Harry movie.
Harry has walked up the street shooting off approx half a dozen shots.
One of the robbers lies injured on the footpath.
As Harry stands over him, the robber's hand inches towards his gun near him.
Harry still pointing his gun at him says -
I know what you're thinking. 'Did he fire six shots or only five'? Well to tell you the truth, in all this excitement, i kind of lost track myself. But being that this is a .44 Magnum, the most powerful handgun in the world, and would blow your head clean off, you've got to ask yourself one question: 'Do I feel lucky?' Well do ya, punk?
More importantly, the robber replies - "I gots to know"
I gots to know - Which solution did you go for ?
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
|