Get parent directory name in Node.js

前端 未结 8 1491
北海茫月
北海茫月 2021-01-31 13:27

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

相关标签:
8条回答
  • 2021-01-31 14:00
    const path = require("path")
    path.dirname(path.basename(__dirname))
    
    0 讨论(0)
  • 2021-01-31 14:02

    Better use @danielwolf's answer instead


    Use split() and pop():

    path.dirname(filename).split(path.sep).pop()
    
    0 讨论(0)
提交回复
热议问题