Results 1 to 8 of 8

Thread: Help with Mid Function

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2008
    Posts
    23

    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....

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    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
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


  4. #4
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Help with Mid Function

    One more way....

    vb Code:
    1. Sub Sample()
    2.     Dim Strg As String, FirstArray() As String, SecondArray() As String
    3.    
    4.     Strg = "\\mysvr\tsccomm\NonPublish\HelpSite\File_Expirations\Date_Teli.doc"
    5.    
    6.     FirstArray = Split(Strg, "\")
    7.    
    8.     '~~> This will give you Date_Teli.doc
    9.     MsgBox FirstArray(UBound(FirstArray))
    10.    
    11.     SecondArray = Split(FirstArray(UBound(FirstArray)), ".")
    12.    
    13.     '~~> This will give you Date_Teli
    14.     MsgBox SecondArray(0)
    15. 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

  5. #5
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    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
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jun 2008
    Posts
    23

    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:
    <&#37;
      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

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Help with Mid Function

    Quote Originally Posted by qman View Post
    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.

  8. #8
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: Help with Mid Function

    See if this works for you
    Code:
    <&#37;
    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
  •  



Click Here to Expand Forum to Full Width