使用单元格公式从文件路径获取父文件夹路径 [英] Get parent folder path from file path using cell formula
问题描述
在 A
的列中,我有20000行,文件名为文件路径
In column A
I have 20000 rows with filename with file path
C:\person\microsoft\ygkyg\mmddyy\filename.xls
\server-41\performance\mmddyy\filename.doc
.....
等。
"C:\person\microsoft\ygkyg\mmddyy\filename.xls"
"\server-41\performance\mmddyy\filename.doc"
.....
etc.
在列 B
我只想得到父文件夹路径。
In column B
I just want to get the parent folder path.
有人可以帮助我配方吗?我尝试过这个,但它给了我的文件名。
Could someone help me with the formula? I tried this but it's giving me the file name.
=MID(a1,FIND(CHAR(1),
SUBSTITUTE(a1,"\",CHAR(1),LEN(a1)-LEN(SUBSTITUTE(a1,"\",""))))+1,LEN(a1))
推荐答案
这个工作。
= MID(A1,1,LEN(A1)-LEN(MID(A1,FIND(CHAR(1),替代(A1, \,CHAR(1),LEN(A1 )-LEN(SUBSTITUTE(A1,\,))))+ 1,LEN(A1))))
Neil 稍微简化了表达,并将其发布为以下评论:
The above was my original answer. Neil simplified the expression somewhat and posted this as a comment below:
=LEFT(A1,FIND("?",SUBSTITUTE(A1,"\","?",LEN(A1)-LEN(SUBSTITUTE(A1,"\","")))))
这充分利用了 ?
是路径中的禁止字符,以便?
可以安全地使用,而不是 CHAR(1) code>作为地标,从而提高可读性。另外,
所以使用 LEFT(A1,x)
相当于&code> MID(A1,1,x) LEFT
是有意义的。但最重要的是,该公式使用 FIND
,而不是使用 LEN
的第二层计数字符。这使得它更可读。
This takes advantage of the fact that ?
is a forbidden character in paths so that "?"
can safely be used instead of CHAR(1)
as a placemark, thus improving readability a little bit. Also, LEFT(A1,x)
is equivalent to, and shorter than MID(A1,1,x)
, so it makes sense to use LEFT
. But most importantly, this formula makes use of FIND
, instead of a second layer of counting characters using LEN
. This makes it much more readable.
这篇关于使用单元格公式从文件路径获取父文件夹路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!