• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

VBScript to compare files... not really working

 
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So here's the deal... I need this VBScript to create a file if the files aren't the same, and NO file if the files are identical.

I have 2 identical files, and the script thinks that the last line is always different. I have not tried different files yet, but if there's something wrong with that logic, please feel free to tell me.

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't know any VB, I am afraid.

Write out with pencil and paper what will happen with your loops. I suspect your nested loops are reading the first line of file 1, then the whole of file 2, then the second line of file 1, etc. Verify your loop by putting some print statements into it.
 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Don't know any VB, I am afraid.



Me either! I know a LITTLE basic from back in the day, but not VB.

Write out with pencil and paper what will happen with your loops. I suspect your nested loops are reading the first line of file 1, then the whole of file 2, then the second line of file 1, etc. Verify your loop by putting some print statements into it.



Yea.... that's what it's supposed to be doing.... and on paper that's what it looks like. I want both files to not care about the order of the comparison file. if the sister line is there, there should be no output.

It seems my if statements aren't working... I'll have to figure out if my syntax is correct.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
VB is very different from real BASIC.

So what you are doing is checking whether the two files contain identical lines but in different orders? Put in some debugging print statements. I think you are getting something written whenever you encounter two different lines. Try it with two identical one-line files and see what happens.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To me that logic looks like:

  • Read the first line from SHOULDBE
  • Read all of the lines from IS and compare them to that line; write any that match to the output.
  • Read the remaining lines from SHOULDBE and do nothing with them.

  •  
    Janeice DelVecchio
    Bartender
    Posts: 1849
    15
    Eclipse IDE Spring VI Editor Java Linux Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Logic should be this:

    Read first line from file 1.
    compare to all lines in file 2
    if it's there, skip.
    if not, write the line to output
    Continue with the remaining lines of file 1.
    write output to the required file if there's anything in it,
    write nothing if output is empty

    Do the same with file 2 comparing to file 1.
     
    Paul Clapham
    Marshal
    Posts: 28193
    95
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Janeice DelVecchio wrote:Logic should be this:

    Read first line from file 1.
    compare to all lines in file 2
    if it's there, skip.
    if not, write the line to output



    I don't see any "skip" in the code, it does compare to ALL lines in file 2 and not just all lines up to the first match.

    Continue with the remaining lines of file 1.
    write output to the required file if there's anything in it,
    write nothing if output is empty.


    And is the same thing supposed to be done for the remaining lines? Compare them to all lines in file 2? Because if that's what you want then the inner loop should be
  • Open file 2
  • Compare the line from file 1 to all lines in file 2
  • Close file 2


  •  
    Janeice DelVecchio
    Bartender
    Posts: 1849
    15
    Eclipse IDE Spring VI Editor Java Linux Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Ok so here's the situation.

    If the 2 files are identical, I want nothing to happen. No file.

    Maybe there's a better way.

    The files have small amounts of data in them that designate which roles a server has.... like

    ADC
    SQL
    EXCHANGE

    If "SQL" isn't in the second file, I need a file created. Likewise, if there's something in the second file that shouldn't be there, I need a different file created.

    I figured if the script read line by line and compared line by line with the second file, then reversed.... only keeping the mismatches, that would be my answer.

    What do you think?
     
    Janeice DelVecchio
    Bartender
    Posts: 1849
    15
    Eclipse IDE Spring VI Editor Java Linux Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Here's what I've come up with:



    I think the logic here is better, but I'm still not getting the right output.
     
    Paul Clapham
    Marshal
    Posts: 28193
    95
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You're still only reading isFile once (for the first record of shouldBe) and not once for every record of shouldBe. All of the code in the inner do-while is only executed for the first record of shouldBe. But I've said this before, am I wrong?
     
    Janeice DelVecchio
    Bartender
    Posts: 1849
    15
    Eclipse IDE Spring VI Editor Java Linux Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Paul Clapham wrote:You're still only reading isFile once (for the first record of shouldBe) and not once for every record of shouldBe. All of the code in the inner do-while is only executed for the first record of shouldBe. But I've said this before, am I wrong?


    Paul, thanks for your help.....

    I guess I'm not understanding.... are you saying the outer loop only happens once? Why wouldn't it loop for each line? Or maybe you're saying that the inner loop only happens once.... doesn't it start over on the next outer loop?

    Maybe I don't understang the nuts and bolts of this enough. I would like to iterate through each line in the first file (shouldBeFile)... each iteration has a loop to iterate through wach line in the second file (isFile).

    For example:

    File 1 (imagine line breaks): ADC SQL CITRIX
    File 2 (imagine line breaks): SQL CITRIX BES

    Looping as follows:
    First loop:
    1. line1 = ADC, line2 = SQL. no match.
    2. line1 = ADC, line2 = CITRIX. no match.
    3. line1 = ADC, line2 = BES. no match.
    Add ADC and a line break to the output
    Second loop:
    1. line1 = SQL, line2 = SQL. Match.
    Nothing gets added to the output
    Third loop:
    1. line1 = CITRIX, line2 = SQL. no match
    2. line1 = CITRIX, line2 = CITRIX. Match
    Nothing gets added to the output.

    I guess I don't even really care about performance.... if it has to compare all the lines of the second file that's ok. I just don't want output if there's no change.
     
    Paul Clapham
    Marshal
    Posts: 28193
    95
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Janeice DelVecchio wrote:Or maybe you're saying that the inner loop only happens once.... doesn't it start over on the next outer loop?



    I thought I said that. Didn't I say that?

    Of course the inner loop is executed every time through the outer loop; but after the first time through the outer loop, the file you're reading in the inner loop is at end-of-file, so executing the inner loop does nothing.

    But come on, stick some debugging code inside the inner loop and prove me wrong.
     
    Janeice DelVecchio
    Bartender
    Posts: 1849
    15
    Eclipse IDE Spring VI Editor Java Linux Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Paul Clapham wrote: but after the first time through the outer loop, the file you're reading in the inner loop is at end-of-file, so executing the inner loop does nothing.





    I guess I didn't think of it that way.

    Now it makes more sense why you said to keep opening and closing the file. I didn't put the two together.

    Sorry to be such a stupidhead. Thanks for your patience.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic