

InterviewSolution
Saved Bookmarks
1. |
Solve : How to compare 2 text files in a script?? |
Answer» <html><body><p>Hello<br/><br/>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 <a href="https://interviewquestions.tuteehub.com/tag/relevant-613818" style="font-weight:bold;" target="_blank" title="Click to know more about RELEVANT">RELEVANT</a>. 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:<br/><br/>FC c:\Download\TodayFile.txt c:\Yesterday\File.txt \L > <a href="https://interviewquestions.tuteehub.com/tag/diff-431920" style="font-weight:bold;" target="_blank" title="Click to know more about DIFF">DIFF</a><br/>Copy diff compareIt > Nul<br/>If not exist compareIt GOTO SAME_FILE_TODAY<br/><br/>I also used command COMP. <br/><br/>I'd love to find a solution that is within DOS. <br/><br/>Any help is welcome.<br/><br/>Thanks<br/><br/>p.s. Please reply to this post or email me directly.<br/><br/><br/><br/><br/><br/> Code: <a>[Select]</a>FC c:\Download\TodayFile.txt c:\Yesterday\File.txt /L | FIND "FC: no dif" > nul <br/> IF ERRORLEVEL 1 goto different<br/>echo Files are the same.<br/>goto end<br/>:different<br/>echo Files are different.<br/>:end<br/>basically, we cheat and use FIND to search through the results of FC, since to my understanding FC doesn't return an errorlevel.Yep, <a href="https://interviewquestions.tuteehub.com/tag/noticed-2877256" style="font-weight:bold;" target="_blank" title="Click to know more about NOTICED">NOTICED</a> that too. I will give it a <a href="https://interviewquestions.tuteehub.com/tag/try-1428546" style="font-weight:bold;" target="_blank" title="Click to know more about TRY">TRY</a>. I am <a href="https://interviewquestions.tuteehub.com/tag/sure-656539" style="font-weight:bold;" target="_blank" title="Click to know more about SURE">SURE</a> it will work.<br/><br/>Thank you Quote from: DosStud on November 03, 2010, 05:58:41 PM</p><blockquote>Yep, noticed that too. I will give it a try. I am sure it will work.<br/><br/>By taking a ride on your suggestion I decided to use it this way...<br/><br/>FC c:\Download\TodayFile.txt c:\Yesterday\File.txt /L | FIND "FC: no dif" > diff<br/>COPY Diff Nul > Nul<br/>IF ERRORLEVEL 1 goto ....<br/><br/>It would be straightforward if FC returned errorlevel<br/><br/>Thank you very much<br/></blockquote> Quote from: BC_Programmer on November 03, 2010, 05:46:46 PM<blockquote>to my understanding FC doesn't return an errorlevel.<br/></blockquote> <br/>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.<br/><br/> Code: <a>[Select]</a>echo off<br/>echo hello > test1.txt<br/>copy test1.txt test2.txt>nul<br/>echo goodbye > test3.txt<br/>fc test1.txt test2.txt>nul<br/>echo Compare 2 identical files errorlevel=%errorlevel%<br/>fc test1.txt test3.txt>nul<br/>echo Compare 2 different files errorlevel=%errorlevel%<br/>fc test1.txt test4.txt 2>1>nul<br/>echo compare file with nonexistent file errorlevel=%errorlevel%<br/>fc test5.txt test4.txt 2>1>nul<br/>echo compare two nonexistent files errorlevel=%errorlevel%<br/><br/> Code: <a>[Select]</a>Compare 2 identical files errorlevel=0<br/>Compare 2 different files errorlevel=1<br/>compare file with nonexistent file errorlevel=2<br/>compare two nonexistent files errorlevel=2<br/>Identical output on Windows XP Professional 32 bit SP3 and Windows 7 64 bit Professional. Am I being dumb here and missing something?<br/><br/><br/><br/>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.</body></html> | |