Solved

VBScript - Nested loop issue

Posted on 2011-02-10
5
627 Views
Last Modified: 2012-05-11
I have written a script to read file A and for each line return the characters before the first comma and set this to string1 and then search file B for lines that start with string1 and then write any lines found to File3 before moving on to read the second line of file A to start over but I can't get it to work correctly as it seems to only work for the first line read in step one and I don't know why.

Any help much appreciated.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objF1 = objFSO.OpenTextFile(strFile1,1,0)
Set objF2 = objFSO.OpenTextFile(strFile2,1,0)
Set objF3 = objFSO.CreateTextFile(strFile3)
objF3.close
Set objF3 = objFSO.OpenTextFile(strFile3,2)

Do Until objF1.AtEndOfStream
   strLine1 = objF1.ReadLine
   Length = inStr(strLine1, ",")
   strName = Left(strLine1,Length)
   Do Until objF2.AtEndOfStream
      strLine2 = objF2.ReadLine
      If inStr(strLine2, strName) = 1 Then
         objF3.Writeline strLine2
      End If
   Loop

Loop

Open in new window

0
Comment
Question by:zeetec
  • 3
  • 2
5 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 34869331
Hi, you need to close and re-open file 2 so that the cursor returns to the start of the file.

Regards,

Rob.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objF1 = objFSO.OpenTextFile(strFile1,1,0)
Set objF3 = objFSO.CreateTextFile(strFile3)
objF3.close
Set objF3 = objFSO.OpenTextFile(strFile3,2)

Do Until objF1.AtEndOfStream
   strLine1 = objF1.ReadLine
   Length = inStr(strLine1, ",")
   strName = Left(strLine1,Length)
   Set objF2 = objFSO.OpenTextFile(strFile2,1,0)
   Do Until objF2.AtEndOfStream
      strLine2 = objF2.ReadLine
      If inStr(strLine2, strName) = 1 Then
         objF3.Writeline strLine2
      End If
   Loop
   objF2.Close
Loop

Open in new window

0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34869731
You also don't need to close and re-open file3 for writing.  When you use CreateTextFile, you automatically create a new file for writing to.

Rob.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objF1 = objFSO.OpenTextFile(strFile1,1,False)
Set objF3 = objFSO.CreateTextFile(strFile3,True)

Do Until objF1.AtEndOfStream
   strLine1 = objF1.ReadLine
   Length = inStr(strLine1, ",")
   strName = Left(strLine1,Length)
   Set objF2 = objFSO.OpenTextFile(strFile2,1,0)
   Do Until objF2.AtEndOfStream
      strLine2 = objF2.ReadLine
      If inStr(strLine2, strName) = 1 Then
         objF3.Writeline strLine2
      End If
   Loop
   objF2.Close
Loop

Open in new window

0
 

Author Comment

by:zeetec
ID: 34869745
I had tried opening and closing the text file inside the first loop previously but for some reason that code results in every line from File2 being written to File3 and I can't figure out why. Thanks for the tip about the createtextfile too.
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 34869770
It shouldn't do that...perhaps try this:

Rob.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objF1 = objFSO.OpenTextFile(strFile1,1,False)
Set objF3 = objFSO.CreateTextFile(strFile3,True)

Do Until objF1.AtEndOfStream
   strLine1 = objF1.ReadLine
   Length = inStr(strLine1, ",")
   strName = Left(strLine1,Length)
   Set objF2 = objFSO.OpenTextFile(strFile2,1,0)
   Do Until objF2.AtEndOfStream
      strLine2 = objF2.ReadLine
      If Left(strLine2, Len(strName)) = strName Then
         objF3.Writeline strLine2
      End If
   Loop
   objF2.Close
Loop

Open in new window

0
 

Author Comment

by:zeetec
ID: 34869888
Ok. I worked out what the issue was. There was (and will always be) a blank line at the end of file1 which was causing it to match every line from file2 so I just needed to account for that with an if statement. Thanks for your help. Points will be duely be awarded to you.

Zeetec
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objF1 = objFSO.OpenTextFile(strFile1,1,False)
Set objF3 = objFSO.CreateTextFile(strFile3,True)

Do Until objF1.AtEndOfStream
   strLine1 = objF1.ReadLine
   Length = inStr(strLine1, ",")
   If Length = 0 Then
      Exit Do
   End If
   strName = Left(strLine1,Length)
   Set objF2 = objFSO.OpenTextFile(strFile2,1,0)
   Do Until objF2.AtEndOfStream
      strLine2 = objF2.ReadLine
      If Left(strLine2, Len(strName)) = strName Then
         objF3.Writeline strLine2
      End If
   Loop
   objF2.Close
Loop

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Unlike scripting languages such as C# where a semi-colon is used to indicate the end of a command, Microsoft's VBScript language relies on line breaks to determine when a command begins and ends. As you can imagine, this quickly results in messy cod…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question