5

I in electron am doing:

 path.dirname('C:\\Users\\Blagoh\\Documents\\GitHub\\Screeenshoter\\dist\\electron\\main')

That path is the actual value of my __dirname. How come it is not giving me C:\\Users\\Blagoh\\Documents\\GitHub\\Screeenshoter\\dist\\electron? I want that main part chopped off.

Blagoh
  • 1,225
  • 1
  • 14
  • 29
  • 2
    The nodejs [source for dirname](https://github.com/nodejs/node/blob/master/lib/path.js#L717-L719) states that it returns `'.'`, if the passed string has a length of 0 or if it is just a string without any (back-)slashes (just a folder). – RoyalBingBong Jul 24 '17 at 08:53
  • Oh dang, thanks @RoyalBingBong - I see I need to put a filename with it. – Blagoh Jul 28 '17 at 17:59

2 Answers2

0

Assuming main is a directory inside electron. Also assuming that you have some file called index.js inside main folder where you want to have the path of electron directory.

So, you can do path.join this way:

var mainFolderParentPath = path.join(__dirname, '../');

Your original file location:

C:\\Users\\Blagoh\\Documents\\GitHub\\Screeenshoter\\dist\\electron\\main\\index.js

__dirname will return

C:\\Users\\Blagoh\\Documents\\GitHub\\Screeenshoter\\dist\\electron\\main

and then inside path.join '../', will chop off the main folder from path. So, you will be left off with:

C:\\Users\\Blagoh\\Documents\\GitHub\\Screeenshoter\\dist\\electron
Vishal
  • 6,238
  • 10
  • 82
  • 158
0

Well you obviously didn't read the docs for dirname. It states that it works like the Unix command dirname which "strips non-directory suffix from file name", thus you get the C:\\Users\\Blagoh\\Documents\\GitHub\\Screeenshoter\\dist\\electron.

What you are looking for is basename.

path.basename('C:\\Users\\Blagoh\\Documents\\GitHub\\Screeenshoter\\dist\\electron\\main') will give you main.

RoyalBingBong
  • 1,106
  • 7
  • 15