Get directory from file path javascript
How to get the directory of a file? Show For example, I pass in a string
I want a function that returns me
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 asked May 4, 2009 at 2:10
GravitonGraviton 80.1k141 gold badges417 silver badges594 bronze badges 4 I don't know if there is any built in functionality for this, but it's pretty straight forward to get the path.
Abyx 11.7k5 gold badges41 silver badges75 bronze badges answered May 4, 2009 at 2:19
PhaedrusPhaedrus 8,29325 silver badges28 bronze badges 2 If you use
Zanon 26.8k20 gold badges109 silver badges122 bronze badges answered Mar 21, 2020 at 12:34
W.PerrinW.Perrin 3,46929 silver badges28 bronze badges 2 Use:
*The answers that are based on lastIndexOf('/') or lastIndexOf('\') are error prone, because path can be "c:\aa/bb\cc/dd". answered Aug 20, 2015 at 17:27
TheZverTheZver 1,4632 gold badges14 silver badges17 bronze badges 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:
Abyx 11.7k5 gold badges41 silver badges75 bronze badges answered May 4, 2009 at 2:35
Matthew FlaschenMatthew Flaschen 271k49 gold badges510 silver badges537 bronze badges Path module has an inbuilt functionYes, the inbuilt module path has dirname() function, which would do the job for you.
I see that your path is neither windows nor Linux compatible. Do not hardcode path; instead, take a reference from a path based on your OS. I generally tackle such situations by creating relative paths using
This gives me a relative path that works regardless of the OS you are using. answered Nov 30, 2021 at 7:21
answered May 4, 2009 at 2:47
brianpeirisbrianpeiris 10.6k1 gold badge29 silver badges44 bronze badges Sorry to bring this back up but was also looking for a solution without referencing the variable twice. I came up with the following:
This is a bit of a walk through manipulating a string to array and back but it's clean enough I think. answered Jan 22, 2016 at 21:18
SmujMaikuSmujMaiku 4464 silver badges10 bronze badges And this? If isn't a program in addressFile, return addressFile
answered Mar 30, 2014 at 21:28
such that
answered May 20, 2018 at 16:45
Ulad KasachUlad Kasach 10.1k9 gold badges55 silver badges81 bronze badges The core Javascript language doesn't provide file/io functions. However if you're working in a Windows OS you can use the FileSystemObject (ActiveX/COM). Note: Don't use this in the client script-side script of a web application though, it's best in other areas such as in Windows script host, or the server side of a web app where you have more control over the platform. This page provides a good tutorial on how to do this. Here's a rough example to do what you want:
Graviton 80.1k141 gold badges417 silver badges594 bronze badges answered May 4, 2009 at 2:17
AshAsh 59.6k31 gold badges150 silver badges168 bronze badges 6 Not the answer you're looking for? Browse other questions tagged javascript or ask your own question.How do I find the directory of a URL?To get a directory from a file path or URL with JavaScript, we can use the path. dirname method. We call path. dirname with a path to get the directory path.
How do I get CWD in JavaScript?In Node. js, there are two built-in ways to get the current directory. You can either use the __dirname variable or the process. cwd() method to get the current folder.
What is __ dirname in node JS?It gives the current working directory of the Node. js process. __dirname: It is a local variable that returns the directory name of the current module. It returns the folder path of the current JavaScript file.
What is webkitRelativePath?webkitRelativePath is a read-only property that contains a string which specifies the file's path relative to the directory selected by the user in an element with its webkitdirectory attribute set.
|