Разделить пути к папкам в Excel, чтобы вернуть окончательную папку

5934
BobJim

У меня есть один столбец в Excel, который имеет путь к файлу и папке. например, C: \ 1_Folder \ 2_Folder \ 3_Folder \ my_file.txt

Я хотел бы извлечь имя конечной папки и поместить его в новый столбец. В этом примере 3_Folder.

Может ли это быть достигнуто с помощью формулы, а не VBA?

Изменить: количество вложенных папок может варьироваться.

3

3 ответа на вопрос

2
Todd

You can use the FIND and MID text functions. This will work for variable number of folders

Path text C:\1_Folder\2_Folder\3_Folder\my_file.txt

Find position of next to last slash (B1): FIND("|",SUBSTITUTE(A1,"\","|",LEN(A1)-LEN(SUBSTITUTE(A1,"\",""))-1))

Find position of last slash (C1): FIND("\",A1,B1+1)

Get the characters between next to last and last slash: MID(A1,B1+1,(C1-B1)-1)

Похоже, что он работает максимум с тремя вложенными папками. В некоторых случаях у меня есть 7 вложенных папок. В этих случаях эти формулы не работают? BobJim 8 лет назад 0
Я просто написал это на основе вашего примера. Я изменил свой ответ для переменного количества папок. Todd 8 лет назад 0
Спасибо за обновление вашей формулы. Почему формулы для ячеек C1 и D1 ссылаются на C15? BobJim 8 лет назад 0
Проверь, исправил. Todd 8 лет назад 1
2
Máté Juhász

I use regex addin for tasks like this, with regular expression:

=RegExReplace(A1,".*\\([^\\]*)\\[^\\]*","$1") - this extracts the substring before the last \ (practically the last folder as you need)

enter image description here

Это довольно гладко. Было бы неплохо, если бы Excel поддерживал регулярные выражения изначально. Todd 8 лет назад 0
0
GuitarPicker

This answer is based on the accepted answer to a very similar question.

If you want it in one formula, you can use:

=RIGHT(A6,LEN(A6)+1-FIND("@",SUBSTITUTE("\"&A6,"\","@",(LEN(A6)-LEN(SUBSTITUTE("\"&A6,"\",""))+1)))) 

... which basically calculates the number of backslashes by removing them all and comparing lengths. It uses that number to substitutes the last occurrence with a "@" and then turns back around and finds the position of the "@" which it feeds into the RIGHT formula to grab the substring trailing the last backslash. An extra backslash and a "+1" are added in to handle the case of zero backslashes in the source. If there are no backslashes, the original string is returned.

If your data already contains "@" then you will need to pick a different substitution character.

Похожие вопросы