Node.js fs.renameSync() 方法
fs.renameSync() 方法用于将给定旧路径处的文件同步重命名为给定新路径。如果它已经存在,它将覆盖目标文件。
句法:
fs.renameSync( oldPath, newPath )
属性值:
- oldPath:它保存必须重命名的文件的路径。它可以是字符串、缓冲区或 URL。
- newPath:它保存文件必须重命名的新路径。它可以是字符串、缓冲区或 URL。
下面的示例说明了 Node.js 中的fs.renameSync() 方法:
示例 1:此示例使用 fs.renameSync() 方法重命名文件。
// Node.js program to demonstrate the
// fs.renameSync() method
// Import the filesystem module
const fs = require('fs');
// List all the filenames before renaming
getCurrentFilenames();
// Rename the file
fs.renameSync('hello.txt', 'world.txt');
// List all the filenames after renaming
getCurrentFilenames();
// function to get current filenames in directory
function getCurrentFilenames() {
console.log("Current filenames:");
fs.readdirSync(__dirname).forEach(file => {
console.log(file);
});
}
输出:
Current filenames:
hello.txt
index.js
package.json
Current filenames:
index.js
package.json
world.txt
示例 2:此示例使用 fs.renameSync() 方法来演示文件重命名过程中的错误。
// Node.js program to demonstrate the
// fs.renameSync() method
// Import the filesystem module
const fs = require('fs');
// List all the filenames before renaming
getCurrentFilenames();
// Rename a non-existent file
fs.renameSync('nonexist.txt', 'world.txt');
// List all the filenames after renaming
getCurrentFilenames();
// 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
internal/fs/utils.js:220
throw err;
^
Error: ENOENT: no such file or directory, rename 'nonexist.txt' -> 'world.txt'
at Object.renameSync (fs.js:643:3)
at Object. (G:\tutorials\nodejs-fs-renameSync\index.js:29:4)
at Module._compile (internal/modules/cjs/loader.js:956:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
at Module.load (internal/modules/cjs/loader.js:812:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)
at internal/main/run_main_module.js:17:11 {
errno: -4058,
syscall: 'rename',
code: 'ENOENT',
path: 'nonexist.txt',
dest: 'world.txt'
}
参考: https://nodejs.org/api/fs.html#fs_fs_renamesync_oldpath_newpath