C# How to loop through the source folder or subfolders that match the destination -


i need overwrite files source destination directory. structure of each folder different i'm trying in generic way. thing is, each folder (source , destination) have numerous subdirectories or none @ all.

the code have this:

//copy , overwrite files depending on whatever in destination //search through destination find file foreach (var dstfile in directory.getfiles(targetdir)) {   //search through source find matching file   foreach (var srcfile  in directory.getfiles(sourcedir))   {      //cut off source file source path       strsrcfile = srcfile.split(path.directoryseparatorchar).last();      strdstfile = dstfile.split(path.directoryseparatorchar).last();      //if destination , source files match up, replace desination source     if (strsrcfile == strdstfile)     {       file.copy(srcfile, path.combine(targetdir, path.getfilename(strsrcfile)), true);     }   } }   //look through subfolders see if files match  foreach (var srcfolder in directory.getdirectories(sourcedir)) {    //search through source files     foreach (var srcfile in directory.getfiles(srcfolder))    {       //search through destination files        foreach (var dstfile in directory.getfiles(targetdir))       { 

as can see there lot of foreach loops, there way streamline this?

make hash (dictionary) of destination directory, walk source directory , see if files exist.

dictionary<string,string> lut1 = new dictionary<string,string>();  foreach (var dstfile in directory.getfiles(targetdir)) {    strdstfile = dstfile.split(path.directoryseparatorchar).last();    lut1 [strdstfile ] = dstfile; }  foreach (var srcfile  in directory.getfiles(sourcedir)) {    strsrcfile = srcfile.split(path.directoryseparatorchar).last();    string dstfile;    if (lut1.trygetvalue(strsrcfile,  out dstfile)) {        file.copy( srcfile,dstfile,true);    } }        

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -