Re: Help with Mid Function
You can do this a few ways, one method:
Code:
Dim strPart As String, iPos As Integer
iPos = InStrRev(Text, "\") ' find last backslash
strPart = Mid$(Text, iPos + 1)
iPos=InStrRev(strPart, ".") find last dot
If iPos Then strPart = Left$(strPart, iPos -1)
' the result is in strPart
Edited: If you want the entire file name, less the path, the first 3 of the 5 lines apply only.
Re: Help with Mid Function
add a textbox and 2 command buttons
Code:
Dim FileName, FileTitle
Private Sub Command1_Click() 'FileTitle with extension
FileName = Text1.Text
For i = Len(FileName) To 1 Step -1
If InStr(i, FileName, "\") <> 0 Then
FileTitle = Right(FileName, (Len(FileName) - i))
Exit For
End If
Next
MsgBox FileTitle
End Sub
Private Sub Command2_Click() 'FileTitle with out extension
FileName = Left(Text1.Text, Len(Text1.Text) - 4)
For i = Len(FileName) To 1 Step -1
If InStr(i, FileName, "\") <> 0 Then
FileTitle = Right(FileName, (Len(FileName) - i))
Exit For
End If
Next
MsgBox FileTitle
End Sub
Private Sub Form_Load()
Text1.Text = "c:\test\test1.txt"
End Sub
Re: Help with Mid Function
One more way....
vb Code:
Sub Sample()
Dim Strg As String, FirstArray() As String, SecondArray() As String
Strg = "\\mysvr\tsccomm\NonPublish\HelpSite\File_Expirations\Date_Teli.doc"
FirstArray = Split(Strg, "\")
'~~> This will give you Date_Teli.doc
MsgBox FirstArray(UBound(FirstArray))
SecondArray = Split(FirstArray(UBound(FirstArray)), ".")
'~~> This will give you Date_Teli
MsgBox SecondArray(0)
End Sub
Re: Help with Mid Function
Code:
Private Sub Command1_Click()
MsgBox ExtractFileTitle("d:\test\test1.txt")
End Sub
Private Function ExtractFileTitle(StrPath As String) As String
ExtractFileTitle = Left(StrPath, Len(StrPath) - 4) 'remove extension name and dot(3+1 chars)
For i = Len(ExtractFileTitle) To 1 Step -1
If InStr(i, ExtractFileTitle, "\") <> 0 Then
ExtractFileTitle = Right(ExtractFileTitle, (Len(ExtractFileTitle) - i))
Exit For
End If
Next
End Function
same thing just like a function
Re: Help with Mid Function
Thanks for all the suggestions, I am really trying to get this to work using VBSCRIPT & ASP. Not sure I really should be posting in this forum.
I currently have it so that I can get all the characters after the 47th position on....
I would really like to get all the characters in between the last "\" and "." since the number of characters will be different each time.
My problem is trying to incorporate your suggestions into this code.
Code:
<%
Do While Not rstSearch.EOF
%>
<tr>
<td><%= rstSearch.Fields("type").Value %></td>
<td><a href="<%= rstSearch.Fields("file_name").Value %>" target="_blank"><%=
mid(rstSearch.Fields("file_name").Value,47,len(rstSearch.Fields("file_name").Value)) %></a></td>
</tr>
<%
rstSearch.MoveNext
Loop
%>
Sample Data:
Original file_name.value
\\serverpath\directory\NonPublish\HelpDesk\now\CertificateExpiration.doc
Modified file_name.value using above code
CertificateExpiration.doc
Re: Help with Mid Function
Quote:
Originally Posted by
qman
I am really trying to get this to work using VBSCRIPT & ASP. Not sure I really should be posting in this forum.
It was the wrong forum (there are many differences between VB6 and VBScript), so I've moved the thread to our "ASP, VB Script" forum.
Re: Help with Mid Function
See if this works for you
Code:
<%
Do While Not rstSearch.EOF
Dim strFile, strFileName, strParts
'Get the filename
strFile = rstSearch.Fields("file_name").Value
'Split it up on the backslashes
strParts = Split(strFile, "\")
'Grab the last element of the array
strFileName = strParts(UBound(strParts))
%>
<tr>
<td><%= rstSearch.Fields("type").Value %></td>
<td><a href="<%= strFile %>" target="_blank"><%= strFileName %></a></td>
</tr>
<%
rstSearch.MoveNext
Loop
%>