Want to trap Return Code if Difference Exits

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sippy
    Visitor
    • Oct 2009
    • 3

    Want to trap Return Code if Difference Exits

    I am trying to embed BeyondComapre in Continuous Integration Process
    I want to detect if a difference exists between two files then react to
    that situation

    I want to know if files are same or different - then I can later look at
    output report if there are differences

    In my batfile errorlevel is always 0 - even with difference

    Thanks


    batfile
    =====
    "C:\Program Files\Beyond Compare 3\bcompare.exe" /silent @GenerateReport.txt t1.txt t2.txt Report.txt

    if %errorlevel%==0 goto exit

    echo "error"

    :exit


    GenerateReport.txt
    ===============
    file-report layout:side-by-side &
    options:display-mismatches &
    output-to:%3 %1 %2
    pause
  • Aaron
    Team Scooter
    • Oct 2007
    • 15997

    #2
    If you are simply comparing two files, you can use the /qc /quickcompare commandline in BC3 to set the error level. Detailed documentation is available in the Help file under Command Line reference.

    Let us know if you have any questions.
    Aaron P Scooter Software

    Comment

    • sippy
      Visitor
      • Oct 2009
      • 3

      #3
      Always get Return code = 0

      Hi

      Thanks but I am always getting a Return Code = 0 even when files are different ??


      "C:\Program Files\Beyond Compare 3\bcompare.exe" /qc=binary /silent @GenerateReport.txt t1.txt t2.txt Report.txt

      if %errorlevel% == 0 goto exit

      echo "error"

      :exit

      pause

      Comment

      • Aaron
        Team Scooter
        • Oct 2007
        • 15997

        #4
        Hello,

        /QC does not use script, but accepts files directly. BCompare.exe /qc=binary t1.txt t2.txt
        This will return the error codes documented at the bottom of the Help file section Command Line Reference.

        This assumes you only want to know if the file is different or not.

        If you need the report, then your initial text was correct, but you need to be using the latest version of BC3 (3.1.7). Also, Script will only set ErrorLevel on fatal scripting errors. Do not use the silent command while troubleshooting this.

        If the error is a fatal scripting error that stops the script (such as an invalid base folder), then it should set the Error Level.

        If the error is per file (such as No Permission for each file), then this does not set the Error Level. This can be recorded in the log. Use the log command for the first line of your script to define where a log should be saved.
        Aaron P Scooter Software

        Comment

        • jojo
          New User
          • Feb 2011
          • 1

          #5
          Well, since you are trying to see whether the file is different or not, I would advise you to shift to the latest BC 3 versions. Anyway, the error shown in each case can be recorded in logs and you can also see to it that you use the log command to save the log to the suitable location!

          Comment

          • TDN
            Journeyman
            • Jan 2013
            • 15

            #6
            What about comparing 2 folders? I want to detect if there is a difference between two folders.

            Comment

            • Aaron
              Team Scooter
              • Oct 2007
              • 15997

              #7
              Hello,

              /QC does not accept folders. You would instead need to use BC Scripting to generate a report, then parse that report for the values you are looking for. For example, you could use a binary scan and generate an XML report using a bcscript.txt similar to:

              criteria binary
              load "c:\folder1" "c:\folder2"
              expand all
              folder-report layout:xml output-to:"c:\bcreport.xml"


              then call this script from the command line using:
              bcompare.exe "@c:\bcscript.txt"

              Scripting documentation can be found in the Help file, Using Beyond Compare, Scripting chapter and the main Scripting Reference section.
              Aaron P Scooter Software

              Comment

              • stoy
                New User
                • Mar 2016
                • 1

                #8
                Use ENABLEDELAYEDEXPANSION

                I had the same problem as the OP and felt that his question was not answered. The problem is related to windows batch processing, not BC. Here is my batch file:
                Code:
                @ECHO ON
                for %%f in (*.pdf) do (
                    call "C:\Program Files (x86)\Beyond Compare 4\bcomp.com" %%f baseline/%%f /qc
                 	echo %errorlevel%
                )
                And the results are:
                Code:
                c:\temp>testscript1.bat
                
                c:\temp>for %f in (*.pdf) do (
                "C:\Program Files (x86)\Beyond Compare 4\bcomp.com" %f baseline/%f /qc
                 echo 1
                )
                
                c:\temp>(
                "C:\Program Files (x86)\Beyond Compare 4\bcomp.com" DIFF.pdf baseline/DIFF.pdf /qc
                 echo 1
                )
                1
                
                c:\temp>(
                "C:\Program Files (x86)\Beyond Compare 4\bcomp.com" SAME.pdf baseline/SAME.pdf /qc
                 echo 1
                )
                1
                
                c:\temp>"C:\Program Files (x86)\Beyond Compare 4\bcomp.com" DIFF.pdf baseline/DIFF.pdf /qc
                
                c:\temp>echo %errorlevel%
                13
                As you can see, when run inside the batch file, the command returns the same result even though the files are different. Yet the command is valid and formatted correctly since I can run it on the command line and it yields the correct result. The problem is how windows handles variables in batch files and can be fixed using ENABLEDELAYEDEXPANSION. Here is my new batch file and the resulting correct output. Note the use of "!" for errorlevel.
                Code:
                @ECHO ON
                SETLOCAL ENABLEDELAYEDEXPANSION
                for %%f in (*.pdf) do (
                    set file=%%~nxf
                    "C:\Program Files (x86)\Beyond Compare 4\bcomp.com" %%f baseline/%%f /qc
                 	echo !errorlevel!
                )
                Code:
                c:\temp>SETLOCAL ENABLEDELAYEDEXPANSION
                
                c:\temp>for %f in (*.pdf) do (
                set file=%~nxf
                 "C:\Program Files (x86)\Beyond Compare 4\bcomp.com" %f baseline/%f /qc
                 echo !errorlevel!
                )
                
                c:\temp>(
                set file=DIFF.pdf
                 "C:\Program Files (x86)\Beyond Compare 4\bcomp.com" DIFF.pdf baseline/DIFF.pdf /qc
                 echo !errorlevel!
                )
                13
                
                c:\temp>(
                set file=SAME.pdf
                 "C:\Program Files (x86)\Beyond Compare 4\bcomp.com" SAME.pdf baseline/SAME.pdf /qc
                 echo !errorlevel!
                )
                1
                c:\temp>
                Hopefully this will save someone time and not having to spend hours trying to figure it out (like I did).

                Comment

                • yordanz
                  Visitor
                  • Aug 2016
                  • 3

                  #9
                  I'm not sure this question has been answered. The issue here is how to propagate the script exit code up. I call bc with the script from C# so I can't really do (nicely) the suggestions made by @stoy. Is there a better way of doing things in bc4?

                  -Y-

                  Comment

                  • Aaron
                    Team Scooter
                    • Oct 2007
                    • 15997

                    #10
                    What script call are you making and what exit code are you trying to capture? The BC4 command line should be returning an %ErrorLevel%, but our documented codes (Help file -> Command Line Reference) do not return all codes for all calls. Some codes only return if using /qc, while others when generally launched, or script. The main issue presented in this thread was the confusion of which codes were supported in which scenarios.
                    Aaron P Scooter Software

                    Comment

                    • yordanz
                      Visitor
                      • Aug 2016
                      • 3

                      #11
                      I have a script which compares two files and writes out an html report. If it succeeds, it returns 0, regardless of whether the files were the same or different. This is called from C# using ProcessStartInfo with FileName = BComparePath and Arguments = $"@{ScriptPath} {OldFile} {NewFile} {ReportPath} /silent". I want to be able to tell whether there was an error *and* whether the old and the new files were different by looking at the .ExitCode of the C# Process object. If I wasn't running a script, I could follow the Return Codes table shown at the bottom of your Command Line Reference page. But with the script I need a way to propagate the exit code of the comparison out of the script.

                      My script has a single command which is file-report. So I guess the question is how do I make the exit code of file-report to be the exit code of the script?

                      -Y-

                      Comment

                      • Aaron
                        Team Scooter
                        • Oct 2007
                        • 15997

                        #12
                        Hello,

                        file-report does not support an exit code to capture comparison status since, as you've noted, we use the return code to indicate script success. This will need to either parse the report itself after it is generated, or use a separate /qc call to generate an ErrorLevel.

                        In cases like this, either strategy works, depending on your need. If you just need a quick error code, you can use:
                        bcomp.com /qc "c:\file1" "c:\file2"

                        then follow with your already implemented script call. This does require scanning the file twice, but is easier to implement than report parsing.
                        Aaron P Scooter Software

                        Comment

                        Working...