Hello, everyone!!
A have a trouble...
Because I want to apply method searchMask to the selected items by pushing button8.
I dont understand, how to connect them, because I dont know how to get path of files in string rootFolder in searchMask method
Could you help me with my challenge?
A have a trouble...
Because I want to apply method searchMask to the selected items by pushing button8.
I dont understand, how to connect them, because I dont know how to get path of files in string rootFolder in searchMask method
Could you help me with my challenge?
C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections;
namespace WindowsFormsApp4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button3_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button5_Click(object sender, EventArgs e)
{
FolderBrowserDialog FBD = new FolderBrowserDialog();
if (FBD.ShowDialog() == DialogResult.OK)
{
listBox1.Items.Clear();
string[] files = Directory.GetFiles(FBD.SelectedPath);
string[] dirs = Directory.GetDirectories(FBD.SelectedPath);
foreach (string file in files)
{
listBox1.Items.Add(Path.GetFileName(file));
}
listBox1.Sorted = true;
foreach (string dir in dirs)
{
listBox1.Items.Add(Path.GetFileName((dir)));
}
}
}
private void button8_Click(object sender, EventArgs e)
{
{
ListBox.SelectedObjectCollection selectedItems = new ListBox.SelectedObjectCollection(listBox1);
selectedItems = listBox1.SelectedItems;
if (listBox1.SelectedIndex != -1)
{
for (int i = selectedItems.Count - 1; i >= 0; i--)
searchMask();
}
}
}
static void searchMask()
{
string rootFolder = @"H:\Test1";
string pattern = @"\b(PlNamOld = FZG[0-9]*)\b";
using (StreamWriter sw = File.CreateText(@"C:\Users\Anton\Desktop\?#folder\target.txt"))
foreach (var file in Directory.EnumerateFiles(rootFolder, "*.txt", SearchOption.AllDirectories))
{
using (StreamReader sr = new StreamReader(file, System.Text.Encoding.Default))
{
string line;
while ((line = sr.ReadLine()) != null)
{
string newstring = line.Substring(0, 8);
Match match = Regex.Match(line, pattern, RegexOptions.IgnoreCase);
if (match.Success)
sw.WriteLine(file + " " + newstring + " " + match.Value);
else
sw.Write("");
}
}
}
}
}
}
Last edited: