I have a file under "modules/test/main/main.js" and i want to get the "test" folder :
I tried this code but all i get is the main
folder
path.basename(path.dirname('modules/test/main/main.js'))
You can split on /
and than drop off last item as it is file name. and than take the middle element
let path = `modules/test/main/main.js` let middle = path.split(/\\//g) middle.pop() console.log(middle[Math.floor(middle.length-1)/2])
You can use path.join()
to go up two directories, and then get the basename
of the directory:
const p = 'modules/test/main/main.js';
const dir = path.basename(path.join(p, '../..')); // 'test'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.