Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: How to compare 2 text files in a script?  (Read 10780 times)

0 Members and 1 Guest are viewing this topic.

DosStud

    Topic Starter


    Starter

    • Experience: Experienced
    • OS: Windows 2003
    How to compare 2 text files in a script?
    « on: November 03, 2010, 05:37:57 PM »
    Hello

    I have to write a script that downloads a small text file from our mainframe daily. If the file has the same contents as in the file downloaded the day before then the script exits but if the files have different contents then I have to append the contents to another file  among other things that are not relevant. The only challenge I have is the comparison of files as you may suspect. I have tried a couple of things but without success.  I tried this:

    FC c:\Download\TodayFile.txt c:\Yesterday\File.txt \L > diff
    Copy diff compareIt > Nul
    If not exist compareIt GOTO SAME_FILE_TODAY

    I also used command COMP.

    I'd love to find a solution that is within DOS.

    Any help is welcome.

    Thanks

    p.s. Please reply to this post or email me directly.






    BC_Programmer


      Mastermind
    • Typing is no substitute for thinking.
    • Thanked: 1140
      • Yes
      • Yes
      • BC-Programming.com
    • Certifications: List
    • Computer: Specs
    • Experience: Beginner
    • OS: Windows 11
    Re: How to compare 2 text files in a script?
    « Reply #1 on: November 03, 2010, 05:46:46 PM »
    Code: [Select]
    FC c:\Download\TodayFile.txt c:\Yesterday\File.txt /L | FIND "FC: no dif" > nul
       IF ERRORLEVEL 1 goto different
    echo Files are the same.
    goto end
    :different
    echo Files are different.
    :end
    basically, we cheat and use FIND to search through the results of FC, since to my understanding FC doesn't return an errorlevel.
    I was trying to dereference Null Pointers before it was cool.

    DosStud

      Topic Starter


      Starter

      • Experience: Experienced
      • OS: Windows 2003
      Re: How to compare 2 text files in a script?
      « Reply #2 on: November 03, 2010, 05:58:41 PM »
      Yep, noticed that too. I will give it a try. I am sure it will work.

      Thank you

      DosStud

        Topic Starter


        Starter

        • Experience: Experienced
        • OS: Windows 2003
        Re: How to compare 2 text files in a script?
        « Reply #3 on: November 03, 2010, 07:05:37 PM »
        Yep, noticed that too. I will give it a try. I am sure it will work.

        By taking a ride on your suggestion I decided to use it this way...

        FC c:\Download\TodayFile.txt c:\Yesterday\File.txt /L | FIND "FC: no dif" > diff
        COPY Diff Nul > Nul
        IF ERRORLEVEL 1 goto ....

        It would be straightforward if FC returned errorlevel

        Thank you very much

        Salmon Trout

        • Guest
        Re: How to compare 2 text files in a script?
        « Reply #4 on: November 04, 2010, 04:57:05 PM »
        to my understanding FC doesn't return an errorlevel.

        That's odd because I thought it returned 0 if the files are the same, 1 if they are different, and 2 if one or both files do not exist.

        Code: [Select]
        @echo off
        echo hello > test1.txt
        copy test1.txt test2.txt>nul
        echo goodbye > test3.txt
        fc test1.txt test2.txt>nul
        echo Compare 2 identical files          errorlevel=%errorlevel%
        fc test1.txt test3.txt>nul
        echo Compare 2 different files          errorlevel=%errorlevel%
        fc test1.txt test4.txt 2>1>nul
        echo compare file with nonexistent file errorlevel=%errorlevel%
        fc test5.txt test4.txt 2>1>nul
        echo compare two nonexistent files      errorlevel=%errorlevel%

        Code: [Select]
        Compare 2 identical files          errorlevel=0
        Compare 2 different files          errorlevel=1
        compare file with nonexistent file errorlevel=2
        compare two nonexistent files      errorlevel=2

        Identical output on Windows XP Professional 32 bit SP3 and Windows 7 64 bit Professional. Am I being dumb here and missing something?




        BC_Programmer


          Mastermind
        • Typing is no substitute for thinking.
        • Thanked: 1140
          • Yes
          • Yes
          • BC-Programming.com
        • Certifications: List
        • Computer: Specs
        • Experience: Beginner
        • OS: Windows 11
        Re: How to compare 2 text files in a script?
        « Reply #5 on: November 04, 2010, 05:31:02 PM »
        cool, that should make the script even shorter then; just a quick errorlevel check in the original script presented by the OP would have done the trick. For some reason I didn't even check if fc actually returned errorlevels. But you know what they say, assuming makes an *censored* out of u and some guy named ming, guess that's what I did.
        I was trying to dereference Null Pointers before it was cool.