Jump to content

Comparing Files


falconv
 Share

Recommended Posts

I'm working on a script that needs to be able to look at the lines of two files, and if a line in the 2nd file isn't in the first, it needs to mark it as added (insert it next to it?), if it's in the first but not the second it needs to place a deleted marker. My problem is this: I can make it compare line-for-line (line one with line one, two with two, etc), but if any line was removed or added in the second file (was in the first, but not the second), from that point on the lines don't match (now line two in file 1 matches line three in file 2 because a line was added). So once it notices one file was removed, from that point on I want it to compare line two with three, three with four, etc, and then if another line is added, then it will go back to two with two, etc.

Please, any help would be appreciated. At the moment I'm trying to figure out a conditional sequence with Select and Case.

Below I've posted the code I have so far (ignore the first SELECT, it's just to give a guess at what the filename should be depending on the date), and below that two portions of the text files I need to compare with some lines added and removed, just as I receive them for processing.

Many thanks in advance!!

; ----------------------------------------------------------------------------
; Script Start
; ----------------------------------------------------------------------------

$year = StringRight( @YEAR, 2 )

Select
    Case @WDAY = 3
        $recentday = @MDAY - 4
        If $recentday <= 0 Then
            $month = @MON - 1
            If $month <= 0 Then $year = $year - 1
            $recentday = $recentday + 30
    Case @WDAY = 6
        $recentday = @MDAY - 3
        If $recentday <= 0 Then
            $month = @MON - 1
            If $month <= 0 Then $year = $year - 1
            $recentday = $recentday + 30
    Case Else
        $wrongday = 1
EndSelect

$recentfilename = FileOpenDialog( "Recent Burn List", @DesktopDir & "\Burn List Archives", "Text files (*.txt)", 3, "burninv_" & @MON & $recentday & $year )
$recentfile = FileOpen( $recentfilename, 0 )
$currentfilename = FileOpenDialog( "Current Burn List", @DesktopDir & "\Burn List Archives", "Text files (*.txt)", 3, "burninv_" & @MON & @MDAY & $year & ".txt" )
$currentfile = FileOpen( $currentfilename, 2 )
While 1
    $compareline_RAWprevious = FileReadLine( $recentfile, $n-1 )
    $compareline_RAW = FileReadLine( $recentfile, $n )
    If @error = -1 Then ExitLoop
    $compareline_RAWnext = FileReadLine( $recentfile, $n+1 )
    $withline_RAW = FileReadLine( $currenfile, $m )
    If @error = -1 Then ExitLoop
    $compareresortlast = StringLeft( $compareline_RAWrecent, 70 )
    $compareresort = StringLeft( $compareline_RAW, 70 )
    $compareresortnext = StringLeft( $compareline_RAWnext, 70 )
    $withresort = StringLeft( $withline_RAW, 70 )
    $comparepricerecent = StringRight( $compareline_RAWrecent, 4 )
    $compareprice = StringRight( $compareline_RAW, 4 )
    $comparepricenext = StringRight( $compareline_RAWnext, 4 )
    $withprice = StringRight( $withline_RAW, 4 )
    $GH = StringInStr( $compareprice, "GH" )
    $W = StringInStr( $compareprice, "-W" )
    Select
        Case $compareresort = $withresort
            Select
                Case $compareprice = $withprice
                Case Else
                    Select

            EndSelect
        Case $compareresort = $withresort
        
        Case 
        Case Else
        
    EndSelect
WEnd

