How do I use extended classes: directory & file


#1

I’m new to atom and web technologies. I need to work with some disk files and directories. I want to use the API extended classes but I cannot figure out how to use them.

Specifically, I want to get a relative path between two files which I think the relativize() method will do but I cannot seem to find a way to use it.

I also want to use the file extended class to determine of a file exists which I think that exists() method will do but I also cannot find a way to use this.

Any help is appreciate. Also, since I am new at this, a more spelled out answer will help.


#2

Hi @morbius1st,

I want to get a relative path between two files which I think the relativize() method will do but I cannot seem to find a way to use it.

As stated in the documentation, relativize returns the relative path to the given path from this
directory, if you need to compute the relative path between two files you should use the path.relative method from the path module:

path = require 'path'
{File} = require 'atom'

file_a = new File(atom.packages.getActivePackage('tree-view').mainModulePath)
file_b = new File(atom.packages.getActivePackage('tabs').mainModulePath)

path.relative(file_a.path, file_b.path) # "../../../tabs/lib/main.js" on my computer

I also want to use the file extended class to determine of a file exists which I think that exists() method will do but I also cannot find a way to use this

Beware, the File.exists() is asynchronous, and returns a promise, so you can either do:

{File} = require 'atom'

file = new File(atom.packages.getActivePackage('tree-view').mainModulePath)

exists = file.existsSync() # true

promise = file.exists()
promise.then (result) -> console.log result # true

#3

Thank you very much for your help with this