我如何使用 javascript 复制C:\folderA\myfile.txt到C:\folderB\myfile.txt?我也有检查,以确保myfile.txt不已经存在folderB。最后我不得不然后重命名新文件myfile.txt到myfile.bak。
我知道 javascript 不能真正用于本地文件系统,但如果可以,我将如何尽可能简单地编写此代码?
小智 10
在浏览器端,您无法访问本地系统文件。但在服务器端,您可以按如下方式进行。
//copyfile.js
const fs = require('fs');
// destination will be created or overwritten by default.
fs.copyFile('C:\folderA\myfile.txt', 'C:\folderB\myfile.txt', (err) => {
if (err) throw err;
console.log('File was copied to destination');
});
Run Code Online (Sandbox Code Playgroud)
nodejs 必须安装在您的服务器上,然后按如下方式运行上面的脚本
node copyfile.js
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23856 次 |
| 最近记录: |