VB如何提取路径中的文件名?

如:将text1.text中路径中的文件名jiangxi 赋给text2.text。谢谢各位!
2024-11-29 06:50:20
推荐回答(5个)
回答1:

楼上的比我的好,建议你多学习一下字符串截取函数,right left mid nstr
Text1是文件路径 Text2是提取的文件名
我写的代码如下:
Private Sub Command1_Click()
Dim i As Integer, n As Integer
For i = 1 To Len(Text1)
If Mid(Text1, i, 1) = "\" Then n = i
Next
Text2 = Right(Text1, Len(Text1) - n)
End Sub

回答2:

楼上几位够费劲的,只需要一行代码:
Text2 = CreateObject("Scripting.FileSystemObject").GetBaseName(Text1)

回答3:

Private Sub Command1_Click()
Text2 = Mid(Text1, InStrRev(Text1, "\") + 1, InStrRev(Text1, ".") - InStrRev(Text1, "\") - 1)
End Sub

回答4:

dim a () as string
a=split(text1.text,"\")
a=split(a(ubound(a)),".")
text2.text=a(ubound(a)-1)

回答5:

Text2 = Mid(Text1, InStrRev(Text1, "\") + 1, Len(Text1))