0

I'm trying to replace the parent directory of a given path

src/path/to/file.ext => dest/path/to/file.newext

My current solution involves using string.split, and looks pretty awful. It works, though.

var path = require('path');

var outDir = 'dest/'
var inFile = 'src/path/to/file.ext'
var outFile = path.basename(inFile, path.extname(inFile)) + '.newext';

var destDir = path.join(outDir, path.dirname(inFile.split('/').slice(1).join('/')));
outFile = path.join(destDir, outFile) // dest/path/to/file.newext

Is there another way I could go about this?

dook
  • 1,213
  • 1
  • 13
  • 30
  • That's a completely different question. :) I am trying to replace the beginning part of the path more elegantly `src/file => dest/file` instead of using split like in the code I provided above. – dook May 20 '14 at 22:41
  • It's actually the same question, just asked in reverse. You want to take the path off of a filename. The question I linked tells you how to take the filename off of the path. Just add your new path to that filename, and you're good to go. – sgress454 May 20 '14 at 22:46

0 Answers0