Created
December 15, 2016 01:33
-
-
Save andreasbotsikas/c76f8c51949e547615a3e49392d6e579 to your computer and use it in GitHub Desktop.
Sync folders by copying only new files, ignoring moved files on target path
This file contains hidden or 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
| <?xml version="1.0" encoding="utf-8" ?> | |
| <configuration> | |
| <startup> | |
| <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> | |
| </startup> | |
| <appSettings> | |
| <add key="FromPath" value="C:\ftpRoot\mySite1;C:\ftpRoot\mySite2"/> | |
| <add key="ToPath" value="C:\ToProcess\mySite1;C:\ToProcess\mySite2"/> | |
| <add key="LogPath" value="C:\ToProcess\transferLog.txt"/> | |
| </appSettings> | |
| </configuration> |
This file contains hidden or 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
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| var fromPaths = ConfigurationManager.AppSettings["FromPath"].Split(';'); | |
| var toPaths = ConfigurationManager.AppSettings["ToPath"].Split(';'); | |
| if (fromPaths.Length != toPaths.Length) | |
| { | |
| throw new ApplicationException(string.Format("From paths must be equal to to paths. Got {0} from and {1} to", fromPaths.Length, toPaths.Length)); | |
| } | |
| var logFile = ConfigurationManager.AppSettings["LogPath"]; | |
| if (File.Exists(logFile)) | |
| { | |
| File.Delete(logFile); | |
| } | |
| for (var folderIndex = 0; folderIndex < fromPaths.Length; folderIndex++) { | |
| var toPath = toPaths[folderIndex]; | |
| var fromPath = fromPaths[folderIndex]; | |
| if (!Directory.Exists(toPath)) | |
| { | |
| throw new ApplicationException(string.Format("toPath {0} not exists", toPath)); | |
| } | |
| if (!Directory.Exists(fromPath)) | |
| { | |
| throw new ApplicationException(string.Format("fromPath {0} not exists", fromPath)); | |
| } | |
| // We need to copy all filed in the root of from path to the | |
| // location of toPath excluding the files that are not already in | |
| // a subfolder of the toPath | |
| using (var log = File.AppendText(logFile)) { | |
| log.WriteLine(String.Format("Processing files in folder {0}. Start on {1:dd/MM/yyyy hh:mm:ss}", fromPath, DateTime.Now)); | |
| var filesHashSet = new HashSet<string>(); | |
| GetListOfExistingFiles(toPath, filesHashSet); | |
| log.WriteLine(String.Format("Found {0} existing files on {1:dd/MM/yyyy hh:mm:ss}", filesHashSet.Count, DateTime.Now)); | |
| foreach (var f in Directory.EnumerateFiles(fromPath, "*", SearchOption.TopDirectoryOnly)) | |
| { | |
| var fileInfo = new FileInfo(f); | |
| if (!filesHashSet.Contains(fileInfo.Name)) | |
| { | |
| log.Write(String.Format("New file detected: {0} : ", fileInfo.Name)); | |
| fileInfo.CopyTo(Path.Combine(toPath, fileInfo.Name)); | |
| log.Write("Copied" + Environment.NewLine); | |
| } | |
| } | |
| log.WriteLine(String.Format("Finished on {0:dd/MM/yyyy hh:mm:ss}", DateTime.Now)); | |
| } | |
| } | |
| } | |
| private static void GetListOfExistingFiles(string toPath, HashSet<string> filesHashSet) | |
| { | |
| foreach (var f in Directory.EnumerateFiles(toPath, "*", SearchOption.AllDirectories)) | |
| { | |
| var fileInfo = new FileInfo(f); | |
| if (!filesHashSet.Contains(fileInfo.Name)) | |
| { | |
| filesHashSet.Add(fileInfo.Name); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment