Solved

VBScript - Nested loop issue

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
powershell script 9 63
Creating An Intelligent Dropdown, Part Deux! 9 32
VBA Vbscript  Issue 9 25
A Table within a User Form 24 42
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

839 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