Filenames and Directorys

Posted on 2003-03-16
Medium Priority
Last Modified: 2010-05-01
looking for a snippet of code to get the folder path of a file i have the full path for..
"c:\this is a fake folder\document.zzz"
and i want
"c:\this is a fake folder"

or something the chdir function wont bitch at!
Question by:gthgb
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

robertlees earned 500 total points
ID: 8146235
Try this...

Function GetPathName(ByVal strString as string) as string
   dim n as integer

   n = InStrRev(strString, "\")
   GetPathName = left(strString, n-1)
End Function

Then call it like this...
   chdir GetPathName("c:\Fake Folder\Document.zzz")

Expert Comment

ID: 8146253
Here is the code to do so:

Private Const gstrSEP_DIR$ = "\"                         ' Directory separator character
Private Const gstrSEP_DRIVE$ = ":"                       ' Driver separater character, e.g., C:\
Private Const gstrSEP_DIRALT$ = "/"                      ' Alternate directory separator character
Public Path As String
Public Filename As String

Private Sub Command1_Click()
SeparatePathAndFileName "D:\BootDisk\level0\utils.cab"
End Sub

'Given a fully qualified filename, returns the path portion and the file
'   portion.

Public Sub SeparatePathAndFileName(FullPath As String, _
    Optional ByRef Path As String, _
    Optional ByRef Filename As String)

    Dim nSepPos As Long
    Dim nSepPos2 As Long
    Dim fUsingDriveSep As Boolean

    nSepPos = InStrRev(FullPath, gstrSEP_DIR)
    nSepPos2 = InStrRev(FullPath, gstrSEP_DIRALT)
    If nSepPos2 > nSepPos Then
        nSepPos = nSepPos2
    End If
    nSepPos2 = InStrRev(FullPath, gstrSEP_DRIVE)
    If nSepPos2 > nSepPos Then
        nSepPos = nSepPos2
        fUsingDriveSep = True
    End If

    If nSepPos = 0 Then
        'Separator was not found.
        Path = CurDir$
        Filename = FullPath
        If fUsingDriveSep Then
            Path = Left$(FullPath, nSepPos)
            Path = Left$(FullPath, nSepPos - 1)
        End If
        Filename = Mid$(FullPath, nSepPos + 1)
    End If
End Sub

Expert Comment

ID: 8147500
Dir ("c:\wherever\somewhereelse\aFile.txt") will return just the file name: 'myFile.txt'. Sine you need the path, not the file name, you can remove the file name string from the complete path. Do this either by length:

myfile = "c:\wherever\somewhereelse\aFile.txt"
MsgBox Mid$(myfile, 1, Len(myfile) - Len(Dir(myfile)) - 1)

...Or simply by using a replace:

MsgBox Replace(myfile, "\" & Dir(myfile), "")

Kindest regards,

Expert Comment

ID: 8147866
here is function I've been using:

Function GetFoldersOnly(ascFullPath As String, Optional bKeepLastSlash As Boolean = False) As String
    Dim iPos As Integer
    Dim iLastPos As Integer
    iPos = InStr(1, ascFullPath, "\")
    Do While iPos > 0
        iLastPos = iPos
        iPos = InStr(iPos + 1, ascFullPath, "\")
    If Not bKeepLastSlash Then iLastPos = iLastPos - 1
    GetFoldersOnly = Left(ascFullPath, iLastPos)
End Function

Author Comment

ID: 8151397
hmm im sure i pressed that accept comment as answer button ages ago.. ahh well..
thanks to everyone that posted :)

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question