我試着去使用Node.js的從文件夾中的文件移動到currentFolder(root of project)/node_modules/mime/
但currentFolder(root of project)/files/
我不斷收到此錯誤:麻煩 - 節點JS從文件夾X文件移動到文件夾Ÿ
events.js:85
throw er; // Unhandled 'error' event
^
Error: EISDIR, open 'files/'
at Error (native)
這裏是我有什麼至今。目前正在做的是循環遍歷我指定的目錄中的每個文件夾,並打印出每個文件夾的文件類型。但我在移動文件時遇到問題,我一直在收到上面指定的錯誤。我當前的代碼如下 -
var mime = require('mime');
var fs = require('fs');
var dest = fs.createWriteStream('files/');
var source;
var i = 0;
// General function - Loop through files in directory X and COPY AND PASTE FILES FROM X to Y
var dive = function (dir, action) {
// Assert that it's a function
if (typeof action !== "function")
action = function (error, file) { };
// Read the directory
fs.readdir(dir, function (err, list) {
// Return the error if something went wrong
if (err)
return action(err);
// Loop through file in list
list.forEach(function (file) {
console.log(mime.lookup(dir + "/" + file));
// Full path of that file
path = dir + "/" + file;
source = fs.createReadStream(path);
source.pipe(dest + '/'+i);
console.log("Moved");
i++;
source.on('end', function() { /* copied */ });
source.on('error', function(err) { /* error */ });
});
});
};
dive("node_modules/mime");
你不寫文件的目錄與流目錄。您需要爲每個目標文件分別創建一個流。 – TMG