4  01-22-2005 01-29-2005  PINES @ SUNRIVER, THE( ORMT    1/4    1000    GH

4  01-23-2005 01-30-2005  MAUI SCHOONER-610      HIMA    2/6    1000    GH

5  01-28-2005 02-04-2005  CYPRESS POINTE GRANDVI FLOR    1/4    1000    GH

5  01-28-2005 02-04-2005  MAYAN PALACE PUERTO PE MXGOC  2/8    1000    GH

5  01-29-2005 02-05-2005  BANFF GATE MOUTAIN RES CD      2/6    1000    GH

5  01-29-2005 02-05-2005  CONDO RIOS            CBJAM  3/6    1000    GH

5  01-29-2005 02-05-2005  RHC-STREAMSIDE AT VAIL CO      1/6    22001    GH

5  01-29-2005 02-05-2005  VILLAGE @ STEAMBOAT/CO CO      1/4    1000    GH

5  01-29-2005 02-05-2005  CELEBRITY RESORTS OF W HIOA    0/4    1000    GH

5  01-29-2005 02-05-2005  CELEBRITY RESORTS OF W HIOA    0/4    22002    GH

5  01-29-2005 02-05-2005  CELEBRITY RESORTS OF W HIOA    0/4    1000    GH

5  01-29-2005 02-05-2005  CELEBRITY RESORTS OF W HIOA    0/4    1000    GH

5  01-28-2005 02-04-2005  CYPRESS POINTE GRANDVI FLOR    1/4    1000    GH

5  01-28-2005 02-04-2005  MAYAN PALACE PUERTO PE MXGOC  2/8    1000    GH

5  01-29-2005 02-05-2005  BANFF GATE MOUTAIN RES CD      2/6    1000    GH

5  01-29-2005 02-05-2005  CONDO RIOS            CBJAM  3/6    1000    GH

5  01-29-2005 02-05-2005  RHC-STREAMSIDE AT VAIL CO      1/6    22001    GH

5  01-29-2005 02-05-2005  VILLAGE @ STEAMBOAT/CO CO      1/4    1000    GH

5  01-29-2005 02-05-2005  CELEBRITY RESORTS OF W HIOA    0/4    1000    GH

5  01-29-2005 02-05-2005  CELEBRITY RESORTS OF W HIOA    0/4    22002    GH

5  01-29-2005 02-05-2005  CELEBRITY RESORTS OF W HIOA    0/4    1000    GH

5  01-29-2005 02-05-2005  CELEBRITY RESORTS OF W HIOA    0/4    1000    GH

5  01-29-2005 02-05-2005  FIESTA AMERICANA CABO  MXCSL  0/2    22006    GH

5  01-29-2005 02-05-2005  FIESTA AMERICANA CABO  MXCSL  0/2    1000    GH

5  01-29-2005 02-05-2005  PINES @ SUNRIVER, THE( ORMT    1/4    1000    GH

5  01-30-2005 02-06-2005  MILL HARBOUR CONDOMINI CBSTC  2/4    1000    GH

If you have questions, please ask. Thanks again! This has been puzzling me! Edited by falconv
Link to comment
Share on other sites

you have to sort the records by some kind of key ( date and time ? ) that

you could test for = , > and < if the files are small you could load them into

arrays but you CAN NOT just test for equality.

you wont be able to know if a record was added to file 2 or deleted from it unless

both of the files have a set sequence that you could compare :

file file

file 1 file 2

===== =====

rec key rec key

1 11 = 1 11 : same record number file is the same

2 12 < 2 13 : file 2 has no record 12 so mark 12 for delete

3 13 = 2 13 : read the next record on file 1

4 14 = 3 14 : same record

5 18 > 4 15 : file 1 has no record 15 so mark for add read file 2

5 18 > 5 16 : file 1 has no record 16 so mark for add read file 2

5 18 = 6 18 : same etc...

Link to comment
Share on other sites

Thanks for the reply! I was beginning to think nobody could help :lmao:

Here's the sequence of information the lines go by:

first number is the week (5th week of the year)

5 01-28-2005 02-04-2005 CYPRESS POINTE GRANDVI FLOR 1/4 1000 GH

second set is the beginning date

5 01-28-2005 02-04-2005 CYPRESS POINTE GRANDVI FLOR 1/4 1000 GH

third is the ending date

5 01-28-2005 02-04-2005 CYPRESS POINTE GRANDVI FLOR 1/4 1000 GH

the words in the middle are the name of the vacation resort

5 01-28-2005 02-04-2005 CYPRESS POINTE GRANDVI FLOR 1/4 1000 GH

after that is the # of rooms and people they can accomodate

5 01-28-2005 02-04-2005 CYPRESS POINTE GRANDVI FLOR 1/4 1000 GH

After that is the internal code of what type of resort it is

5 01-28-2005 02-04-2005 CYPRESS POINTE GRANDVI FLOR 1/4 1000 GH

and last is the price (by code)

5 01-28-2005 02-04-2005 CYPRESS POINTE GRANDVI FLOR 1/4 1000 GH

I hope that helps you decipher what I need to have compared.

...hmm. I just had an idea. It's not as efficient, as it'd have to loop through the entire file many times, but perhaps I can just have it look at line 1 of file 1 and compare it to each line in file 2. Then once it's done, if it was found it will go on to line 2, otherwise it will mark it as deleted and append <--deleted to it in the output file.

I'll try it tomorrow.

If you have any ideas, or suggestions, they'd be appreciated!

Thanks again!

Link to comment
Share on other sites

*bump* again.

This is a tricky problem, if anyone has any ideas, please let me know, I'll try anything. If you need files to practice with, I can send some. I've been mulling over this, and studying concepts, and trying ideas, but so far I've had no luck. I know there has to be a way!! help is appreciated!

Link to comment
Share on other sites

*bump* again.

This is a tricky problem, if anyone has any ideas, please let me know, I'll try anything.  If you need files to practice with, I can send some.  I've been mulling over this, and studying concepts, and trying ideas, but so far I've had no luck.  I know there has to be a way!!  help is appreciated!

<{POST_SNAPBACK}>

C:\Documents and Settings\Administrator>comp /?
Compares the contents of two files or sets of files.

COMP [data1] [data2] [/D] [/A] [/L] [/N=number] [/C]

  data1  Specifies location and name(s) of first file(s) to compare.
  data2  Specifies location and name(s) of second files to compare.
  /D        Displays differences in decimal format.
  /A        Displays differences in ASCII characters.
  /L        Displays line numbers for differences.
  /N=number Compares only the first specified number of lines in each file.
  /C        Disregards case of ASCII letters when comparing files.

To compare sets of files, use wildcards in data1 and data2 parameters.

You could try piping the results of COMP on the command line to a file and work with the generated file.

Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache

Link to comment
Share on other sites

You could try piping the results of COMP on the command line to a file and work with the generated file.

Thanks! I was afraid it would compare line-for-line like my script would do, and that's not quite what I want, but I will never know unless I try it :lmao:

Thanks again, I'll post how it goes.

Link to comment
Share on other sites

loop through the entire file many times, but perhaps I can just have it look at line 1 of file 1 and compare it to each line in file 2.  Then once it's done, if it was found it will go on to line 2

I believe this solution combined with a previous suggestion is the right way.

A previous suggestion implied sorting the files first.

Get a search item from sorted file 1.

Get a search item from sorted file 2.

Compare.

If significant do something

If F2Search item PRECEDES the expected, then

Get a search item from file 2, else

Get a search item from file 1.

; Don't lose the item from FILE 2!!

Compare.

This is skeleton.

If files are long, this is ok approach I think.

I must soon make a similar solution. Let me know if you get anywhere, and I will post my solution when it gets done too.

J

PS, My problem is to find and extract any url in file, IOW to repeatedly search a file for a string. Any suggestions? As a newbie, I am confounded with reading lets say 10 characters, and then searching for a url in those characters, but what if I have only read out a part of the url from the file? The file is always html in my case.

Sorry, I just got idea. I could read in one char at a time til I get "=", saving previous chars in a buffer. If previous chars are "href" then I found url.

I Apologize for hijacking your thread with this.

If I am too verbose, just say so. You don't need to run on and on.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...