Get file folder with Nodejs

Asked

Viewed 1,269 times

1

Hello, I am trying to get the file folder but without success. I’ve tried with path, with __dirname, process.cwd(), but all return the complete path, for example:

C: Node folder where is the.js file

What I want to know is the file folder:

the file.js

How can I do that?

  • But you chose the worst as an answer to your problem...

  • It was the only one who returned the way I wanted

  • You tested the path.basename? If yes and did not return what you wanted your question was not clear.

  • tested, returned the entire path

  • Does not return the whole path no, you tested wrong.

  • Actually it brings the file in the directory, I need the folder and the file, example: foo/bar.html

  • 1

    So your question is unclear. O que eu quero saber é a pasta do arquivo, the file folder is different from the file composition. Finally, in the basename has 3 different variables. Although you have given a confusing explanation that is the best way using the resources of Node.js

  • Although my suggestion works, Sorack’s response gets the same result in a more expensive form of Node

Show 4 more comments

3 answers

1

Use path dirname.

    // filePath deve ser C:\node\pasta\onde\esta\o
    var filePath = require('path').dirname('C:\node\pasta\onde\esta\o\arquivo.js');
    var currentDirectory = filePath.split(path.sep).pop();
  • This returns like the others, I just want to know the folder the file is in, without the previous folders and directory, how this returns

1

Use the function path.basename:

const path = require('path');

// __dirname = C:\desenvolvimento\amostra
// __filename = C:\desenvolvimento\amostra\amostra.js
const diretorio = path.basename(__dirname);
const arquivo = path.basename(__filename);
const composicao = path.join(diretorio, arquivo);

console.log('Diretório:', diretorio); // Diretório: amostra
console.log('Arquivo:', arquivo); // Arquivo: amostra.js
console.log('Diretório + Arquivo:', composicao); // Diretório + Arquivo: amostra\amostra.js

path.basename

The path.basename() methods Returns the last Portion of a path, similar to the Unix basename command. Trailing directory separators are Ignored, see path.sep.

In free translation:

The method path.basename() returns the last portion of a path, similar to the Unix command basename. Tabs on the right are ignored, see path.sep.

0


Hello,

I don’t know if there is a direct way to get only the directory folder, but I suggest you take the result you already have and do a simple manipulation.

const caminho = 'C:\\node\\pasta\\onde\\esta\\o\\arquivo.js';

const caminhoPorNivelDeDiretorio = caminho.split('\\');

const diretorio = caminhoPorNivelDeDiretorio.slice(-2).join('\\');
  • Solved in parts, let’s say I have the following structure: 
/pages/index.html
/pages/top/top.html
/pages/bot/bot.html
 At split time, it takes the right subfolders of the pages, but it also takes the pages folder, generating something like this in the output: 
/pages/index.html
/top/top.html
/bot/bot.html


  • I did not understand exactly the doubt, what would be the expected result for this other structure /pages/index.html /pages/top/top.html /pages/bot/bot.html?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.