javascript - Matching and writing data between 2 directories with node.js -
so i'll try , make sound simple possible, because don't understand im doing.
basically have 2 directories, sake of example call them 'a' , 'b'. 'a' directory other directories inside of it(inside files). 'b' directory itself, files in it, contain data.
i need match files , b, , write data b a.
this example code, not best because im extremely confused, tried make simple possible read
var id = [] function match(){ var = fs.readdirsync(__dirname+"/x/") var b = fs.readdirsync(__dirname+"/y/") id.push(a) for(o in id){ fs.readdirsync(__dirname+"/x/"+id[o]) // got confused, heres example if(file names in id[o] match b){ write data b, id[o] } } } match()
edit: aside moving files, need copy files. directory have files a,b,c. , directory b have a,b,d. , b being copied, , file d being left alone.
posit labs suggestion of a duplicate question in comments wise if want done. top rated answer suggests package ncp.
assuming you're in learn, however...
the objective function that
- takes source directory , target directory
- enumerates contents
- copies files source target
- makes source directories in target , recursively calls on each directory pair
be aware: solution very demo , should not used in production. use of recursion bite if file system deep, fact code synchronous (*sync) can slow down.
instead of string building paths, use path module. handles cross platform , strange inputs , edge cases.
first, 2 helper functions: ensuring directories exist should , copying file 1 path another. ultimately, these 2 real tasks in mirroring 2 directories.
var ensuredirectoryexists = function(path) { if (fs.existssync(path)) return; fs.mkdirsync(path); } var copyfile = function(sourcefile, targetfile) { if (fs.existssync(targetfile)) return; fs.writefilesync(targetfile, fs.readfilesync(sourcefile)); }
then main work. ensure directories exist , read contents.
ensuredirectoryexists(source); ensuredirectoryexists(target); var sourcelist = fs.readdirsync(source); var targetlist = fs.readdirsync(target);
then every element in source directory
sourcelist.foreach(function(sourcefile) { /* code */ })
quickly check if exists (just name. real work, should check types , checksum)
if (targetlist.indexof(sourcefile) > -1) return;
gather information on target (fs.stats detailed in manual)
var fullsourcepath = path.join(source, sourcefile); var fulltargetpath = path.join(target, sourcefile); var filestats = fs.statsync(fullsourcepath);
last, perform correct action based on type. files copied, directories ensured exist , recursively called, , else causes program shrug , keep going.
if (filestats.isfile()) { copyfile(fullsourcepath, fulltargetpath); } else if (filestats.isdirectory()) { copydirectories(fullsourcepath, fulltargetpath); } else { // there can other things in list. see https://nodejs.org/api/fs.html#fs_class_fs_stats console.log(`not sure ${sourcefile} is. ignoring.`); }
complete solution
var fs = require('fs'); var path = require('path'); var ensuredirectoryexists = function(path) { if (fs.existssync(path)) return; fs.mkdirsync(path); } var copyfile = function(sourcefile, targetfile) { // console.log(`copying ${sourcefile} ${targetfile}`); if (fs.existssync(targetfile)) return; fs.writefilesync(targetfile, fs.readfilesync(sourcefile)); // or streams. note ignores errors. read more here: https://stackoverflow.com/questions/11293857/fastest-way-to-copy-file-in-node-js // fs.createreadstream(sourcefile).pipe(fs.createwritestream(targetfile)); } var copydirectories = function(source, target) { // console.log(`copying ${source} ${target}`); // ensure directories exist ensuredirectoryexists(source); ensuredirectoryexists(target); var sourcelist = fs.readdirsync(source); var targetlist = fs.readdirsync(target); //sourcelist has contents of directories sourcelist.foreach(function(sourcefile) { // omit file it exists // note quite naive, work demo purposes if (targetlist.indexof(sourcefile) > -1) return; var fullsourcepath = path.join(source, sourcefile); var fulltargetpath = path.join(target, sourcefile); var filestats = fs.statsync(fullsourcepath); // console.log(`${sourcefile} ${filestats.isfile() ? 'file' : (filestats.isdirectory() ? 'directory' : '??')}.`); if (filestats.isfile()) { copyfile(fullsourcepath, fulltargetpath); } else if (filestats.isdirectory()) { copydirectories(fullsourcepath, fulltargetpath); } else { // there can other things in list. see https://nodejs.org/api/fs.html#fs_class_fs_stats console.log(`not sure ${sourcefile} is. ignoring.`); } }); } copydirectories( path.join(__dirname, 'source'), path.join(__dirname, 'target') )
Comments
Post a Comment