aspose file tools*
The moose likes Other Languages and the fly likes VBScript to compare files... not really working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Languages » Other Languages
Bookmark "VBScript to compare files... not really working" Watch "VBScript to compare files... not really working" New topic
Author

VBScript to compare files... not really working

Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1613
    
  10

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.



When you do things right, people won't be sure you've done anything at all.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
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
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1613
    
  10

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
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
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.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18114
    
    8

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
    Saloon Keeper

    Joined: Sep 14, 2009
    Posts: 1613
        
      10

    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
    Bartender

    Joined: Oct 14, 2005
    Posts: 18114
        
        8

    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
    Saloon Keeper

    Joined: Sep 14, 2009
    Posts: 1613
        
      10

    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
    Saloon Keeper

    Joined: Sep 14, 2009
    Posts: 1613
        
      10

    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
    Bartender

    Joined: Oct 14, 2005
    Posts: 18114
        
        8

    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
    Saloon Keeper

    Joined: Sep 14, 2009
    Posts: 1613
        
      10

    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
    Bartender

    Joined: Oct 14, 2005
    Posts: 18114
        
        8

    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
    Saloon Keeper

    Joined: Sep 14, 2009
    Posts: 1613
        
      10

    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.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: VBScript to compare files... not really working
     
    Similar Threads
    IO Doubt
    Want to write a combined text file, but nothing happens..
    Read this for quick help please!
    why doesn't this work?
    How to read from DOS pipe?