Skip to content
Advertisement

Get directory of a file name in Javascript

How to get the directory of a file?

For example, I pass in a string

C:Program Filesnantbinnant.exe

I want a function that returns me

C:Program Filesnantbin

I would prefer a built in function that does the job, instead of having manually split the string and exclude the last one.

Edit: I am running on Windows

Advertisement

Answer

I don’t know if there is any built in functionality for this, but it’s pretty straight forward to get the path.

path = path.substring(0,path.lastIndexOf("\")+1);
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement