Reputation: 123
I am trying to find a way to compare some text in 2 files and if a match is found then run a process.
Here are examples of the files;
'File A' = Automated list of text with this format;
example1
ex2
289 Example
fht_nkka
'File B' = File names from a directory search;
example1
test2
test4785
Using my 2 example files, I want to search them both and find matches.
So 'File A' above contains 'example1' and 'example1' is in 'File B'. What I want to be able to do is create 'string[] match based on all matches. Is there an easy way of doing this?
NOTE: these files do not always have the same line data or amount of lines in.
Upvotes: 0
Views: 2652
Reputation: 123
Managed to sort this out, here is what I have done;
var fileAcontents = File.ReadAllLines(fileA);
var fileBcontents = File.ReadAllLines(fileB);
HashSet<string> hashSet = new HashSet<string>(fileAcontents);
foreach (string i in fileBList)
{
if (hashSet.Contains(i))
{
// <- DO SOMETHING :)
}
}
Upvotes: 1
Reputation: 10862
//Keep in a list of strings with FileA contents
List<string> linesOfFileA = new List<string>();
string line ;
using (StreamReader sr = new StreamReader(pathToFileA))
{
//read each line of fileA
line = sr.ReadLine();
while(line != null)
{
linesOfFileA.Add(line) ;
line = sr.ReadLine();
}
}
//Now read the contents of FileB
string fileWithoutExtension ;
int posOfExtension ;
using (StreamReader srB = new StreamReader(pathToFileB))
{
//read each line of fileB
line = sr.ReadLine();
while(line != null)
{
posOfExtension = line.LastIndexOf(".");
if(posOfExtension < 0)
{
fileWithoutExtension = line ;
}
else
{
fileWithoutExtension = line.Substring(0,posOfExtension) ;
}
//Check to see if the FileA contains file but without Extension
if(linesOfFileA.Contains(fileWithoutExtension))
{
//Store into another list / or execute here
}
line = sr.ReadLine();
}
}
In the first part of the code you skip the number of lines that you need, but because of the current shown format they will not affect your comparison
Upvotes: 0
Reputation: 1638
Fill a dictionary object with File A contents then loop through File B contents querying File A dictionary object. Reason for dictionary object is its speed if you have a large array of data.
Dictionary<int, string> FileA = new Dictionary<int, string>();
string sFileAList = dataFileA;
Loop through File A contents and add to Dict where i is a counter.
int count = 0;
foreach (string s in sFileAList.split('\n')) {
count++;
if (count > 3) FileA.Add(i, s);
}
Then compare while looping through File B contents.
foreach (string s in dataFileB.split('\n')) {
if (FileA.ContainsValue(s)) {
// Run exe
}
}
Upvotes: -1
Reputation: 35460
System.IO.File.ReadAllLines()
on each of the two files to create two string arrays.for
loop on your second array with fixed increment to read the required filename.Array.BinarySearch()
to quickly locate whether that required filename exists in the list of files (the other array that is).Here is a rough sketch of the code:
string[] AllRecs = System.IO.File.ReadAllLines(FIRST_FILE_PATH);
string[] AllFileNames = System.IO.File.ReadAllLines(SECOND_FILE_PATH);
Array.Sort(AllFileNames);
for (int i = 3; i < AllRecs.Length; i += 8)
{
if (Array.BinarySearch(AllFileNames, AllRecs(i) + ".exe") >= 0)
System.Diagnostics.Process.Start(AllRecs(i) + ".exe");
}
Upvotes: 1