Me5 Me5 -4 years ago 321
Node.js Question

Get parent directory name in node.js

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"


I want to get
"FolderIWant"


I have tried:

var path = require('path');
var parentDir = path.dirname(filename);


but it returns
../test1/folder1/FolderIWant

Answer Source

Use split() and pop():

path.dirname(filename).split(path.sep).pop()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download