|
-
Oct 7th, 2009, 09:28 AM
#1
Thread Starter
Junior Member
Help with Mid Function
Ideally, I am trying to retrieve the characters between "\" and "." while then adding everything after "."
Original
text = "\\mysvr\tsccomm\NonPublish\HelpSite\File_Expirations\Date_Teli.doc"
In real world, I will not know the name of the document being displayed in "text".
After the retrieval, text would be set to:
text = "Date_Teli.doc"
Hope this makes sense....
-
Oct 7th, 2009, 09:48 AM
#2
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.
Last edited by LaVolpe; Oct 7th, 2009 at 01:07 PM.
-
Oct 7th, 2009, 10:57 AM
#3
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
-
Oct 7th, 2009, 11:02 AM
#4
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
Last edited by Siddharth Rout; Oct 7th, 2009 at 11:13 AM.
Reason: Typo
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Oct 7th, 2009, 11:34 AM
#5
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
-
Oct 7th, 2009, 01:01 PM
#6
Thread Starter
Junior Member
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
Last edited by Hack; Oct 7th, 2009 at 01:51 PM.
Reason: Added Code Tags
-
Oct 7th, 2009, 01:08 PM
#7
Re: Help with Mid Function
 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.
-
Oct 8th, 2009, 07:09 AM
#8
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
%>
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
|