Solved

VBScript - Nested loop issue

Posted on 2011-02-10
5
611 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now