Link to home
Start Free TrialLog in
Avatar of zeetec
zeetec

asked on

VBScript - Nested loop issue

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

Avatar of RobSampson
RobSampson
Flag of Australia image

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

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

Avatar of zeetec
zeetec

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of zeetec

ASKER

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