I need to split a file path like this C:\Program Files\Flintstones\fred.txt into two cells, one containing C:\Program Files\Flintstones and the other containing fred.txt.
I came across this solution at CodeGeek
To find the directory:
=LEFT(B1, FIND("*", SUBSTITUTE(B1, "\", "*", LEN(B1) - LEN(SUBSTITUTE(B1, "\", "" )))) - 1)
Lets break it down:
1) since * cannot occur in a file name we can replace the \ with *.
2) this allows us to calculate how many \es there are in the string.
2.1) replace all \ with an empty string - SUBSTITUTE(B1, "\", "" )
2.2) count how long the string is without all the \es - LEN(SUBSTITUTE(B1, "\", "" ))
2.3) subtract that from the length of the string - LEN(B1) - LEN(SUBSTITUTE(B1, "\", "" ))
3) now replace only the last one (n-th occurence, where n is the value found in 2.3) with a * by using SUBSTITUTE's 4 parameter form. - SUBSTITUTE(B1, "\", "*", LEN(B1) - LEN(SUBSTITUTE(B1, "\", "" )))
4) then find the one we replaced - FIND("*", SUBSTITUTE(B1, "\", "*", LEN(B1) - LEN(SUBSTITUTE(B1, "\", "" ))))
5) and get only the text to the left of it. - LEFT(B1, FIND("*", SUBSTITUTE(B1, "\", "*", LEN(B1) - LEN(SUBSTITUTE(B1, "\", "" )))) - 1)
To find the file:
=RIGHT(B1, LEN(B1) - FIND("*", SUBSTITUTE(B1, "\", "*", LEN(B1) - LEN(SUBSTITUTE(B1, "\", "")))))
References:
http://code-geek.blogspot.com/2010/05/find-last-character-in-excel.html
No comments:
Post a Comment