-
Feb 21st, 2003, 03:35 PM
#1
Thread Starter
PowerPoster
VB Snippet - File Name without Extension
VB Code:
Public Function FileNameWOExt(FileName As String) As String
Dim i As Integer
Dim lpFileName As String
On Error Resume Next
For i = Len(FileName) To 1 Step -1
If Mid(FileName, i, 1) = "\" Then
Exit For
End If
Next
lpFileName = Mid$(FileName, i + 1)
i = InStr(Mid(FileName, i + 1), ".")
If i > 1 Then
GetFileName = Mid(lpFileName, 1, i - 1)
End If
End Function
Remaining quiet down here !!!
BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....
-
Feb 22nd, 2003, 04:39 PM
#2
Fanatic Member
James,
I think you need to change GetFileName to FileNameWOExt because your function is not working properly.
Also, I like to point out something and hope you don't mind. WindowXP and most of the OS now a day allows period in a file name. For example, My.Admin.asp. Using your function, against a file like that, will only return "My".
Here are two alternatives.
Code:
Private Sub Form_Load()
'Alternative 1
MsgBox FileNameWithoutExt("Z:\Update\Done\My.Admin.asp")
'Alternative 2
'Dim fso As New FileSystemObject
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
MsgBox fso.GetBaseName("Z:\Update\Done\My.Admin.asp")
End Sub
Function FileNameWithoutExt(strFileName As String) As String
Dim intBackSlash As Integer
Dim intPeriod As Integer
intBackSlash = InStrRev(strFileName, "\", Len(strFileName), vbTextCompare) + 1
intPeriod = InStrRev(strFileName, ".", Len(strFileName), vbTextCompare)
If intPeriod = 0 Then intPeriod = Len(strFileName) + 1 'Incase there is no extension
FileNameWithoutExt = Mid(strFileName, intBackSlash, intPeriod - intBackSlash)
End Function
Bird of Prey
Mr. Bald Eagle.
[img][/img]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|