1
+ using System ;
2
+ using System . Xml . Serialization ;
3
+ using System . IO ;
4
+ using System . Windows . Forms ;
5
+ using System . Collections . Generic ;
6
+ using taskt . UI . Forms ;
7
+ using taskt . UI . CustomControls ;
8
+
9
+ namespace taskt . Core . Automation . Commands
10
+ {
11
+ [ Serializable ]
12
+ [ Attributes . ClassAttributes . Group ( "Folder Operation Commands" ) ]
13
+ [ Attributes . ClassAttributes . Description ( "This command moves a folder to a specified destination" ) ]
14
+ [ Attributes . ClassAttributes . UsesDescription ( "Use this command to move a folder to a new destination." ) ]
15
+ [ Attributes . ClassAttributes . ImplementationDescription ( "This command implements '' to achieve automation." ) ]
16
+ public class MoveFolderCommand : ScriptCommand
17
+ {
18
+ [ XmlAttribute ]
19
+ [ Attributes . PropertyAttributes . PropertyDescription ( "Indicate whether to move or copy the folder" ) ]
20
+ [ Attributes . PropertyAttributes . PropertyUISelectionOption ( "Move Folder" ) ]
21
+ [ Attributes . PropertyAttributes . PropertyUISelectionOption ( "Copy Folder" ) ]
22
+ [ Attributes . PropertyAttributes . InputSpecification ( "Specify whether you intend to move the folder or copy the folder. Moving will remove the folder from the original path while Copying will not." ) ]
23
+ [ Attributes . PropertyAttributes . SampleUsage ( "Select either **Move Folder** or **Copy Folder**" ) ]
24
+ [ Attributes . PropertyAttributes . Remarks ( "" ) ]
25
+ public string v_OperationType { get ; set ; }
26
+
27
+ [ XmlAttribute ]
28
+ [ Attributes . PropertyAttributes . PropertyDescription ( "Please indicate the path to the source folder" ) ]
29
+ [ Attributes . PropertyAttributes . PropertyUIHelper ( Attributes . PropertyAttributes . PropertyUIHelper . UIAdditionalHelperType . ShowVariableHelper ) ]
30
+ [ Attributes . PropertyAttributes . PropertyUIHelper ( Attributes . PropertyAttributes . PropertyUIHelper . UIAdditionalHelperType . ShowFolderSelectionHelper ) ]
31
+ [ Attributes . PropertyAttributes . InputSpecification ( "Enter or Select the path to the folder." ) ]
32
+ [ Attributes . PropertyAttributes . SampleUsage ( "C:\\ temp\\ myfolder or [vTextFolderPath]" ) ]
33
+ [ Attributes . PropertyAttributes . Remarks ( "" ) ]
34
+ public string v_SourceFolderPath { get ; set ; }
35
+
36
+ [ XmlAttribute ]
37
+ [ Attributes . PropertyAttributes . PropertyDescription ( "Please indicate the directory to move/copy to" ) ]
38
+ [ Attributes . PropertyAttributes . PropertyUIHelper ( Attributes . PropertyAttributes . PropertyUIHelper . UIAdditionalHelperType . ShowVariableHelper ) ]
39
+ [ Attributes . PropertyAttributes . PropertyUIHelper ( Attributes . PropertyAttributes . PropertyUIHelper . UIAdditionalHelperType . ShowFolderSelectionHelper ) ]
40
+ [ Attributes . PropertyAttributes . InputSpecification ( "Enter or Select the new path to the file." ) ]
41
+ [ Attributes . PropertyAttributes . SampleUsage ( "C:\\ temp\\ new path or [vTextFolderPath]" ) ]
42
+ [ Attributes . PropertyAttributes . Remarks ( "" ) ]
43
+ public string v_DestinationDirectory { get ; set ; }
44
+
45
+ [ XmlAttribute ]
46
+ [ Attributes . PropertyAttributes . PropertyDescription ( "Create folder if destination does not exist" ) ]
47
+ [ Attributes . PropertyAttributes . PropertyUISelectionOption ( "Yes" ) ]
48
+ [ Attributes . PropertyAttributes . PropertyUISelectionOption ( "No" ) ]
49
+ [ Attributes . PropertyAttributes . InputSpecification ( "Specify whether the directory should be created if it does not already exist." ) ]
50
+ [ Attributes . PropertyAttributes . SampleUsage ( "Select **Yes** or **No**" ) ]
51
+ [ Attributes . PropertyAttributes . Remarks ( "" ) ]
52
+ public string v_CreateDirectory { get ; set ; }
53
+
54
+ [ XmlAttribute ]
55
+ [ Attributes . PropertyAttributes . PropertyDescription ( "Delete folder if it already exists" ) ]
56
+ [ Attributes . PropertyAttributes . PropertyUISelectionOption ( "Yes" ) ]
57
+ [ Attributes . PropertyAttributes . PropertyUISelectionOption ( "No" ) ]
58
+ [ Attributes . PropertyAttributes . InputSpecification ( "Specify whether the folder should be deleted first if it is already found to exist." ) ]
59
+ [ Attributes . PropertyAttributes . SampleUsage ( "Select **Yes** or **No**" ) ]
60
+ [ Attributes . PropertyAttributes . Remarks ( "" ) ]
61
+ public string v_DeleteExisting { get ; set ; }
62
+
63
+ public MoveFolderCommand ( )
64
+ {
65
+ this . CommandName = "MoveFolderCommand" ;
66
+ this . SelectionName = "Move/Copy Folder" ;
67
+ this . CommandEnabled = true ;
68
+ this . CustomRendering = true ;
69
+ }
70
+
71
+ public override void RunCommand ( object sender )
72
+ {
73
+ //apply variable logic
74
+ var sourceFolder = v_SourceFolderPath . ConvertToUserVariable ( sender ) ;
75
+ var destinationFolder = v_DestinationDirectory . ConvertToUserVariable ( sender ) ;
76
+
77
+ if ( ( v_CreateDirectory == "Yes" ) && ( ! System . IO . Directory . Exists ( destinationFolder ) ) )
78
+ {
79
+ Directory . CreateDirectory ( destinationFolder ) ;
80
+ }
81
+
82
+ //get source folder name and info
83
+ DirectoryInfo sourceFolderInfo = new DirectoryInfo ( sourceFolder ) ;
84
+
85
+ //create final path
86
+ var finalPath = System . IO . Path . Combine ( destinationFolder , sourceFolderInfo . Name ) ;
87
+
88
+ //delete if it already exists per user
89
+ if ( v_DeleteExisting == "Yes" && System . IO . Directory . Exists ( finalPath ) )
90
+ {
91
+ Directory . Delete ( finalPath , true ) ;
92
+ }
93
+
94
+ if ( v_OperationType == "Move Folder" )
95
+ {
96
+ //move folder
97
+ Directory . Move ( sourceFolder , finalPath ) ;
98
+ }
99
+ else if ( v_OperationType == "Copy Folder" )
100
+ {
101
+ //copy folder
102
+ DirectoryCopy ( sourceFolder , finalPath , true ) ;
103
+ }
104
+
105
+ }
106
+ private void DirectoryCopy ( string sourceDirName , string destDirName , bool copySubDirs )
107
+ {
108
+ // Get the subdirectories for the specified directory.
109
+ DirectoryInfo dir = new DirectoryInfo ( sourceDirName ) ;
110
+
111
+ if ( ! dir . Exists )
112
+ {
113
+ throw new DirectoryNotFoundException (
114
+ "Source directory does not exist or could not be found: "
115
+ + sourceDirName ) ;
116
+ }
117
+
118
+ DirectoryInfo [ ] dirs = dir . GetDirectories ( ) ;
119
+ // If the destination directory doesn't exist, create it.
120
+ Directory . GetParent ( destDirName ) ;
121
+ if ( ! Directory . GetParent ( destDirName ) . Exists )
122
+ {
123
+ throw new DirectoryNotFoundException (
124
+ "Destination directory does not exist or could not be found: "
125
+ + Directory . GetParent ( destDirName ) ) ;
126
+ }
127
+
128
+ if ( ! Directory . Exists ( destDirName ) )
129
+ {
130
+ Directory . CreateDirectory ( destDirName ) ;
131
+ }
132
+
133
+ // Get the files in the directory and copy them to the new location.
134
+ FileInfo [ ] files = dir . GetFiles ( ) ;
135
+ foreach ( FileInfo file in files )
136
+ {
137
+ string temppath = Path . Combine ( destDirName , file . Name ) ;
138
+ file . CopyTo ( temppath , false ) ;
139
+ }
140
+
141
+ // If copying subdirectories, copy them and their contents to new location.
142
+ if ( copySubDirs )
143
+ {
144
+ foreach ( DirectoryInfo subdir in dirs )
145
+ {
146
+ string temppath = Path . Combine ( destDirName , subdir . Name ) ;
147
+ DirectoryCopy ( subdir . FullName , temppath , copySubDirs ) ;
148
+ }
149
+ }
150
+ }
151
+ public override List < Control > Render ( frmCommandEditor editor )
152
+ {
153
+ base . Render ( editor ) ;
154
+
155
+ RenderedControls . AddRange ( CommandControls . CreateDefaultDropdownGroupFor ( "v_OperationType" , this , editor ) ) ;
156
+ RenderedControls . AddRange ( CommandControls . CreateDefaultInputGroupFor ( "v_SourceFolderPath" , this , editor ) ) ;
157
+ RenderedControls . AddRange ( CommandControls . CreateDefaultInputGroupFor ( "v_DestinationDirectory" , this , editor ) ) ;
158
+ RenderedControls . AddRange ( CommandControls . CreateDefaultDropdownGroupFor ( "v_CreateDirectory" , this , editor ) ) ;
159
+ RenderedControls . AddRange ( CommandControls . CreateDefaultDropdownGroupFor ( "v_DeleteExisting" , this , editor ) ) ;
160
+ return RenderedControls ;
161
+ }
162
+
163
+ public override string GetDisplayValue ( )
164
+ {
165
+ return base . GetDisplayValue ( ) + " [" + v_OperationType + " from '" + v_SourceFolderPath + "' to '" + v_DestinationDirectory + "']" ;
166
+ }
167
+ }
168
+ }
0 commit comments