0

Processing a bunch of files using Node and I need to separate the file name from the directory. Does node have a simple way to do this without additional dependencies? I could use an NPM package like Filename Regex, but thought I'd check if there something available out of the box?

So for example suppose we have src/main/css/file.css. Hoping something like this is possible:

 const fs = require('fs');
 const path = fs.filePath(String pathAndFileName);  //path = src/main/css
 const file = fs.fileName(String pathAndFileName);  //file = file.css
Ole
  • 41,793
  • 59
  • 191
  • 359

1 Answers1

1

The utilities for manipulating file paths are in the path module. https://nodejs.org/api/path.html

 const {dirname, basename}  = require('path');

 const path = dirname(String pathAndFileName);
 const file = basename(String pathAndFileName);
generalhenry
  • 17,227
  • 4
  • 48
  • 63