js批量修改本地文件名
时间: 2023-06-25 17:03:48 浏览: 58
你可以使用Node.js来批量修改本地文件名,以下是一个简单的示例代码:
```js
const fs = require('fs');
const path = require('path');
const directoryPath = '/path/to/directory';
fs.readdir(directoryPath, (err, files) => {
if (err) {
console.log('Error getting directory information.');
} else {
files.forEach((file) => {
const oldPath = path.join(directoryPath, file);
const newPath = path.join(directoryPath, file.replace('old', 'new'));
fs.rename(oldPath, newPath, (err) => {
if (err) throw err;
console.log(`${oldPath} renamed to ${newPath}`);
});
});
}
});
```
在这个示例中,我们首先使用`fs.readdir`读取目录中的所有文件。然后,我们使用`fs.rename`将每个文件名中的“old”替换为“new”,并将其重命名为新文件名。
请注意,在上面的代码中,我们将目录路径硬编码为`'/path/to/directory'`。你需要将其替换为实际的目录路径,以便代码正确运行。