Node.js return new file name after rename

时间:2018-09-18 19:41:59

标签: node.js

im using the fs.rename from node, however im running into the issue that im not able to return the new fileName after it has been renamed.

fs.rename(file, directoryPath + md5(buf + randomNumber()) + '.mp3',(err)=> {
  if (err) return reject(err);
  resolve(md5(buf));
})

Instead of the md5(buf) here what would i do to get the new name of the file? what im recieving now from the resolve is a new md5 name for the file, but before it has been resolved the filename is different.

1 个答案:

答案 0 :(得分:2)

Well, you can't!

What you can do, is first put the name in a variable so you can use it later!

Like this:

const renameTo = directoryPath + md5(buf + randomNumber()) + '.mp3';
fs.rename(file, renameTo,(err)=> {
  if (err) return reject(err);
  resolve(renameTo);
})

happy coding!