I am using Node.js, and I want to obtain the parent directory name for a file. I have the file "../test1/folder1/FolderIWant/test.txt"
.
我正在使用Node.js,我想获取文件的父目录名称。我有文件“../test1/folder1/FolderIWant/test.txt”。
I want to get "FolderIWant"
.
我想得到“FolderIWant”。
I have tried:
我努力了:
var path = require('path');
var parentDir = path.dirname(filename);
But it returns ../test1/folder1/FolderIWant
.
但它返回../test1/folder1/FolderIWant。
2 个解决方案
#1
25
Use split() and pop():
使用split()和pop():
path.dirname(filename).split(path.sep).pop()
#1
25
Use split() and pop():
使用split()和pop():
path.dirname(filename).split(path.sep).pop()