How to compare 2 different text file lines and then process information if a match is in both file lines - Please help
-
I am attempting to build a command line tool that will create shortcuts for an application on the start menu and desktop. I have existing code that I use elsewhere to create the shortcuts. However, the program looks at two different files one file has only the site code and the other has the other information needed to created the shortcut by site for a whole region. The idea is to go through both lines until it finds the 3 letter abbreviation for the site in both files. For example one file only contains BEC (sites2.txt - the city) and the other file (R03Sites2.csv - Region) contains 50 lines with a different cities and all the pertinent information.
Sub Main()
'--------------------------------------------------------------------------------------------------------
'Declare Variables
Dim strSiteCode
Dim strVistaFQDN
Dim strPort
Dim arrStr'-------------------------------------------------------------------------------------------------------- 'Start processing Using sr As New StreamReader("c:\\dell\\R03Sites2.csv") Dim line As String ' Read and display lines from the file until the end of ' the file is reached. Do line = sr.ReadLine() Dim Sites As String = My.Computer.FileSystem.ReadAllText("c:\\dell\\Sites2.txt") If Not (line Is Nothing) Then Console.WriteLine("Start processing loop") arrStr = Split(sr.ReadLine, ",") strSiteCode = arrStr(0) strVistaFQDN = arrStr(1) strPort = arrStr(2) Console.WriteLine("Array populated... Enter if then statement") If Trim(Sites) = Trim(strSiteCode) Then Console.WriteLine(Trim(Sites) & " , " & Trim(strSiteCode)) Console.WriteLine("s=" & strVistaFQDN & " p=" & strPort) 'build shorcuts here. Code to build shortcuts is working correctly when ran 'statically but it never finds a match so it always moves to the else and writes 'Console.WriteLine("Error: No Matching Information in the Sites and R03sites files!") 'There is only 1 line is Sites and an array is created for the data in R03sites. 'My intention is to match the line in Sites to the portion of the a
-
I am attempting to build a command line tool that will create shortcuts for an application on the start menu and desktop. I have existing code that I use elsewhere to create the shortcuts. However, the program looks at two different files one file has only the site code and the other has the other information needed to created the shortcut by site for a whole region. The idea is to go through both lines until it finds the 3 letter abbreviation for the site in both files. For example one file only contains BEC (sites2.txt - the city) and the other file (R03Sites2.csv - Region) contains 50 lines with a different cities and all the pertinent information.
Sub Main()
'--------------------------------------------------------------------------------------------------------
'Declare Variables
Dim strSiteCode
Dim strVistaFQDN
Dim strPort
Dim arrStr'-------------------------------------------------------------------------------------------------------- 'Start processing Using sr As New StreamReader("c:\\dell\\R03Sites2.csv") Dim line As String ' Read and display lines from the file until the end of ' the file is reached. Do line = sr.ReadLine() Dim Sites As String = My.Computer.FileSystem.ReadAllText("c:\\dell\\Sites2.txt") If Not (line Is Nothing) Then Console.WriteLine("Start processing loop") arrStr = Split(sr.ReadLine, ",") strSiteCode = arrStr(0) strVistaFQDN = arrStr(1) strPort = arrStr(2) Console.WriteLine("Array populated... Enter if then statement") If Trim(Sites) = Trim(strSiteCode) Then Console.WriteLine(Trim(Sites) & " , " & Trim(strSiteCode)) Console.WriteLine("s=" & strVistaFQDN & " p=" & strPort) 'build shorcuts here. Code to build shortcuts is working correctly when ran 'statically but it never finds a match so it always moves to the else and writes 'Console.WriteLine("Error: No Matching Information in the Sites and R03sites files!") 'There is only 1 line is Sites and an array is created for the data in R03sites. 'My intention is to match the line in Sites to the portion of the a
You're getting the exception because you've read a line from the file which doesn't contain two commas. That's probably caused by the fact that you're skipping every other line:
line = sr.ReadLine() ' Read a line
If Not (line Is Nothing) Then
arrStr = Split(sr.ReadLine, ",") ' Read another lineYou need to change that to re-use the line you've already read. You also need to check that the array returned from
Split
contains enough elements:line = sr.ReadLine()
If line IsNot Nothing Then
arrStr = Split(line, ",")
If arrStr.Length >= 3 Then
strSiteCode = arrStr(0)
strVistaFQDN = arrStr(1)
strPort = arrStr(2)
...You should also move the code that reads the contents of
Sites2.txt
outside of the loop. As it stands, you're reading the entire file for every line inSites2.csv
, even though you're not changing it.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
You're getting the exception because you've read a line from the file which doesn't contain two commas. That's probably caused by the fact that you're skipping every other line:
line = sr.ReadLine() ' Read a line
If Not (line Is Nothing) Then
arrStr = Split(sr.ReadLine, ",") ' Read another lineYou need to change that to re-use the line you've already read. You also need to check that the array returned from
Split
contains enough elements:line = sr.ReadLine()
If line IsNot Nothing Then
arrStr = Split(line, ",")
If arrStr.Length >= 3 Then
strSiteCode = arrStr(0)
strVistaFQDN = arrStr(1)
strPort = arrStr(2)
...You should also move the code that reads the contents of
Sites2.txt
outside of the loop. As it stands, you're reading the entire file for every line inSites2.csv
, even though you're not changing it.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Thanks for the response. I made the suggested changes and am still having issues. This is what I have now:
Sub Main()
'--------------------------------------------------------------------------------------------------------
'Declare Variables
Dim strSiteCode
Dim strVistaFQDN
Dim strPort
Dim arrStr
Dim Sites As String = My.Computer.FileSystem.ReadAllText("c:\dell\Sites2.txt")
'--------------------------------------------------------------------------------------------------------
'Start processingUsing sr As New StreamReader("c:\\dell\\R03Sites3.csv") Dim line As String ' Read and display lines from the file until the end of ' the file is reached. Do line = sr.ReadLine() Console.WriteLine("line = " & line) Console.WriteLine("Start processing loop") If line IsNot Nothing Then arrStr = Split(line, ",") If arrStr.Length >= 3 Then strSiteCode = arrStr(0) strVistaFQDN = arrStr(1) strPort = arrStr(2) Console.WriteLine(Trim(Sites) & " , " & Trim(strSiteCode)) Console.WriteLine("s=" & strVistaFQDN & " p=" & strPort) Console.WriteLine("Array populated... Enter if then statement") If Trim(Sites) = Trim(strSiteCode) Then 'Console.WriteLine(Trim(Sites) & " , " & Trim(strSiteCode)) 'Console.WriteLine("s=" & strVistaFQDN & " p=" & strPort) Console.WriteLine("Successfully processed!") Exit Do Else Console.WriteLine("Error: No Matching Information in the Sitecode and R03sites files!") End If End If End If Loop Until line Is Nothing End Using End Sub
Here is the output (I cut the R03 file down to 2 entries): C:\Dell>bcmass line = BEC,test.beckley.com,19233 Start processing loop BEC,BEC , BEC s=test.beckley.com p=19233 Array populated... Enter if then statement Error: No Matching Information in the Sitecode and R03sites files! line = BIR,test.birmingham.com,19230 Start processing loop BEC,BEC ,
-
Thanks for the response. I made the suggested changes and am still having issues. This is what I have now:
Sub Main()
'--------------------------------------------------------------------------------------------------------
'Declare Variables
Dim strSiteCode
Dim strVistaFQDN
Dim strPort
Dim arrStr
Dim Sites As String = My.Computer.FileSystem.ReadAllText("c:\dell\Sites2.txt")
'--------------------------------------------------------------------------------------------------------
'Start processingUsing sr As New StreamReader("c:\\dell\\R03Sites3.csv") Dim line As String ' Read and display lines from the file until the end of ' the file is reached. Do line = sr.ReadLine() Console.WriteLine("line = " & line) Console.WriteLine("Start processing loop") If line IsNot Nothing Then arrStr = Split(line, ",") If arrStr.Length >= 3 Then strSiteCode = arrStr(0) strVistaFQDN = arrStr(1) strPort = arrStr(2) Console.WriteLine(Trim(Sites) & " , " & Trim(strSiteCode)) Console.WriteLine("s=" & strVistaFQDN & " p=" & strPort) Console.WriteLine("Array populated... Enter if then statement") If Trim(Sites) = Trim(strSiteCode) Then 'Console.WriteLine(Trim(Sites) & " , " & Trim(strSiteCode)) 'Console.WriteLine("s=" & strVistaFQDN & " p=" & strPort) Console.WriteLine("Successfully processed!") Exit Do Else Console.WriteLine("Error: No Matching Information in the Sitecode and R03sites files!") End If End If End If Loop Until line Is Nothing End Using End Sub
Here is the output (I cut the R03 file down to 2 entries): C:\Dell>bcmass line = BEC,test.beckley.com,19233 Start processing loop BEC,BEC , BEC s=test.beckley.com p=19233 Array populated... Enter if then statement Error: No Matching Information in the Sitecode and R03sites files! line = BIR,test.birmingham.com,19230 Start processing loop BEC,BEC ,
The reason you are getting the extra data in the output is because of the line:
Console.WriteLine(Trim(Sites) & " , " & Trim(strSiteCode))
You need to split the
Sites
data the same as the other lines, so you can compare BEC to BEC. Also, you only read theSites
data once, so how do you expect it to be different on subsequent passes through the loop? -
The reason you are getting the extra data in the output is because of the line:
Console.WriteLine(Trim(Sites) & " , " & Trim(strSiteCode))
You need to split the
Sites
data the same as the other lines, so you can compare BEC to BEC. Also, you only read theSites
data once, so how do you expect it to be different on subsequent passes through the loop?Thanks for the response. You're suggestion helped but the if then statement still doesn't work. Also, Sites data never changes each computer at each site has a Sites data file with the appropriate entry. It only has one line in the file and it is formatted like this: BEC,BEC. Here is the new code:
Do
line = sr.ReadLine()
Console.WriteLine("line = " & line)
Console.WriteLine("Start processing loop")
If line IsNot Nothing Then
arrStr = Split(line, ",")
siteArr = Split(Sites, ",")
Site1 = siteArr(0)
If arrStr.Length >= 3 Then
strSiteCode = arrStr(0)
strVistaFQDN = arrStr(1)
strPort = arrStr(2)
Console.WriteLine(Trim(Site1) & " , " & Trim(strSiteCode))
Console.WriteLine("s=" & strVistaFQDN & " p=" & strPort)
Console.WriteLine("Array populated... Enter if then statement")
If Trim(Sites) = Trim(strSiteCode) Then
'Console.WriteLine(Trim(Sites) & " , " & Trim(strSiteCode))
'Console.WriteLine("s=" & strVistaFQDN & " p=" & strPort)
Console.WriteLine("Successfully processed!")
Exit Do
Else
Console.WriteLine("Error: No Matching Information in the Sitecode and R03sites files!")
End If
End If
End If
Loop Until line Is NothingHere is the output: \Dell>bcmass line = BEC,test.beckley.com,19233 Start processing loop BEC , BEC s=test.beckley.com p=19233 Array populated... Enter if then statement Error: No Matching Information in the Sitecode and R03sites files! line = BIR,test.birmingham.com,19230 Start processing loop BEC , BIR s=test.birmingham.com p=19230 Array populated... Enter if then statement Error: No Matching Information in the Sitecode and R03sites files! line = Start processing loop
-
The reason you are getting the extra data in the output is because of the line:
Console.WriteLine(Trim(Sites) & " , " & Trim(strSiteCode))
You need to split the
Sites
data the same as the other lines, so you can compare BEC to BEC. Also, you only read theSites
data once, so how do you expect it to be different on subsequent passes through the loop?This line If Trim(Sites) = Trim(strSiteCode) Then is supposed to find the matching entry in both Sites and R03sites files ie. BEC , BEC and then process the shortcut creation and write to console if no match is found. Then loop through the rest of the file. I would like to learn how to make it stop when it finds the match but I can't even make it work the way I need it to yet.
-
Thanks for the response. You're suggestion helped but the if then statement still doesn't work. Also, Sites data never changes each computer at each site has a Sites data file with the appropriate entry. It only has one line in the file and it is formatted like this: BEC,BEC. Here is the new code:
Do
line = sr.ReadLine()
Console.WriteLine("line = " & line)
Console.WriteLine("Start processing loop")
If line IsNot Nothing Then
arrStr = Split(line, ",")
siteArr = Split(Sites, ",")
Site1 = siteArr(0)
If arrStr.Length >= 3 Then
strSiteCode = arrStr(0)
strVistaFQDN = arrStr(1)
strPort = arrStr(2)
Console.WriteLine(Trim(Site1) & " , " & Trim(strSiteCode))
Console.WriteLine("s=" & strVistaFQDN & " p=" & strPort)
Console.WriteLine("Array populated... Enter if then statement")
If Trim(Sites) = Trim(strSiteCode) Then
'Console.WriteLine(Trim(Sites) & " , " & Trim(strSiteCode))
'Console.WriteLine("s=" & strVistaFQDN & " p=" & strPort)
Console.WriteLine("Successfully processed!")
Exit Do
Else
Console.WriteLine("Error: No Matching Information in the Sitecode and R03sites files!")
End If
End If
End If
Loop Until line Is NothingHere is the output: \Dell>bcmass line = BEC,test.beckley.com,19233 Start processing loop BEC , BEC s=test.beckley.com p=19233 Array populated... Enter if then statement Error: No Matching Information in the Sitecode and R03sites files! line = BIR,test.birmingham.com,19230 Start processing loop BEC , BIR s=test.birmingham.com p=19230 Array populated... Enter if then statement Error: No Matching Information in the Sitecode and R03sites files! line = Start processing loop
If Trim(Sites) = Trim(strSiteCode) Then
That should be
If Trim(Site1) = Trim(strSiteCode) Then ' Site1 contains the first field of the Sites data
You need to slow down, think what you are trying to achieve, and look at exactly what each line is doing. Rather than using
Console.WriteLine
to see what is going on, you should use the debugger step by step, so you can see exactly what is happening. And in the second loop (as I explained previously)BEC
will never be equal toBIR
. -
If Trim(Sites) = Trim(strSiteCode) Then
That should be
If Trim(Site1) = Trim(strSiteCode) Then ' Site1 contains the first field of the Sites data
You need to slow down, think what you are trying to achieve, and look at exactly what each line is doing. Rather than using
Console.WriteLine
to see what is going on, you should use the debugger step by step, so you can see exactly what is happening. And in the second loop (as I explained previously)BEC
will never be equal toBIR
.That totally makes sense. I just didn't change sites to site1 in all instances. I know that BEC will never equal BIR, R03sites has numerous sites and what I wanted to do was loop through all the sites in R03sites and write an error to the console on non-matching items and process the match. I just don't know how to do what I want a better way. I am sure you could make the loop stop when it finds a match but I don't know how to do it yet. I am still new to programming and have never worked with 2 text files at once, I have mostly just used text files to read computer names or ip addresses and then do something straightforward with it or write text to a log file. I appreciate the help. I left work but will give this a try tomorrow.
-
If Trim(Sites) = Trim(strSiteCode) Then
That should be
If Trim(Site1) = Trim(strSiteCode) Then ' Site1 contains the first field of the Sites data
You need to slow down, think what you are trying to achieve, and look at exactly what each line is doing. Rather than using
Console.WriteLine
to see what is going on, you should use the debugger step by step, so you can see exactly what is happening. And in the second loop (as I explained previously)BEC
will never be equal toBIR
.Thanks for the help. It worked perfectly.
-
Thanks for the help. It worked perfectly.
-
This line If Trim(Sites) = Trim(strSiteCode) Then is supposed to find the matching entry in both Sites and R03sites files ie. BEC , BEC and then process the shortcut creation and write to console if no match is found. Then loop through the rest of the file. I would like to learn how to make it stop when it finds the match but I can't even make it work the way I need it to yet.