public void Delete(Parameters parameters) { if (LongDirectory.Exists(Path)) { string[] paths = LongDirectory.GetFiles(Path); foreach (string filePath in paths) { if (parameters.Verbose) { Console.Out.WriteLine("{0}", filePath); } LongFile.Delete(filePath); } if (parameters.Recursive) { string[] subDirs = LongDirectory.GetDirectories(Path); foreach (string subDir in subDirs) { PFolder pFolder = new PFolder(subDir); pFolder.Delete(parameters); } } if (parameters.Verbose) { Console.Out.WriteLine("{0}", Path); } LongDirectory.Delete(Path); } else if (LongFile.Exists(Path)) { if (parameters.Verbose) { Console.Out.WriteLine("{0}", Path); } LongFile.Delete(Path); } if (Path == parameters.Path) { if (parameters.RequireEnterToExit) { Console.Out.WriteLine("Press enter to exit"); Console.In.ReadLine(); } } }
internal void QueueDelta(Delta d) { string archivefolder = _conn.Path + ".ts" + _S + "old" + _S; Directory.CreateDirectory(archivefolder); if (d != null) { // empty old error queue _ErrorQue = new ConcurrentQueue <SyncFile>(); d.FilesDeleted.ForEach(x => { try { if (LongFile.Exists(_conn.Path + x)) { _log.Info("Deleting file : " + _conn.Path + x); string fn = archivefolder + x; LongFile.Move(_conn.Path + x, fn); } } catch (Exception ex) { _log.Error(ex); } }); d.FoldersDeleted.ForEach(x => { _log.Info("Deleting folder : " + _conn.Path + x); if (LongDirectory.Exists(_conn.Path + x)) { try { LongDirectory.Move(_conn.Path + x, archivefolder + x); } catch { } try { var f = LongDirectory.GetFiles(_conn.Path + x, "*.*", SearchOption.AllDirectories); if (f.Length == 0) { LongDirectory.Delete(_conn.Path + x, true); } } catch (Exception ex) { _log.Error(ex); } } }); d.FilesAdded.ForEach(x => { _que.Enqueue(x); }); d.FilesChanged.ForEach(x => { _que.Enqueue(x); }); _log.Info("delfolders = " + d.FoldersDeleted.Count); _log.Info("delfiles = " + d.FilesDeleted.Count); _log.Info("files added = " + d.FilesAdded.Count); _log.Info("files changed = " + d.FilesChanged.Count); _log.Info("que count = " + _que.Count); SaveQueue(); } }
public void DoAction() { try { if (ItemType == SyncItemType.Folder) { SyncedFile past1 = MainDbContext.DB.SyncedFiles.FirstOrDefault(f => f.Path == Path1); SyncedFile past2 = MainDbContext.DB.SyncedFiles.FirstOrDefault(f => f.Path == Path2); if (Action == SyncActions.CopyFrom1To2) { if (!LongDirectory.Exists(Path2)) { LongDirectory.CreateDirectory(Path2); } if (past1 == null) { MainDbContext.DB.SyncedFiles.Add(new SyncedFile() { LastModified = DateTime.MinValue, Path = Path1 }); } if (past2 == null) { MainDbContext.DB.SyncedFiles.Add(new SyncedFile() { LastModified = DateTime.MinValue, Path = Path2 }); } Status = SyncItemStatus.ActionOK; } else if (Action == SyncActions.CopyFrom2To1) { if (!LongDirectory.Exists(Path1)) { LongDirectory.CreateDirectory(Path1); } if (past1 == null) { MainDbContext.DB.SyncedFiles.Add(new SyncedFile() { LastModified = DateTime.MinValue, Path = Path1 }); } if (past2 == null) { MainDbContext.DB.SyncedFiles.Add(new SyncedFile() { LastModified = DateTime.MinValue, Path = Path2 }); } Status = SyncItemStatus.ActionOK; } else if (Action == SyncActions.Delete1 || Action == SyncActions.Delete2 || Action == SyncActions.DeleteBoth) { if (LongDirectory.Exists(Path1)) { LongDirectory.Delete(Path1, true); } if (LongDirectory.Exists(Path2)) { LongDirectory.Delete(Path2, true); } if (past1 != null) { MainDbContext.DB.SyncedFiles.Remove(past1); } if (past2 != null) { MainDbContext.DB.SyncedFiles.Remove(past2); } Status = SyncItemStatus.ActionOK; } } else if (ItemType == SyncItemType.File) { SyncedFile past1 = MainDbContext.DB.SyncedFiles.FirstOrDefault(f => f.Path == Path1); SyncedFile past2 = MainDbContext.DB.SyncedFiles.FirstOrDefault(f => f.Path == Path2); if (Action == SyncActions.CopyFrom1To2) { LongFile.Copy(Path1, Path2); DateTime lastModified = LongFile.GetLastWriteTime(Path1); if (past1 == null) { past1 = new SyncedFile() { LastModified = lastModified, Path = Path1 }; MainDbContext.DB.SyncedFiles.Add(past1); } else if (!lastModified.Equals(past1.LastModified)) { past1.LastModified = lastModified; } if (past2 == null) { past2 = new SyncedFile() { LastModified = lastModified, Path = Path2 }; MainDbContext.DB.SyncedFiles.Add(past2); } else if (!lastModified.Equals(past2.LastModified)) { past2.LastModified = lastModified; } Status = SyncItemStatus.ActionOK; } else if (Action == SyncActions.CopyFrom2To1) { LongFile.Copy(Path2, Path1); DateTime lastModified = LongFile.GetLastWriteTime(Path2); if (past1 == null) { past1 = new SyncedFile() { LastModified = lastModified, Path = Path1 }; MainDbContext.DB.SyncedFiles.Add(past1); } else if (!lastModified.Equals(past1.LastModified)) { past1.LastModified = lastModified; } if (past2 == null) { past2 = new SyncedFile() { LastModified = lastModified, Path = Path2 }; MainDbContext.DB.SyncedFiles.Add(past2); } else if (!lastModified.Equals(past2.LastModified)) { past2.LastModified = lastModified; } Status = SyncItemStatus.ActionOK; } else if (Action == SyncActions.Delete1) { if (LongFile.Exists(Path1)) { LongFile.Delete(Path1); } if (past1 != null) { MainDbContext.DB.SyncedFiles.Remove(past1); } if (past2 != null) { MainDbContext.DB.SyncedFiles.Remove(past2); } Status = SyncItemStatus.ActionOK; } else if (Action == SyncActions.Delete2) { if (LongFile.Exists(Path2)) { LongFile.Delete(Path2); } if (past1 != null) { MainDbContext.DB.SyncedFiles.Remove(past1); } if (past2 != null) { MainDbContext.DB.SyncedFiles.Remove(past2); } Status = SyncItemStatus.ActionOK; } else if (Action == SyncActions.DeleteBoth) { if (LongFile.Exists(Path1)) { LongFile.Delete(Path1); } if (LongFile.Exists(Path2)) { LongFile.Delete(Path2); } if (past1 != null) { MainDbContext.DB.SyncedFiles.Remove(past1); } if (past2 != null) { MainDbContext.DB.SyncedFiles.Remove(past2); } Status = SyncItemStatus.ActionOK; } } MainDbContext.DB.SaveChanges(); } catch (Exception ex) { //TODO What to do when a copy/delete doesnt work? } }