Get directory of a file name in Javascript

前端 未结 9 1512
囚心锁ツ
囚心锁ツ 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条回答
  •  伪装坚强ぢ
    2021-01-17 08:17

    Sorry to bring this back up but was also looking for a solution without referencing the variable twice. I came up with the following:

    var filepath = 'C:\\Program Files\\nant\\bin\\nant.exe';
        // C:\Program Files\nant\bin\nant.exe
    var dirpath = filepath.split('\\').reverse().splice(1).reverse().join('\\');
        // C:\Program Files\nant\bin
    

    This is a bit of a walk through manipulating a string to array and back but it's clean enough I think.

提交回复
热议问题