Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get directory from a file path or url

Tags:

I am trying to get the directory location of a file, and I'm not sure how to get it. I can't seem to find a module that allows me to do this.

So for example say I have this string:

/this/is/a/path/to/a/file.html 

how can I get this:

/this/is/a/path/to/a 

I know I can use something like this:

path.substr(0, path.lastIndexOf("/") - 1); 

But I am not sure if that is as good of a method as something that might be built in to node.

I have also tried:

var info = url.parse(full_path); console.log(info); 

and the result doesn't return what I am looking for, that gets the full path including the filename.

So, is there something built into node that can do this and do it well?

like image 638
Get Off My Lawn Avatar asked Apr 07 '15 16:04

Get Off My Lawn


People also ask

How do I find the directory of a file?

Click the Start button and then click Computer, click to open the location of the desired file, hold down the Shift key and right-click the file. Copy As Path: Click this option to paste the full file path into a document. Properties: Click this option to immediately view the full file path (location).

Can a file path be a URL?

file is a registered URI scheme (for "Host-specific file names"). So yes, file URIs are URLs.

Is a file path a directory?

A path is a slash-separated list of directory names followed by either a directory name or a file name. A directory is the same as a folder.

What is absolute path extract directory?

What Does Absolute Path Mean? An absolute path refers to the complete details needed to locate a file or folder, starting from the root element and ending with the other subdirectories. Absolute paths are used in websites and operating systems for locating files and folders.


1 Answers

Using path module of node.js:

path.dirname('/this/is/a/path/to/a/file'); 

returns

'/this/is/a/path/to/a' 
like image 63
Hypaethral Avatar answered Oct 10 '22 06:10

Hypaethral