+ Reply to Thread
Results 1 to 4 of 4

Extract a text string from between the 3rd last pair of backslashes (from a dir tree)?

  1. #1
    Registered User
    Join Date
    02-12-2012
    Location
    Victoria
    MS-Off Ver
    Excel 2010, Excel 2013/64
    Posts
    23

    Question Extract a text string from between the 3rd last pair of backslashes (from a dir tree)?

    Hi all,

    First of all, thanks to anyone who can help.
    I sort of have this working, but it is *really* messy. I've hacked it together from a couple of useful posts from help forums online (the main one from here, but my modifications have made it really dirty.

    I'm using a macro I found online to provide me with a directory tree. The directories are listed in column A, and I want to grab a string from those cells. Specifically, I want to grab the 3rd directory back from the end. e.g.

    A1
    c:\this\is\a\variable\tree\with\manyvariable\directory\names

    should return "manyvariable"
    in C1

    The number of directories in the tree varies, so counting between pairs of backslashes from the left won't fly. The one thing that *is* fixed, is that I always need the 3rd last directory as a string.

    I've got it going in a rather convoluted manner. Because the way I'm doing it, I can't put the answer in C1, and it requires 3 cells to do the calculation in stages.

    In C1 I have:
    Please Login or Register  to view this content.
    which strips off the last directory, and the terminal rightmost character that remains (which happens to be a backslash).
    i.e. c:\this\is\a\variable\tree\with\manyvariable\directory

    Then in D1 I have:
    Please Login or Register  to view this content.
    which is basically a repetition of the last function.
    i.e. c:\this\is\a\variable\tree\with\manyvariable

    Then in E1 I have:
    Please Login or Register  to view this content.
    This returns "manyvariable". Finally.

    But boy, it is messy. I tried nesting all the code, but Excel 2010 has a hissy fit.. it's too big I think.

    Now I'm more than happy to use an excel VB macro to do this, because there must be a smarter way!

    Any ideas would be greatly appreciated!

    best regards.
    Last edited by kabammi; 07-09-2012 at 11:13 PM. Reason: just made it a little clearer

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Extract a text string from between the 3rd last pair of backslashes (from a dir tree)?

    If there are no spaces in your folder names, this works:

    =LEFT(SUBSTITUTE(TRIM(RIGHT(SUBSTITUTE(A1, "\", REPT(" ", 100)), 300))," ", "\"), FIND("\", SUBSTITUTE(TRIM(RIGHT(SUBSTITUTE(A1, "\", REPT(" ", 100)), 300))," ", "\"))-1)

    If there are spaces in your pathnames, a little uglier:

    =SUBSTITUTE(LEFT(SUBSTITUTE(TRIM(RIGHT(SUBSTITUTE(SUBSTITUTE(A1," ","^"), "\", REPT(" ", 100)), 300))," ", "\"), FIND("\", SUBSTITUTE(TRIM(RIGHT(SUBSTITUTE(SUBSTITUTE(A1," ","^"), "\", REPT(" ", 100)), 300))," ", "\"))-1),"^"," ")
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Expert RobertMika's Avatar
    Join Date
    06-22-2009
    Location
    Haverhill, UK
    MS-Off Ver
    Excel 2003-13
    Posts
    1,530

    Re: Extract a text string from between the 3rd last pair of backslashes (from a dir tree)?

    =trim(left(right(substitute("\"&a1,"\",rept(" ",200)),600),200))

  4. #4
    Registered User
    Join Date
    02-12-2012
    Location
    Victoria
    MS-Off Ver
    Excel 2010, Excel 2013/64
    Posts
    23

    Re: Extract a text string from between the 3rd last pair of backslashes (from a dir tree)?

    Thanks!

    Both methods seem to work for me (yes JBeaucaire, often the directories do have spaces - and your second method works well).

    dewilk wins the prize for most succinct!

    cheers!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1