我有以下代码将文件从一个目录移动到另一个目录: var fs = require('fs'), oldPath = 'firstfile.txt', newPath = 'temp/firstfile.txt';fs.rename(oldPath, newPath, function (err) { console.log('rename callback ', err); }); 如
var fs = require('fs'),
oldPath = 'firstfile.txt',
newPath = 'temp/firstfile.txt';
fs.rename(oldPath, newPath, function (err) {
console.log('rename callback ', err);
});
如果newPath文件已存在,是否可能触发错误?
试试下面的代码:它调用.exists方法来检查路径是否存在
var fs = require('fs'),
oldPath = 'firstfile.txt',
newPath = 'temp/firstfile.txt';
fs.exists(newPath, function(exists){
if (!exists) {
fs.rename(oldPath, newPath, function (err) {
console.log('rename callback ', err);
});
} else {
console.log('The File Already exists');
}
});
