1.

Solve : How to compare 2 text files in a script??

Answer»

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.





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.Yep, NOTICED that too. I will give it a TRY. I am SURE it will work.

Thank you Quote from: DosStud on November 03, 2010, 05:58:41 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
Quote from: BC_Programmer on November 03, 2010, 05:46:46 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?



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.


Discussion

No Comment Found