Move files from multiple directories to one

 
Below is a simple code snippet to move files from multiple recursive directories into a separate directory.
e.g. Let's say I have a directory called 'Music' and it contains directories like 'Album1', 'Album2' etc. Then below code will moves all the files (not the directories) from Album1 and Album2 to specified directory.




arguments: 
   source: source directory path (Path to the directory 'Music' in above example).
   target: path of the directory where you want to move the files.



        static void Do(string source, string target)
        {
            foreach (var src in Directory.GetDirectories(source))
            {
                foreach (var file in Directory.GetFiles(src.ToString()))
                {
                    FileInfo f = new FileInfo(file);
                    File.Move(file, target + "\\" + f.Name);
                }

                foreach (var dir in Directory.GetDirectories(src.ToString()))
                {
                    Do(src.ToString(), target);
                }
            }
        }

aj1490

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard.

0 comments: