如何截取文件名,而去掉后缀?

kurt_hu 2000-08-17 12:13:00
请问:如何截取文件名,而去掉后缀?如:file1.txt,我只要file1即可。
...全文
1252 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
honey 2001-04-17
  • 打赏
  • 举报
回复
这个问题看似简单,其实不然呀!如果是8.3格式的文件到还好解决,假如是长文件名就不好处理了.特别是涉及到中文文件名,中文在VB各个版本的的长度也不一样.
ttyp 2001-04-17
  • 打赏
  • 举报
回复
'文件名前缀
Function GetFileName(ByVal FileName As String) As String
Dim DotIndex As Long
DotIndex = InStrRev(FileName, ".")
If DotIndex = 0 Then
GetFileName = ""
Else
GetFileName = Left(FileName, DotIndex - 1)
End If
End Function

'文件名后缀
Function GetFileNameTail(ByVal FileName As String) As String
Dim DotIndex As Long
DotIndex = InStrRev(FileName, ".")
If DotIndex = 0 Then
GetFileNameTail = ""
Else
GetFileNameTail = mid(FileName, DotIndex+1)
End If
End Function
jamezj 2001-04-17
  • 打赏
  • 举报
回复
用Mid()函数截取字符串
Jackyin 2001-04-17
  • 打赏
  • 举报
回复

vb.net 都快出了,你们还用 5.0??
shines77 2001-04-17
  • 打赏
  • 举报
回复
不过可以自己写一个来模拟,嘻嘻,我就有:)
shines77 2001-04-17
  • 打赏
  • 举报
回复
InStrRev()好象在VB6.0才有,Vb5.0没有的。
Jackyin 2001-04-17
  • 打赏
  • 举报
回复
Try this:
strFileName = "aaaaaaaaaa.bbb.txt"
intPos = InStrRev(strFileName, ".")
strGet = Left(strFileName, intPos - 1)
wxz 2000-08-19
  • 打赏
  • 举报
回复
shines较好,但没有考虑一些特需情况,如
aaaaaaaaaa.exe.bak
所以查找.应该从右边开始,其实用API最简单
fuzhanyu 2000-08-19
  • 打赏
  • 举报
回复
mid(file.???,1,len(file.???)-4)
fuzhanyu 2000-08-19
  • 打赏
  • 举报
回复
mid(file.???,1,len(file.***)-4)
shines77 2000-08-19
  • 打赏
  • 举报
回复
wxz: 对,其实应该判断最后一个"."的,不过用API也很简单,用GetFileTitle(),如下:

Option Explicit

Private Declare Function GetFileTitle Lib "comdlg32.dll" Alias "GetFileTitleA" _
(ByVal lpszFile As String, ByVal lpszTitle As String, ByVal cbBuf As Integer) As Integer

Private Sub Form_Load()
Dim test As String
test = GetFileNameTitle("aaaaaaaaaa.exe")
End Sub

Function GetFileNameTitle(ByVal PathName As String)
Dim Buffer As String, Ret As Integer
Dim bb As Integer
'Create a buffer
Buffer = Space(255)
'Get the file title
Ret = GetFileTitle(PathName, Buffer, 255)
'Buffer left
Buffer = Left(Buffer, InStr(1, Buffer, Chr$(0)) - 1)
'Get the result
If Ret = 0 Then
GetFileNameTitle = Buffer
Else
GetFileNameTitle = PathName
End If
'Show the result
MsgBox "文件名是: " & GetFileNameTitle, vbInformation
End Function

不过我发现API也解决不了"aaaaaaa.exe.bak"的问题,他竟然返回"aaaaaaa.exe.bak",看来还是自己写的好(我在win2000上)。
lysccd 2000-08-17
  • 打赏
  • 举报
回复
VB应该提供这种功能,如果没有话,那就太臭了。建议你不要用VB了
gx_sunny 2000-08-17
  • 打赏
  • 举报
回复
Mid()函数!
Firing_Sky 2000-08-17
  • 打赏
  • 举报
回复
用一个循环来逐字读取文件名,直到碰到第一个“.”号为止,很简单的啊!
shines77 2000-08-17
  • 打赏
  • 举报
回复
可以用InStr()函数,当然我知道用API函数也可以,我也知道做,
不过没必要,请看下面的程序:

Option Explicit

Private Sub Form_Load()
Dim Files As String
Dim FilesTail As String
Const tmFiles = "MyFiles.Txt"
Files = GetFileName(tmFiles)
FilesTail = GetFileNameTail(tmFiles)
Me.AutoRedraw = True
Me.Print
Me.Print " 原始文件名: "; tmFiles
Me.Print " 文件名为: "; Files
Me.Print " 后缀名为: "; FilesTail
End Sub

'文件名前缀
Function GetFileName(ByVal FileName As String) As String
Dim DotIndex As Long
DotIndex = InStr(1, FileName, ".")
If DotIndex = 0 Then
GetFileName = ""
Else
GetFileName = Left(FileName, DotIndex - 1)
End If
End Function

'文件名后缀
Function GetFileNameTail(ByVal FileName As String) As String
Dim DotIndex As Long
DotIndex = InStr(1, FileName, ".")
If DotIndex = 0 Then
GetFileNameTail = ""
Else
GetFileNameTail = Right(FileName, Len(FileName) - DotIndex)
End If
End Function

我帮你还做了一个获得文件名后缀的函数,中文名同样使用哦(win2000上,win98还没测试)。
Un1 2000-08-17
  • 打赏
  • 举报
回复
Public Function GetFileMainName(FilePath As String) As String
Dim l As Long
l = InStrRev(FilePath, ".")

If l > 0 Then
If InStr(l, FilePath, "\") < l Then
GetFileMainName = Left$(FilePath, l - 1)
End If
End If

If Len(GetFileMainName) <= 0 Then GetFileMainName = FilePath
End Function


7,762

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