Advertisement
Guest User

filter/find

a guest
Feb 14th, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.39 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Management.Automation;
  11. using System.Management.Automation.Runspaces;
  12. using System.Collections.ObjectModel;
  13.  
  14. namespace WindowsFormsApplication1
  15. {
  16.     public partial class Form3 : Form
  17.     {
  18.         public Form3()
  19.         {
  20.             InitializeComponent();
  21.             label5.Hide();
  22.             label6.Hide();
  23.         }
  24.  
  25.         private void button1_Click(object sender, EventArgs e)
  26.         {
  27.             using (var dialog = new System.Windows.Forms.FolderBrowserDialog())
  28.             {
  29.                 System.Windows.Forms.DialogResult result = dialog.ShowDialog();
  30.                 label5.Text = dialog.SelectedPath;
  31.                 label5.Show();
  32.             }
  33.            
  34.            
  35.         }
  36.  
  37.         private void button3_Click(object sender, EventArgs e)
  38.         {
  39.             this.Hide();
  40.             Form1 myForm = new Form1();
  41.             myForm.Show();
  42.         }
  43.  
  44.         private void button5_Click(object sender, EventArgs e)
  45.         {
  46.             this.Hide();
  47.             Form1 myForm = new Form1();
  48.             myForm.Show();
  49.         }
  50.  
  51.         private void button6_Click(object sender, EventArgs e)
  52.         {
  53.             this.Hide();
  54.             Form3 myForm = new Form3();
  55.             myForm.Show();
  56.         }
  57.  
  58.         private void button2_Click(object sender, EventArgs e)
  59.         {
  60.             using (var dialog = new System.Windows.Forms.OpenFileDialog())
  61.             {
  62.                 System.Windows.Forms.DialogResult result = dialog.ShowDialog();
  63.                 label6.Text = dialog.FileName;
  64.                 label6.Show();
  65.             }
  66.         }
  67.  
  68.         private void button4_Click(object sender, EventArgs e)
  69.         {
  70.             string filter = textBox1.Text;
  71.             string folder = label5.Text;
  72.             string output = label6.Text;
  73.  
  74.             String[] substrings = folder.Split('\\');
  75.             folder = "";
  76.             foreach(string substring in substrings){
  77.                 folder = folder + substring + "\\" + "\\";
  78.             }
  79.  
  80.             String[] substringsOutput = output.Split('\\');
  81.             output = "";
  82.             foreach (string substringOutput in substringsOutput)
  83.             {
  84.                 output = output + substringOutput + "\\" + "\\";
  85.             }
  86.  
  87.             output = output.Remove(output.Length - 2);
  88.            
  89.             Runspace runspace = RunspaceFactory.CreateRunspace();
  90.  
  91.             runspace.Open();
  92.  
  93.             Pipeline pipeline = runspace.CreatePipeline();
  94.             var filterEscaped = "\"" + filter + "\\" + "\"";
  95.  
  96.             string jqExecuteCommand = "jq -r 'select(.text | tostring | contains(\\" + filterEscaped + ")) | [.text] | select(length > 0) | @csv' " + " $files[$i].FullName " + "  >> " + output;
  97.             string fileLoopCommand = "$files = Get-ChildItem " + folder + " -Recurse -Include *.json" + "\n" + "for($i=0; $i -lt $files.Count; $i++) {" + jqExecuteCommand +"}";
  98.  
  99.             pipeline.Commands.AddScript(fileLoopCommand);
  100.  
  101.             pipeline.Commands.Add("Out-String");
  102.  
  103.             Collection<PSObject> results = pipeline.Invoke("Set-ExecutionPolicy Unrestricted");
  104.  
  105.             runspace.Close();
  106.  
  107.         }
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement