Results 1 to 8 of 8

Thread: [RESOLVED] How to get just the pathway from a pathway and a filename? :)

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Resolved [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:
    1. Dim strPath as string
    2. Dim strFile as string
    3. Dim UB as string
    4.  
    5.     strPath = LBLPath.Caption
    6.     strFile = Split(strPath, "")
    7.     UB = UBound(strFile)
    8.  
    9.     MsgBox strFile(UB)
    10. 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!!

  2. #2
    Fanatic Member
    Join Date
    Feb 2019
    Posts
    924

    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.

  3. #3
    Frenzied Member
    Join Date
    Dec 2008
    Location
    Melbourne Australia
    Posts
    1,487

    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

  4. #4
    The Idiot
    Join Date
    Dec 2014
    Posts
    3,013

    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.

  5. #5
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,915

    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.

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: How to get just the pathway from a pathway and a filename? :)

    Thank you all for your help!

  7. #7
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    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.

  8. #8
    Frenzied Member
    Join Date
    Dec 2008
    Location
    Melbourne Australia
    Posts
    1,487

    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
  •  



Click Here to Expand Forum to Full Width