|
| 1 | +using System.IO; |
| 2 | +using System.Windows.Forms; |
| 3 | + |
| 4 | +namespace PatternFileMover.Action |
| 5 | +{ |
| 6 | + internal class MoveOverwrite : AbstractAction |
| 7 | + { |
| 8 | + public bool DoMoveOverwrite() |
| 9 | + { |
| 10 | + try |
| 11 | + { |
| 12 | + File.Move( |
| 13 | + this.GetSourcePath(), |
| 14 | + this.GetTargetPath() + |
| 15 | + Path.DirectorySeparatorChar + |
| 16 | + Path.GetFileName(this.GetCurrent().Cells[0].Value.ToString() |
| 17 | + ) |
| 18 | + ); |
| 19 | + } |
| 20 | + catch (IOException) |
| 21 | + { |
| 22 | + return false; |
| 23 | + } |
| 24 | + |
| 25 | + return true; |
| 26 | + } |
| 27 | + |
| 28 | + public override bool ExecuteAction() |
| 29 | + { |
| 30 | + return base.ExecuteAction(); |
| 31 | + } |
| 32 | + |
| 33 | + public override bool Matches(DataGridViewRow row) |
| 34 | + { |
| 35 | + if (!base.Matches(row)) |
| 36 | + { |
| 37 | + return false; |
| 38 | + } |
| 39 | + |
| 40 | + if ( |
| 41 | + ( |
| 42 | + this.GetFileExtension() == "*.*" && |
| 43 | + Path.GetFileNameWithoutExtension( |
| 44 | + this.GetCurrent().Cells[(int)NameAssociationCellIndex.Name].Value.ToString() |
| 45 | + ).Contains(this.GetSearchPattern()) |
| 46 | + ) || |
| 47 | + ( |
| 48 | + Path.GetFileNameWithoutExtension( |
| 49 | + this.GetCurrent().Cells[(int)NameAssociationCellIndex.Name].Value.ToString() |
| 50 | + ).Contains(this.GetSearchPattern()) && |
| 51 | + this.GetFileExtension() == Path.GetExtension(this.GetCurrent().Cells[(int)NameAssociationCellIndex.Name].Value.ToString()) |
| 52 | + ) && |
| 53 | + ( |
| 54 | + Directory.Exists(this.GetTargetPath() + Path.DirectorySeparatorChar) |
| 55 | + ) |
| 56 | + ) |
| 57 | + { |
| 58 | + return true; |
| 59 | + } |
| 60 | + |
| 61 | + return false; |
| 62 | + } |
| 63 | + |
| 64 | + public override void Prepare() |
| 65 | + { |
| 66 | + base.Prepare(); |
| 67 | + |
| 68 | + if (File.Exists( |
| 69 | + this.GetTargetPath() + |
| 70 | + Path.DirectorySeparatorChar + |
| 71 | + Path.GetFileName(this.GetCurrent().Cells[0].Value.ToString()) |
| 72 | + ) |
| 73 | + ) |
| 74 | + { |
| 75 | + // delete the existing file before it gets replaced with the current |
| 76 | + // processed file |
| 77 | + File.Delete( |
| 78 | + this.GetTargetPath() + |
| 79 | + Path.DirectorySeparatorChar + |
| 80 | + Path.GetFileName(this.GetCurrent().Cells[0].Value.ToString()) |
| 81 | + ); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments