[英]javascript - Copy file from one directory to another
How could I use javascript to copy C:\\folderA\\myfile.txt
to C:\\folderB\\myfile.txt
? 我怎么能用javascript将C:\\folderA\\myfile.txt
复制到C:\\folderB\\myfile.txt
? I also have to check to make sure that myfile.txt
does not already exist in folderB
. 我还必须检查以确保folderB
中不存在myfile.txt
。 And finally I have to then rename the new file from myfile.txt
to myfile.bak
. 最后,我必须将新文件从myfile.txt
重命名为myfile.bak
。
I know javascript can't really be used on a local file system, but if it could be, how would I write this code as simply as possible? 我知道javascript实际上不能在本地文件系统上使用,但如果可能,我将如何尽可能简单地编写这段代码?
in browser side you cannot access local system files.But in server side you could do it as follows. 在浏览器端,您无法访问本地系统文件。但在服务器端,您可以按如下方式进行操作。
//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');
});
nodejs has to be installed on your server and then run above script as follows 必须在您的服务器上安装 nodejs,然后按如下方式在脚本上运行
node copyfile.js
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.