I want to select a path only from a fully qualified name.
我想只从完全限定的名称中选择一个路径。
For example, C:\Newfolder\iTDC.mdb
is shown in in the text box.
例如,C:\ Newfolder \ iTDC.mdb显示在文本框中。
But I want to take only C:\Newfolder
, removing the iTDC.mdb
.
但我只想采取C:\ Newfolder,删除iTDC.mdb。
How can skip the file?
怎么可以跳过文件?
4 个解决方案
#1
Quick and dirty
又脏又脏
Dim sPath As String
sPath = "C:\Newfolder\iTDC.mdb"
sPath = Left(sPath, InStrRev(sPath, "\"))
#2
If you add a reference to the Microsoft Scripting Runtime (using Project->References), then you can use the FileSystemObject
to do file-related operations. For example:
如果添加对Microsoft Scripting Runtime的引用(使用Project-> References),则可以使用FileSystemObject执行与文件相关的操作。例如:
Dim oFSO as New FileSystemObject
strFolder = oFSO.GetFolder(strPath)
The FileSystemObject
also has other useful methods for composing paths (BuildPath
) and for testing for the existence of files, folders, etc. (FileExists
, FolderExists
).
FileSystemObject还有其他有用的方法来组合路径(BuildPath)和测试文件,文件夹等的存在(FileExists,FolderExists)。
#3
You can use the PathRemoveFileSpec function, available in every version of Windows from 2000 and 98. Here is a VB6 implementation.
您可以使用PathRemoveFileSpec函数,该函数可用于2000和98的每个Windows版本。这是一个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
I prefer to avoid the Microsoft Scripting Runtime (including FileSystemObject). In my experience it's occasionally broken on user machines, perhaps because their IT department are paranoid about viruses. There are other useful functions in shlwapi.dll, e.g. for testing whether folders exist or files exist.
我更喜欢避免使用Microsoft Scripting Runtime(包括FileSystemObject)。根据我的经验,它偶尔会在用户计算机上崩溃,可能是因为他们的IT部门对病毒感到偏执。 shlwapi.dll还有其他有用的功能,例如:用于测试文件夹是否存在或文件是否存在
#4
' 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
#1
Quick and dirty
又脏又脏
Dim sPath As String
sPath = "C:\Newfolder\iTDC.mdb"
sPath = Left(sPath, InStrRev(sPath, "\"))
#2
If you add a reference to the Microsoft Scripting Runtime (using Project->References), then you can use the FileSystemObject
to do file-related operations. For example:
如果添加对Microsoft Scripting Runtime的引用(使用Project-> References),则可以使用FileSystemObject执行与文件相关的操作。例如:
Dim oFSO as New FileSystemObject
strFolder = oFSO.GetFolder(strPath)
The FileSystemObject
also has other useful methods for composing paths (BuildPath
) and for testing for the existence of files, folders, etc. (FileExists
, FolderExists
).
FileSystemObject还有其他有用的方法来组合路径(BuildPath)和测试文件,文件夹等的存在(FileExists,FolderExists)。
#3
You can use the PathRemoveFileSpec function, available in every version of Windows from 2000 and 98. Here is a VB6 implementation.
您可以使用PathRemoveFileSpec函数,该函数可用于2000和98的每个Windows版本。这是一个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
I prefer to avoid the Microsoft Scripting Runtime (including FileSystemObject). In my experience it's occasionally broken on user machines, perhaps because their IT department are paranoid about viruses. There are other useful functions in shlwapi.dll, e.g. for testing whether folders exist or files exist.
我更喜欢避免使用Microsoft Scripting Runtime(包括FileSystemObject)。根据我的经验,它偶尔会在用户计算机上崩溃,可能是因为他们的IT部门对病毒感到偏执。 shlwapi.dll还有其他有用的功能,例如:用于测试文件夹是否存在或文件是否存在
#4
' 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