1

How to get parent folder name from worspacefolder path in nodejs.I tried but not working.can anyone find where i did the mistake? prevFolder name should be rain.

  var pathname=vscode.workspace.workspaceFolders[0].uri;  
  /* c:/xampp/htdocs/rain/tree */

  var prevFolder= path.dirname(pathname).split(path.sep).pop();

  console.log(prevFolder)
4
  • Let me check. 1 min Commented Aug 9, 2019 at 13:54
  • No..not working..getting like this: /c:/xampp/htdocs/ Commented Aug 9, 2019 at 14:05
  • Okay, I misunderstood. This is all you need path.basename(path.dirname(pathname)) No need for array manipulations. Commented Aug 9, 2019 at 14:31
  • You are asking to point out your error. You got answers referring to the code you showed. That code is essential for your question. It seems that you accidentally deleted it. I hence undid that edit. Please keep in mind, that questions need to make sense even after having received answers. Especially they need to keep those answers valid. Commented May 5, 2021 at 5:25

5 Answers 5

3

Keep it simple:

var prevFolder = path.basename(path.dirname(pathname));

No need for array fiddling.

Sign up to request clarification or add additional context in comments.

Comments

2

Your own solution will work if you don't use path.sep (since you are on Windows, it is set to '\' and not '/':

var prevFolder= path.dirname(pathname).split('/').pop();

But to avoid the separator issue completely, you could use:

var prevFolder= path.basename(path.dirname(pathname));

Probably more efficient than the split method, too.

3 Comments

Can you post your answer for that question?
Wow, your edit looks pretty similar to my earlier answer... Thanks.
@Mark, didn't see your answer when editing mine. They are actually identical. A strong sign that this might be the right way to do it ;-)
0

You can get the current working directory from process.cwd(); After that, you can use it up to your choice.

1 Comment

If we use process we can not get workspace folder path
0

i dont know the command "vscode.workspace.workspaceFolders[0].uri". But if it returns "c:/xampp/htdocs/projects/rain/tree" and you need tree, you can do:

  var pathname=vscode.workspace.workspaceFolders[0].uri
  var prevFolder= pathname.split("/");
  console.log(prevFolder[prevFolder.length-1])

Comments

0

Try like this,

path.dirname(filename).split(path.sep).pop()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.