How to extract a directory from a file path string?

I want to choose a path with only a full name.

For example, C:\Newfolder\iTDC.mdbdisplayed in a text box.

But I want to take only C:\Newfolderby deleting iTDC.mdb.

How to skip a file?

+2
source share
4 answers

Quick and dirty

Dim sPath As String

sPath = "C:\Newfolder\iTDC.mdb"

sPath = Left(sPath, InStrRev(sPath, "\"))
+8
source

If you add a link to the Microsoft Scripting Runtime executable (using Project-> References), you can use it FileSystemObjectto perform file related operations. For instance:

Dim oFSO as New FileSystemObject

strFolder = oFSO.GetFolder(strPath)

FileSystemObjectalso has other useful methods for linking paths ( BuildPath) and for testing for files, folders, etc. ( FileExists, FolderExists).

+5
source

PathRemoveFileSpec, Windows 2000 98. VB6.

Private Declare Function PathRemoveFileSpec Lib "Shlwapi" _
  Alias "PathRemoveFileSpecW" (ByVal szPath As Long) As Long

'Convert input file path to drive & directory only. (Supports UNC too) '    
Function sPathOnly(ByVal sInput As String) As String
  Dim sWorking As String
  sWorking = sInput
  If (PathRemoveFileSpec(StrPtr(sWorking)) <> 0) Then
    'Call succeeded. Trim trailing Null '
    sPathOnly = sTrimNull(sWorking)
  Else
    sPathOnly = sWorking
  End If
End Function

'Trim trailing null characters (e.g. from a string returned from an API call) '
Function sTrimNull(ByVal sIn As String) As String
  Dim iZeroCharacter As Long
  iZeroCharacter = InStr(sIn, Chr$(0))
  If iZeroCharacter > 0 Then
    sTrimNull = Left$(sIn, iZeroCharacter - 1)
  Else
    sTrimNull = sIn
  End If
End Function

Runtime Microsoft Scripting ( FileSystemObject). , , , , - . shlwapi.dll, . .

+3
' GetFilenameWithoutExtension:  Return filename without extension from complete path
Public Function GetFilenameWithoutExtension(path As String) As String
    Dim pos As Integer
    Dim filename As String
    pos = InStrRev(path, "\")
    If pos > 0 Then
        filename = Mid$(path, pos + 1, Len(path))
        GetFilenameWithoutExtension = Left(filename, Len(filename) - Len(Mid$(filename, InStrRev(filename, "."), Len(filename))))
    Else
        GetFilenameWithoutExtension = ""
    End If
End Function

' GetFilenameWithExtension: Return filename with extension from complete path
Public Function GetFilenameWithExtension(path As String) As String
    Dim pos As Integer
    pos = InStrRev(path, "\")
    If pos > 0 Then
        GetFilenameWithExtension = Mid$(path, pos + 1, Len(path))
    Else
        GetFilenameWithExtension = ""
    End If
End Function


' GetDirectoryFromPathFilename: Return directory path contain filename
Public Function GetDirectoryFromPathFilename(path As String) As String
    Dim pos As Integer
    pos = InStrRev(path, "\")
    If pos > 0 Then
        GetDirectoryFromPathFilename = Left$(path, pos)
    Else
        GetDirectoryFromPathFilename = ""
    End If
End Function
0

Source: https://habr.com/ru/post/1745700/


All Articles