Reputation: 15903
Im trying to use node.js to move files from folder the currentFolder(root of project)/node_modules/mime/
to currentFolder(root of project)/files/
But I keep getting this error:
events.js:85
throw er; // Unhandled 'error' event
^
Error: EISDIR, open 'files/'
at Error (native)
Here is what I have so far. At the moment what it's doing is looping through each folder in the directory I've specified and printing out the file type of each one that was read. But I'm having trouble moving the files, I keep getting the error specified above. My current code is below -
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");
Upvotes: 0
Views: 59
Reputation: 2740
You can createWriteStream
on a file, not a directory. You need separate stream for each target file.
So instead of this:
source = fs.createReadStream(path);
source.pipe(dest + '/'+i);
you need this:
source = fs.createReadStream(path);
var destFile = fs.createWriteStream('files/'+i);
source.pipe(destFile);
Upvotes: 1