Last active
November 29, 2015 03:31
-
-
Save bdss58/b526e869b0fdeb3331bf to your computer and use it in GitHub Desktop.
nodejs copy file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use scrict'; | |
var fs = require('fs'); | |
var stat = fs.stat; | |
function copy(src, dest){ | |
fs.readdir(src, function(err, paths) { | |
if (err) { | |
console.log(err); | |
throw err; | |
} else { | |
paths.forEach(function(path) { | |
var _src = src + '/' + path; | |
var _dest = dest + '/' + path; | |
stat(_src, function(err, st) { | |
if (err) { | |
throw err; | |
} | |
if (st.isFile()) { | |
var readStream = fs.createReadStream(_src); | |
var writestream = fs.createWriteStream(_dest); | |
readStream.pipe(writestream); | |
} else if (st.isDirectory) { | |
doWithExists(_src, _dest, copy); | |
} | |
}); | |
}); | |
} | |
}); | |
} | |
function doWithExists(src, dest, callback) { | |
fs.exists(dest, function(exists) { | |
if (exists) { | |
callback(src, dest); | |
} else { | |
fs.mkdir(dest, function() { | |
callback(src, dest); | |
}); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment