Search code examples
node.jsnodes

Move all .txt files from one folder to another folder using Node js


I have tried with this code but it's not working it display error like that file not exists on that directory. System take .txt as file not as extension of file.

const fs = require('fs');
    var oldPath = '/abc/def/ghi/*.txt'
    var newPath = '/xyz/cbi/'

    fs.rename(oldPath, newPath, function (err) {
    if (err) throw err
    console.log('Successfully renamed - AKA moved!')
    })

Solution

  • Try this one:

    const shell = require('child_process').execSync ; 
    const src= `/abc/def/ghi`;
    const dist= `/xyz/cbi`;
    
    shell(`mv ${src}/* ${dist}`);