path.dirname on Windows path is giving `.`

2019-07-25 09:30发布

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.

2条回答
Emotional °昔
2楼-- · 2019-07-25 09:59

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
查看更多
聊天终结者
3楼-- · 2019-07-25 09:59

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.

查看更多
登录 后发表回答