Get directory of a file name in Javascript

前端 未结 9 1531
囚心锁ツ
囚心锁ツ 2021-01-17 07:41

How to get the directory of a file?

For example, I pass in a string

C:\\Program Files\\nant\\bin\\nant.exe

I want a function that r

9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-17 08:18

    There's no perfect solution, because this functionality isn't built-in, and there's no way to get the system file-separator. You can try:

    path = path.substring(0, Math.max(path.lastIndexOf("/"), path.lastIndexOf("\\"))); 
    alert(path);
    

提交回复
热议问题