使用单元格公式从文件路径获取父文件夹路径
在 A
列中,我有 20000 行,其中包含文件名和文件路径
“C:\person\microsoft\ygkyg\mmddyy\文件名.xls”
“\server-41\performance\mmddyy\文件名.doc”
......
等等
在 B
列中,我只想获取父文件夹路径。
有人可以帮我计算公式吗?我尝试了这个,但它给了我文件名。
=MID(a1,FIND(CHAR(1),
SUBSTITUTE(a1,"\",CHAR(1),LEN(a1)-LEN(SUBSTITUTE(a1,"\",""))))+1,LEN(a1))
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"
.....
etc.
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))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这有效。
=MID(A1,1,LEN(A1)-LEN(MID(A1,FIND(CHAR(1)),SUBSTITUTE(A1,"\",CHAR(1),LEN(A1)-LEN(SUBSTITUTE( A1,"\",""))))+1,LEN(A1))))
以上是我原来的答案。 Neil 稍微简化了表达式并将其发布为 下面的注释:
这利用了
?
是一个事实路径中禁用字符,以便可以安全地使用"?"
代替CHAR(1)
作为地标,从而稍微提高可读性。此外,LEFT(A1,x)
等价于MID(A1,1,x)
,并且比MID(A1,1,x)
短,因此使用LEFT
是有意义的代码>.但最重要的是,此公式使用了 FIND,而不是使用 LEN 进行字符计数的第二层。这使得它更具可读性。This works.
=MID(A1,1,LEN(A1)-LEN(MID(A1,FIND(CHAR(1),SUBSTITUTE(A1,"\",CHAR(1),LEN(A1)-LEN(SUBSTITUTE(A1,"\",""))))+1,LEN(A1))))
The above was my original answer. Neil simplified the expression somewhat and posted this as a comment below:
This takes advantage of the fact that
?
is a forbidden character in paths so that"?"
can safely be used instead ofCHAR(1)
as a placemark, thus improving readability a little bit. Also,LEFT(A1,x)
is equivalent to, and shorter thanMID(A1,1,x)
, so it makes sense to useLEFT
. But most importantly, this formula makes use ofFIND
, instead of a second layer of counting characters usingLEN
. This makes it much more readable.对于 2024 年遇到此问题的人,Excel 2019/365 及更高版本中提供了一种更简单的方法:
For those coming to this problem in 2024, there's a much easier method available in Excel 2019/365 onward:
我没有足够的声誉来评论 Bendataclear 针对 Excel 2019/365 的解决方案,但如果单元格 A1 中没有文件路径,则可以使用以下公式:
I don't have enough reputation to comment on bendataclear's solution for Excel 2019/365, but if you don't have the file path in cell A1, you can use this formula: