Skip to content

Commit a126fed

Browse files
authored
Merge pull request #189 from wladneto/development-branch
Add new command to extract file and bug fix to issue 196
2 parents ced52b5 + 30af174 commit a126fed

File tree

7 files changed

+221
-5
lines changed

7 files changed

+221
-5
lines changed

taskt.sln

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 15
4-
VisualStudioVersion = 15.0.26730.10
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29613.14
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "taskt", "taskt\taskt.csproj", "{C1BE3204-94D1-4A9A-AE30-C3E302383182}"
77
EndProject
@@ -24,7 +24,6 @@ Global
2424
{C1BE3204-94D1-4A9A-AE30-C3E302383182}.Release|x64.ActiveCfg = Release|x64
2525
{C1BE3204-94D1-4A9A-AE30-C3E302383182}.Release|x64.Build.0 = Release|x64
2626
{3FC72D0C-D083-44C4-96E9-13FE599F8E0E}.Debug|Any CPU.ActiveCfg = Release|Any CPU
27-
{3FC72D0C-D083-44C4-96E9-13FE599F8E0E}.Debug|Any CPU.Build.0 = Release|Any CPU
2827
{3FC72D0C-D083-44C4-96E9-13FE599F8E0E}.Debug|x64.ActiveCfg = Debug|x64
2928
{3FC72D0C-D083-44C4-96E9-13FE599F8E0E}.Debug|x64.Build.0 = Debug|x64
3029
{3FC72D0C-D083-44C4-96E9-13FE599F8E0E}.Release|Any CPU.ActiveCfg = Release|Any CPU
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.IO;
5+
using System.Windows.Forms;
6+
using System.Xml.Serialization;
7+
using taskt.Core.IO;
8+
using taskt.UI.CustomControls;
9+
using taskt.UI.Forms;
10+
11+
namespace taskt.Core.Automation.Commands
12+
{
13+
[Serializable]
14+
[Attributes.ClassAttributes.Group("File Operation Commands")]
15+
[Attributes.ClassAttributes.Description("")]
16+
[Attributes.ClassAttributes.UsesDescription("")]
17+
[Attributes.ClassAttributes.ImplementationDescription("")]
18+
public class ExtractFileCommand : ScriptCommand
19+
{
20+
[XmlAttribute]
21+
[Attributes.PropertyAttributes.PropertyDescription("Please select file type")]
22+
[Attributes.PropertyAttributes.PropertyUISelectionOption("zip")]
23+
[Attributes.PropertyAttributes.PropertyUISelectionOption("7z")]
24+
[Attributes.PropertyAttributes.PropertyUISelectionOption("xz")]
25+
[Attributes.PropertyAttributes.PropertyUISelectionOption("bzip2")]
26+
[Attributes.PropertyAttributes.PropertyUISelectionOption("tar")]
27+
[Attributes.PropertyAttributes.PropertyUISelectionOption("wim")]
28+
[Attributes.PropertyAttributes.PropertyUISelectionOption("iso")]
29+
[Attributes.PropertyAttributes.InputSpecification("Select source file type")]
30+
[Attributes.PropertyAttributes.SampleUsage("Select **Type file**")]
31+
[Attributes.PropertyAttributes.Remarks("")]
32+
public string v_FileType { get; set; }
33+
34+
[XmlAttribute]
35+
[Attributes.PropertyAttributes.PropertyDescription("Please indicate the file path or file URL to be extract")]
36+
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowFileSelectionHelper)]
37+
[Attributes.PropertyAttributes.InputSpecification("Enter or Select the path to the applicable file or enter file URL.")]
38+
[Attributes.PropertyAttributes.SampleUsage(@"C:\temp\myfile.zip , [vFilePath] or https://temp.com/myfile.zip")]
39+
[Attributes.PropertyAttributes.Remarks("")]
40+
public string v_FilePathOrigin { get; set; }
41+
42+
[XmlAttribute]
43+
[Attributes.PropertyAttributes.PropertyDescription("Please select source file")]
44+
[Attributes.PropertyAttributes.PropertyUISelectionOption("File Path")]
45+
[Attributes.PropertyAttributes.PropertyUISelectionOption("File URL")]
46+
[Attributes.PropertyAttributes.InputSpecification("Select source path")]
47+
[Attributes.PropertyAttributes.SampleUsage("Select **File Path**, **File URL**")]
48+
[Attributes.PropertyAttributes.Remarks("")]
49+
public string v_FileSourceType { get; set; }
50+
51+
[XmlAttribute]
52+
[Attributes.PropertyAttributes.PropertyDescription("Please indicate the file path to be send after extract")]
53+
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowFileSelectionHelper)]
54+
[Attributes.PropertyAttributes.InputSpecification("Enter or Select the path to the applicable file or enter file URL.")]
55+
[Attributes.PropertyAttributes.SampleUsage(@"C:\temp\ or [vFilePath]")]
56+
[Attributes.PropertyAttributes.Remarks("")]
57+
public string v_PathDestination { get; set; }
58+
59+
[XmlAttribute]
60+
[Attributes.PropertyAttributes.PropertyDescription("Please select the variable to receive the list of extract files")]
61+
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
62+
[Attributes.PropertyAttributes.InputSpecification("Select or provide a variable from the variable list")]
63+
[Attributes.PropertyAttributes.SampleUsage("**vSomeVariable**")]
64+
[Attributes.PropertyAttributes.Remarks("If you have enabled the setting **Create Missing Variables at Runtime** then you are not required to pre-define your variables, however, it is highly recommended.")]
65+
public string v_applyToVariableName { get; set; }
66+
67+
public ExtractFileCommand()
68+
{
69+
this.CommandName = "ExtractFileCommand";
70+
this.SelectionName = "File Extraction";
71+
this.CommandEnabled = true;
72+
this.CustomRendering = true;
73+
}
74+
75+
public override void RunCommand(object sender)
76+
{
77+
// get type of file either from a physical file or from a URL
78+
var vFileType = v_FileType.ConvertToUserVariable(sender);
79+
//get variable path or URL to source file
80+
var vSourceFilePathOrigin = v_FilePathOrigin.ConvertToUserVariable(sender);
81+
// get source type of file either from a physical file or from a URL
82+
var vSourceFileType = v_FileSourceType.ConvertToUserVariable(sender);
83+
// get file path to destination files
84+
var vFilePathDestination = v_PathDestination.ConvertToUserVariable(sender);
85+
// get file path to destination files
86+
var v_VariableName = v_applyToVariableName.ConvertToUserVariable(sender);
87+
88+
if (vSourceFileType == "File URL")
89+
{
90+
//create temp directory
91+
var tempDir = Core.IO.Folders.GetFolder(Folders.FolderType.TempFolder);
92+
var tempFile = System.IO.Path.Combine(tempDir, $"{ Guid.NewGuid()}." + vFileType);
93+
94+
//check if directory does not exist then create directory
95+
if (!System.IO.Directory.Exists(tempDir))
96+
{
97+
System.IO.Directory.CreateDirectory(tempDir);
98+
}
99+
100+
// Create webClient to download the file for extraction
101+
var webclient = new System.Net.WebClient();
102+
var uri = new Uri(vSourceFilePathOrigin);
103+
webclient.DownloadFile(uri, tempFile);
104+
105+
// check if file is downloaded successfully
106+
if (System.IO.File.Exists(tempFile))
107+
{
108+
vSourceFilePathOrigin = tempFile;
109+
}
110+
111+
// Free not needed resources
112+
uri = null;
113+
if (webclient != null)
114+
{
115+
webclient.Dispose();
116+
webclient = null;
117+
}
118+
}
119+
120+
// Check if file exists before proceeding
121+
if (!System.IO.File.Exists(vSourceFilePathOrigin))
122+
{
123+
throw new System.IO.FileNotFoundException("Could not find file: " + vSourceFilePathOrigin);
124+
}
125+
126+
// Get 7Z app
127+
var zPath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath), "Resources", "7z.exe");
128+
129+
// If the directory doesn't exist, create it.
130+
if (!Directory.Exists(vFilePathDestination))
131+
Directory.CreateDirectory(vFilePathDestination);
132+
133+
var result = "";
134+
System.Diagnostics.Process process = new System.Diagnostics.Process();
135+
136+
try
137+
{
138+
var temp = Guid.NewGuid();
139+
//Extract in temp to get list files and directories and delete
140+
ProcessStartInfo pro = new ProcessStartInfo();
141+
pro.WindowStyle = ProcessWindowStyle.Hidden;
142+
pro.UseShellExecute = false;
143+
pro.FileName = zPath;
144+
pro.RedirectStandardOutput = true;
145+
pro.Arguments = "x " + vSourceFilePathOrigin + " -o" + vFilePathDestination + "/" + temp + " -aoa";
146+
process.StartInfo = pro;
147+
process.Start();
148+
process.WaitForExit();
149+
string[] dirPaths = Directory.GetDirectories(vFilePathDestination + "/" + temp, "*", SearchOption.TopDirectoryOnly);
150+
string[] filePaths = Directory.GetFiles(vFilePathDestination + "/" + temp, "*", SearchOption.TopDirectoryOnly);
151+
152+
foreach (var item in dirPaths)
153+
{
154+
result = result + item + Environment.NewLine;
155+
}
156+
foreach (var item in filePaths)
157+
{
158+
result = result + item + Environment.NewLine;
159+
}
160+
result = result.Replace("/" + temp, "");
161+
Directory.Delete(vFilePathDestination + "/" + temp, true);
162+
163+
//Extract
164+
pro.Arguments = "x " + vSourceFilePathOrigin + " -o" + vFilePathDestination + " -aoa";
165+
process.StartInfo = pro;
166+
process.Start();
167+
process.WaitForExit();
168+
169+
170+
result.StoreInUserVariable(sender, v_applyToVariableName);
171+
}
172+
catch (System.Exception Ex)
173+
{
174+
process.Kill();
175+
v_applyToVariableName = Ex.Message;
176+
}
177+
178+
179+
180+
181+
182+
183+
184+
}
185+
public override List<Control> Render(frmCommandEditor editor)
186+
{
187+
base.Render(editor);
188+
189+
//create standard group controls
190+
RenderedControls.AddRange(CommandControls.CreateDefaultDropdownGroupFor("v_FileType", this, editor));
191+
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_FilePathOrigin", this, editor));
192+
RenderedControls.AddRange(CommandControls.CreateDefaultDropdownGroupFor("v_FileSourceType", this, editor));
193+
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_PathDestination", this, editor));
194+
RenderedControls.Add(CommandControls.CreateDefaultLabelFor("v_applyToVariableName", this));
195+
var VariableNameControl = CommandControls.CreateStandardComboboxFor("v_applyToVariableName", this).AddVariableNames(editor);
196+
RenderedControls.AddRange(CommandControls.CreateUIHelpersFor("v_applyToVariableName", this, new Control[] { VariableNameControl }, editor));
197+
RenderedControls.Add(VariableNameControl);
198+
199+
return RenderedControls;
200+
201+
}
202+
203+
public override string GetDisplayValue()
204+
{
205+
return base.GetDisplayValue() + " [Extract From '" + v_FilePathOrigin + " to " + v_PathDestination + "' and apply result to '" + v_applyToVariableName + "'";
206+
}
207+
}
208+
}

taskt/Core/Automation/Commands/ScriptCommand.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ namespace taskt.Core.Automation.Commands
149149
[XmlInclude(typeof(ParseJsonModelCommand))]
150150
[XmlInclude(typeof(EncryptionCommand))]
151151
[XmlInclude(typeof(MathCalculationCommand))]
152-
[XmlInclude(typeof(SetEnginePreferenceCommand))]
152+
[XmlInclude(typeof(SetEnginePreferenceCommand))]
153+
[XmlInclude(typeof(ExtractFileCommand))]
153154
public abstract class ScriptCommand
154155
{
155156
[XmlAttribute]

taskt/Core/Automation/Commands/TryCommand.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ public override void RunCommand(object sender, Script.ScriptAction parentCommand
6363
{
6464
engine.ExecuteCommand(parentCommand.AdditionalScriptCommands[j]);
6565
}
66-
66+
break;
67+
6768
}
6869

6970
}

taskt/Resources/7z.dll

1.6 MB
Binary file not shown.

taskt/Resources/7z.exe

451 KB
Binary file not shown.

taskt/taskt.csproj

+7
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@
167167
<Compile Include="Core\Automation\Commands\AddDictionaryCommand.cs" />
168168
<Compile Include="Core\Automation\Commands\CreateDictionaryCommand.cs" />
169169
<Compile Include="Core\Automation\Commands\BeginMutliIfCommand.cs" />
170+
<Compile Include="Core\Automation\Commands\ExtractFileCommand.cs" />
170171
<Compile Include="Core\Automation\Commands\NextLoopCommand.cs" />
171172
<Compile Include="Core\Automation\Commands\OutlookEmailCommand.cs" />
172173
<Compile Include="Core\Automation\Commands\GetRegexMatchesCommand.cs" />
@@ -738,6 +739,12 @@
738739
<None Include="Resources\command-remote.png" />
739740
<None Include="Resources\command-begin-multi-if.png" />
740741
<None Include="Resources\command-nextloop.png" />
742+
<Content Include="Resources\7z.dll">
743+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
744+
</Content>
745+
<Content Include="Resources\7z.exe">
746+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
747+
</Content>
741748
<Content Include="Resources\IEDriverServer.exe">
742749
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
743750
</Content>

0 commit comments

Comments
 (0)