0

I am new to TypeScript and was wondering what command to use to remove the file path and extension. for example if my file is ./data/input/myfile.js how can i strip it out to only have myfile (without path and extension)

Pari
  • 45
  • 6

2 Answers2

0

This is more of a javascript question and not a typescript question ;)

Use the built-in path library of node, which covers all the edge cases of filesystems

max
  • 310
  • 1
  • 6
  • Parsing paths by splitting them using arbitrary separators is not advisable. The example works fine on Linux and macOS (and any other Un*x flavour) but it doesn't work on Windows (because Windows paths use ``\`` as separator and they are also prefixed by the drive). This approach also doesn't handle the multiple consecutive occurrences of the separator, relative paths etc. Use the functions provided by the [`path` module](https://nodejs.org/dist/latest-v12.x/docs/api/path.html) of Node.js. – axiac May 26 '20 at 21:07
  • Thank you very much. Using path helped. – Pari May 26 '20 at 21:12
0

In node.js, you would use the path module and use path.basename() in that module:

const path = require('path');

let basename = path.basename('./data/input/myfile.js');
console.log(basename);    // "myfile.js"

FYI, this has nothing to do with TypeScript, this is just a plain node.js question.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Thank you very much. I just started to do coding on type script and I am still confused about nodejs, type script – Pari May 28 '20 at 18:10