Extract filename from path

后端 未结 8 802
梦谈多话
梦谈多话 2021-01-04 08:21

I need to extract the filename from a path (a string):

e.g., \"C:\\folder\\folder\\folder\\file.txt\" = \"file\" (or even \"file.txt\" to get me sta

8条回答
  •  情歌与酒
    2021-01-04 08:40

    I was looking for a solution without code. This VBA works in the Excel Formula Bar:

    To extract the file name:

    =RIGHT(A1,LEN(A1)-FIND("~",SUBSTITUTE(A1,"\","~",LEN(A1)-LEN(SUBSTITUTE(A1,"\","")))))
    

    To extract the file path:

    =MID(A1,1,LEN(A1)-LEN(MID(A1,FIND(CHAR(1),SUBSTITUTE(A1,"\",CHAR(1),LEN(A1)-LEN(SUBSTITUTE(A1,"\",""))))+1,LEN(A1))))
    

提交回复
热议问题