Node.js fsPromises.rename() 方法
fsPromises.rename()方法用于将给定旧路径处的文件异步重命名为给定新路径。如果它已经存在,它将覆盖目标文件。它在成功时无需争论即可解决 Promise。
句法:
fsPromises.rename( oldPath, newPath )
参数:此方法接受上面提到的两个参数,如下所述:
- oldPath:它保存必须重命名的文件的路径。它可以是字符串、缓冲区或 URL。
- newPath:它保存文件必须重命名的新路径。它可以是字符串、缓冲区或 URL。
下面的示例说明了 Node.js 中的fsPromises.rename()方法:
示例 1:此示例使用fsPromises.rename()方法重命名文件:
Node.js
// Node.js program to demonstrate the
// fsPromises.rename() method
// Import filesystem module
const fs = require('fs');
const fsPromises = require('fs').promises;
// List all the filenames before renaming
getCurrentFilenames();
(async function main() {
try {
// Rename the file
fsPromises.rename('GFG.txt', 'GeeksforGeeks.txt')
console.log("\nFile Renamed!\n");
// List all the filenames after renaming
getCurrentFilenames();
} catch (err) {
console.error(err);
}
})();
// Function to get current filenames
// in directory
function getCurrentFilenames() {
console.log("Current filenames:");
fs.readdirSync(__dirname).forEach(file => {
console.log(file);
});
}
Node.js
// Node.js program to demonstrate the
// fsPromises.rename() method
// Import filesystem module
const fs = require('fs');
const fsPromises = require('fs').promises;
(async function main() {
try {
// List all the filenames before renaming
getCurrentFilenames();
// Rename the file
fsPromises.rename('GeeksforGeeks.txt', 'geeks.txt')
// List all the filenames after renaming
console.log("\nFile Renamed\n");
// List all the filenames after renaming
getCurrentFilenames();
}
catch (err) {
console.error(err);
}
})();
// Function to get current filenames
// in directory
function getCurrentFilenames() {
console.log("Current filenames:");
fs.readdirSync(__dirname).forEach(file => {
console.log(file);
});
}
输出:
Current filenames:
GFG.txt
GeeksforGeeks.js
File Renamed!
Current filenames:
GeeksforGeeks.js
GFG.txt
示例 2:此示例使用fsPromises.rename()方法来演示文件重命名过程中的错误:
节点.js
// Node.js program to demonstrate the
// fsPromises.rename() method
// Import filesystem module
const fs = require('fs');
const fsPromises = require('fs').promises;
(async function main() {
try {
// List all the filenames before renaming
getCurrentFilenames();
// Rename the file
fsPromises.rename('GeeksforGeeks.txt', 'geeks.txt')
// List all the filenames after renaming
console.log("\nFile Renamed\n");
// List all the filenames after renaming
getCurrentFilenames();
}
catch (err) {
console.error(err);
}
})();
// Function to get current filenames
// in directory
function getCurrentFilenames() {
console.log("Current filenames:");
fs.readdirSync(__dirname).forEach(file => {
console.log(file);
});
}
输出:
Current filenames:
index.js
package.json
world.txt
[Error: ENOENT: no such file or directory, rename
'G:\tutorials\nodejs-fs-rename\GeeksforGeeks.txt' ->
'G:\tutorials\nodejs-fs-rename\geeks.txt'] {
errno: -4058,
code: 'ENOENT',
syscall: 'rename',
path: 'G:\\tutorials\\nodejs-fs-rename\\GeeksforGeeks.txt',
dest: 'G:\\tutorials\\nodejs-fs-rename\\geeks.txt'
}
参考: https://nodejs.org/api/fs.html#fs_fspromises_rename_oldpath_newpath