Results 1 to 7 of 7
  1. #1
    JaceyK is offline Novice
    Windows 10 Access 2016
    Join Date
    Jan 2023
    Posts
    1

    Extracting substring between two slashes in a file path

    So we are wanting to grab data between two back slashes - specifically the last back slash and the next one before that. Ex: C:\data\folder\text\file\document_xlsx\ - we want to extract document_xlsx into another field. I know I can use Update query and instrrev function but I am just not getting what I need - guessing its more complex? Oh and it will vary in length, obviously. Can anyone help? Please?

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    I would probably use the Split() function if not the InstrRev() starting at Len() -1
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,793
    To get the last you can use -1 but to get the one before that, you will have to calculate the next position to start from. That might be InstrRev of the Len of your path minus the number that the first instrrev returns. A lot simpler if this "C:\data\folder\text\file" is constant.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    Something like this would work:

    Expr1: Replace(Mid("C:\data\folder\text\file\document_xls x",InStrRev(Left("C:\data\folder\text\file\documen t_xlsx",Len("C:\data\folder\text\file\document_xls x")-1),"")),"","")

    If you need to apply it to a field it looks a bit better:
    Expr1: Replace(Mid([YourField],InStrRev(Left([YourField],Len([YourField])-1),"")),"","")

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  5. #5
    moke123's Avatar
    moke123 is online now Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,651
    I use something like this

    Code:
    Function fGetElement(strIN As String, strSeparator As String, Optional intSegment As Integer = 0) As String
    'intSegment is 0 based reading right to left
    
        Dim var As Variant, i As Integer
        
        If Right(Trim(strIN), 1) = strSeparator Then strIN = Left(strIN, Len(strIN) - 1)
    
        var = Split(strIN, strSeparator)
    
        i = UBound(var)
    
        fGetElement = var(i - intSegment)
    
    End Function
    Code:
    ?fGetElement("C:\data\folder\text\file\document_xlsx\","\")
    document_xlsx
    
    ?fGetElement("C:\data\folder\text\file\document_xlsx\","\",1)
    file
    
    ?fGetElement("C:\data\folder\text\file\document_xlsx\","\",2)
    text
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  6. #6
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    or in one line
    Code:
    ?split("C:\data\folder\text\file\document_xlsx\","")(ubound(split("C:\data\folder\text\file\document_xlsx\",""))-1)
    document_xlsx

  7. #7
    moke123's Avatar
    moke123 is online now Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,651
    Oops. My bad. I read the original post as they wanted the last element and the one before it.
    My function is generic where you can have any character separating the values.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

Please reply to this thread with any new information or opinions.

Similar Threads

  1. EXtracting substring from a string
    By pderwael in forum Access
    Replies: 4
    Last Post: 12-28-2021, 04:23 AM
  2. Replies: 2
    Last Post: 01-13-2015, 07:16 AM
  3. Replies: 13
    Last Post: 12-12-2013, 07:22 PM
  4. Replies: 3
    Last Post: 07-30-2012, 02:16 PM
  5. Replies: 10
    Last Post: 03-04-2012, 12:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums