簡體   English   中英

javascript - 將文件從一個目錄復制到另一個目錄

[英]javascript - Copy file from one directory to another

我怎么能用javascript將C:\\folderA\\myfile.txt復制到C:\\folderB\\myfile.txt 我還必須檢查以確保folderB中不存在myfile.txt 最后,我必須將新文件從myfile.txt重命名為myfile.bak

我知道javascript實際上不能在本地文件系統上使用,但如果可能,我將如何盡可能簡單地編寫這段代碼?

在瀏覽器端,您無法訪問本地系統文件。但在服務器端,您可以按如下方式進行操作。

//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,然后按如下方式在腳本上運行

node copyfile.js

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM