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; namespace WindowsFormsApp1 { public partial class fileSearcher : Form { string lineReader; bool convertDateSought; bool convertDateFound; int numDateSought; int numDateFound; string shortListString; int scratchPad; bool firstLineMatches; List shortList = new List(); List dateFoundList = new List(); List newShinyList = new List(); // Making an array that loads itself with all text (.txt) files found in the // folder (C:\DATA\Shortcut to Clients), and its subfolders String[] all = Directory.GetFiles(@"C:\First Folder\Sub Folder", "*.txt", SearchOption.AllDirectories); public fileSearcher() { InitializeComponent(); } // Method called upon when user clicks search button private void searchButton_Click(object sender, EventArgs e) { // Calling reset method to clear all variables. I put it here vs end of this method b/c code // doesn't always go to end ie. in instance of an error and code stops running b/c of "return;" reset(sender, e); // if data entered in "Searching For" textbox is 10 characters if (searchTextBox.Text.Length == 10) { // and if data (minus where dashes s/b) in "Searching for" textbox is able to be // converted to a number, convertDatSought will equal "true" convertDateSought = int.TryParse(searchTextBox.Text.Remove(4, 1).Remove(6, 1), out numDateSought); // if imm. above not able to be converted to an integer... if (!convertDateSought) { // show message MessageBox.Show($"The date you are seeking cannot be converted into an integer.\n\n" + $"Code ceasing to run."); // stop running code return; } } else // if text in search textbox is NOT 10 digits... { // show message box... MessageBox.Show("You are searching for a date, which is not (but should be) 10 digits and in the format yyyy-mm-dd.\n\n" + "Code ceasing to run."); // stop running code return; } // run the following loop as many times as is necessary to run through all spots in the "all" array for (int i = 0; i < all.Length; i++) { // making the data in the file, which is referenced by the address in the // present "all" array spot, readable StreamReader source = new StreamReader(all[i]); // provided the current file in question has text to be read while (source.Peek() != -1) { // make lineReader equal to present line of code being read lineReader = source.ReadLine(); // if imm. above reads "commitmentDateTextBox" if ("commitmentDateTextBox" == lineReader) { firstLineMatches = true; // make bool variable true } if (firstLineMatches) // if imm. above bool var. is true { firstLineMatches = false; // switch it back to false // read next line lineReader = source.ReadLine(); // if the next line (read imm. above) is 10 char.'s in length if (lineReader.Length == 10) { // if the immediately above, minus characters where the dashes should be, // can be converted to an integer, output numDateFound and make convertDateFound true convertDateFound = int.TryParse(lineReader.Remove(4, 1).Remove(6, 1), out numDateFound); // if imm. above can't be converted if (convertDateFound != true) { // show message MessageBox.Show($"One of the lines of code, \"{lineReader}\" that you were trying to convert to an integer, in the source file \"{all[i]}\" could not be converted.\n\nCode ceasing to run."); // end return; } // if number can be converted and // if number found in latest line read is equal to, or less than, // number sought else if (numDateFound <= numDateSought) { // add latest line read (with dashes) + word "break" + current file address in question + line break // to shortList shortList.Add(lineReader + "break" + all[i] + "\n"); // add converted date found, without dashes, converted to an integer // to dateFoundList dateFoundList.Add(numDateFound); } } else // if latest line read is not 10 digits { // display message DialogResult result = MessageBox.Show($"The source file in question:\n{all[i]}\n\nHas a line: {lineReader}\n\nWhich is not (but should be) 10 digits and in the format yyyy-mm-dd.\n\n" + "Do you want to open the problem source file?", "Problem in Source File", MessageBoxButtons.YesNo); // if user selects "yes" button if (result == DialogResult.Yes) { // open file in question so errors can be addressed System.Diagnostics.Process.Start($"{all[i]}"); } else // if user didn't hit "yes" { // end return; } } } } } // sort list of converted dates (w/o dashes) dateFoundList.Sort(); // reverse list (because without this, it shows the most recent dates at the bottom // and I wanted them at the top) dateFoundList.Reverse(); // for each item in the list of dates found foreach (int h in dateFoundList) { // add an empty spot in an up-to-now unused list newShinyList.Add(""); } // this loop runs as many times as is necessary to run through list of dates found for (int i = 0; i < dateFoundList.Count(); i++) { // this loop runs as many times as is necessary to run through list of dates <= date sought for (int j = 0; j < shortList.Count(); j++) { // if short list of strings, culled down to just dates w/o dashes, can be converted to an integer if (int.TryParse(shortList[j].Remove(shortList[j].IndexOf("break"), shortList[j].Length - shortList[j].IndexOf("break")).Remove(4, 1).Remove(6, 1), out scratchPad)) { // and if result from imm. above matches present date in question in sorted list of dates found if (dateFoundList[i].ToString() == scratchPad.ToString()) { // at this point the current date in the sorted list of dates found // and the current date in the culled list of dates (that are <= the date sought), match // since the sorted list of dates found is sorted, we are using it as the index // the following replaces the empty spot in the new list "newShinyList" that corresponds // with the date in question in the sorted list of dates found // and inserts the string from shortList, but replaces "break" with a // tab followed by two line breaks newShinyList[i] = (shortList[j].Replace("break", "\t") + "\n\n"); // clears scratchPad variable scratchPad = 0; // makes shortList spot that was just matched, into a string that can be read // ie. if it didn't have a certain number of convertible integers (ie. 0), nor the word "break" // then it would throw an error when later loops were trying to read it // it is important that it doesn't match any of the other dates in the // sorted list of dates found or else it might show in the final list shortList[j] = "00000000break000000000000000000"; } } // there is no "else" b/c we know it can be converted, as done previously } } // for each entry in this list foreach (string s in newShinyList) { // if entry in newShinyList is not empty if (s != "") { // add it to this string variable with two linebreaks shortListString += s + "\n\n"; } } // show the string variable (which is basically a culled, ordered, list) MessageBox.Show($"{shortListString}"); // show message asking user if they want to go through each file DialogResult result2 = MessageBox.Show($"Do you want to go through each file?", "Go through each file?", MessageBoxButtons.YesNo); // if User selects Yes if (result2 == DialogResult.Yes) { // run the loop as many times as necessary to run through newShinyList for (int j = 0; j < newShinyList.Count(); j++) { // if list entry in question is not blank if (newShinyList[j] != "") { // show message asking User if they want to open file DialogResult result3 = MessageBox.Show($"Current file is:\n\n{newShinyList[j].Remove(0, newShinyList[j].IndexOf("C:\\DATA"))}\n\nDo you want to open it?", "Want to open this file?", MessageBoxButtons.YesNo); // if User selected yes if (result3 == DialogResult.Yes) { // save file location in shortListString shortListString = newShinyList[j].Remove(0, newShinyList[j].IndexOf("C:\\DATA")).Remove(newShinyList[j].Remove(0, newShinyList[j].IndexOf("C:\\DATA")).LastIndexOf("\\"), newShinyList[j].Remove(0, newShinyList[j].IndexOf("C:\\DATA")).Length - newShinyList[j].Remove(0, newShinyList[j].IndexOf("C:\\DATA")).LastIndexOf("\\")); // open imm. above file location System.Diagnostics.Process.Start(shortListString); // clear shortListString shortListString = ""; // stop running code return; } } } } } // reset variables, clear lists etc. private void reset(object sender, EventArgs e) { lineReader = ""; convertDateSought = false; convertDateFound = false; numDateSought = 0; numDateFound = 0; shortListString = ""; scratchPad = 0; shortList.Clear(); dateFoundList.Clear(); newShinyList.Clear(); } } }