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
.
But you chose the worst as an answer to your problem...
– Sorack
It was the only one who returned the way I wanted
– Douglas Teles
You tested the
path.basename
? If yes and did not return what you wanted your question was not clear.– Sorack
tested, returned the entire path
– Douglas Teles
Does not return the whole path no, you tested wrong.
– Sorack
Actually it brings the file in the directory, I need the folder and the file, example: foo/bar.html
– Douglas Teles
Let’s go continue this discussion in chat.
– Douglas Teles
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 thebasename
has 3 different variables. Although you have given a confusing explanation that is the best way using the resources ofNode.js
– Sorack
Although my suggestion works, Sorack’s response gets the same result in a more expensive form of Node
– Arthur Ferraz