Skip to content

Commit 12c172c

Browse files
committed
Implement MoveOverwrite as action
Closes #15
1 parent 04745d6 commit 12c172c

File tree

5 files changed

+94
-0
lines changed

5 files changed

+94
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
}

PatternFileMover/Actions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public enum AvailableActions
1616
Copy = 1,
1717
[Description("grid.Action.CopyOverwrite")]
1818
CopyOverwrite = 2,
19+
[Description("grid.Action.MoveOverwrite")]
20+
MoveOverwrite = 3,
1921
}
2022

2123
public class AvailableAction

PatternFileMover/NameAssociationsForm.de.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,7 @@
180180
<data name="grid.Action.CopyOverwrite" xml:space="preserve">
181181
<value>Kopieren (Überschreiben)</value>
182182
</data>
183+
<data name="grid.Action.MoveOverwrite" xml:space="preserve">
184+
<value>Verschieben (Überschreiben)</value>
185+
</data>
183186
</root>

PatternFileMover/NameAssociationsForm.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,4 +369,7 @@
369369
<data name="grid.Action.CopyOverwrite" xml:space="preserve">
370370
<value>Copy (Overwrite)</value>
371371
</data>
372+
<data name="grid.Action.MoveOverwrite" xml:space="preserve">
373+
<value>Move (Overwrite)</value>
374+
</data>
372375
</root>

PatternFileMover/PatternFileMover.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
<Compile Include="Action\CopyOverwrite.cs" />
5555
<Compile Include="Action\IAction.cs" />
5656
<Compile Include="Action\Move.cs" />
57+
<Compile Include="Action\MoveOverwrite.cs" />
5758
<Compile Include="Form2.cs">
5859
<SubType>Form</SubType>
5960
</Compile>

0 commit comments

Comments
 (0)