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
3

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

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.