Extract filename from path

后端 未结 8 665
梦谈多话
梦谈多话 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:51

    Using Java:

    String myPath="C:\folder\folder\folder\file.txt";
    System.out.println("filename " +  myPath.lastIndexOf('\\'));
    
    0 讨论(0)
  • 2021-01-04 08:52

    I believe this works, using VBA:

    Dim strPath As String
    strPath = "C:\folder\folder\folder\file.txt"
    
    Dim strFile As String
    strFile = Right(strPath, Len(strPath) - InStrRev(strPath, "\"))
    

    InStrRev looks for the first instance of "\" from the end, and returns the position. Right makes a substring starting from the right of given length, so you calculate the needed length using Len - InStrRev

    0 讨论(0)
提交回复
热议问题