Batch file and returning values from functions












-1














I have a batch file that has multiple routines. examples



:checkFileExists
if not exist %1%2 (
echo %2 does not exist under %1
set returnValue=FAIL
exit /b 1
)
exit /b 0
:checkPortNumber
if %1 LSS 1024 (
port number should be greater than 1024 and less than 65535
set returnValue=FAIL
exit /b 1
)
... other checks for port number
exit /b 0
... main code..
set returnValue=OK
call :checkFileExists c:tmp
echo %returnValue%
call :checkFileExists c:tmp2
echo %returnValue%
call :checkPortNumber 89
echo %returnValue%


c:tmp is just an example and it exists. my return value correctly displays as OK. c:tmp2 does not exist and my returnValue correctly displays as FAIL.



I do expect checkPortNumber to fail the test but returnValue is set to OK always. I have tried with setlocal enableDelayedExpansion also and not. Is there something I am missing?










share|improve this question



























    -1














    I have a batch file that has multiple routines. examples



    :checkFileExists
    if not exist %1%2 (
    echo %2 does not exist under %1
    set returnValue=FAIL
    exit /b 1
    )
    exit /b 0
    :checkPortNumber
    if %1 LSS 1024 (
    port number should be greater than 1024 and less than 65535
    set returnValue=FAIL
    exit /b 1
    )
    ... other checks for port number
    exit /b 0
    ... main code..
    set returnValue=OK
    call :checkFileExists c:tmp
    echo %returnValue%
    call :checkFileExists c:tmp2
    echo %returnValue%
    call :checkPortNumber 89
    echo %returnValue%


    c:tmp is just an example and it exists. my return value correctly displays as OK. c:tmp2 does not exist and my returnValue correctly displays as FAIL.



    I do expect checkPortNumber to fail the test but returnValue is set to OK always. I have tried with setlocal enableDelayedExpansion also and not. Is there something I am missing?










    share|improve this question

























      -1












      -1








      -1







      I have a batch file that has multiple routines. examples



      :checkFileExists
      if not exist %1%2 (
      echo %2 does not exist under %1
      set returnValue=FAIL
      exit /b 1
      )
      exit /b 0
      :checkPortNumber
      if %1 LSS 1024 (
      port number should be greater than 1024 and less than 65535
      set returnValue=FAIL
      exit /b 1
      )
      ... other checks for port number
      exit /b 0
      ... main code..
      set returnValue=OK
      call :checkFileExists c:tmp
      echo %returnValue%
      call :checkFileExists c:tmp2
      echo %returnValue%
      call :checkPortNumber 89
      echo %returnValue%


      c:tmp is just an example and it exists. my return value correctly displays as OK. c:tmp2 does not exist and my returnValue correctly displays as FAIL.



      I do expect checkPortNumber to fail the test but returnValue is set to OK always. I have tried with setlocal enableDelayedExpansion also and not. Is there something I am missing?










      share|improve this question













      I have a batch file that has multiple routines. examples



      :checkFileExists
      if not exist %1%2 (
      echo %2 does not exist under %1
      set returnValue=FAIL
      exit /b 1
      )
      exit /b 0
      :checkPortNumber
      if %1 LSS 1024 (
      port number should be greater than 1024 and less than 65535
      set returnValue=FAIL
      exit /b 1
      )
      ... other checks for port number
      exit /b 0
      ... main code..
      set returnValue=OK
      call :checkFileExists c:tmp
      echo %returnValue%
      call :checkFileExists c:tmp2
      echo %returnValue%
      call :checkPortNumber 89
      echo %returnValue%


      c:tmp is just an example and it exists. my return value correctly displays as OK. c:tmp2 does not exist and my returnValue correctly displays as FAIL.



      I do expect checkPortNumber to fail the test but returnValue is set to OK always. I have tried with setlocal enableDelayedExpansion also and not. Is there something I am missing?







      windows batch






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 15 '18 at 7:26









      SESHAGIRI SRIRAM

      1




      1






















          1 Answer
          1






          active

          oldest

          votes


















          1














          This reordered/reworked/reformatted code behaves as expected.



          Main problem with your code is that you only set returnValue=OK once at the beginning, if a subroutine set returnValue=Fail this does persist even if another sub does exit /B 0.



          :: Q:Test20181215SU_1383774.cmd
          @echo off
          :: ... main code..

          call :checkFileExists c:tmp || Echo returned errorlevel %errorlevel%
          echo %returnValue%

          call :checkFileExists c:tmp2 || Echo returned errorlevel %errorlevel%
          echo %returnValue%

          call :checkPortNumber 89 || Echo returned errorlevel %errorlevel%
          echo %returnValue%

          Goto :Eof

          :checkFileExists
          if not exist "%~1%~2" (
          echo %2 does not exist under %1
          set returnValue=FAIL
          exit /b 1
          )
          set returnValue=OK
          exit /b 0

          :checkPortNumber
          if %1 LSS 1024 (
          echo port number should be greater than 1024 and less than 65535
          set returnValue=FAIL
          exit /b 1
          )
          :: ... other checks for port number
          set returnValue=OK
          exit /b 0





          share|improve this answer



















          • 1




            It would be a better answer if you explained why this works when the original code doesn't.
            – DavidPostill
            Dec 15 '18 at 18:26











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "3"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1383774%2fbatch-file-and-returning-values-from-functions%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          This reordered/reworked/reformatted code behaves as expected.



          Main problem with your code is that you only set returnValue=OK once at the beginning, if a subroutine set returnValue=Fail this does persist even if another sub does exit /B 0.



          :: Q:Test20181215SU_1383774.cmd
          @echo off
          :: ... main code..

          call :checkFileExists c:tmp || Echo returned errorlevel %errorlevel%
          echo %returnValue%

          call :checkFileExists c:tmp2 || Echo returned errorlevel %errorlevel%
          echo %returnValue%

          call :checkPortNumber 89 || Echo returned errorlevel %errorlevel%
          echo %returnValue%

          Goto :Eof

          :checkFileExists
          if not exist "%~1%~2" (
          echo %2 does not exist under %1
          set returnValue=FAIL
          exit /b 1
          )
          set returnValue=OK
          exit /b 0

          :checkPortNumber
          if %1 LSS 1024 (
          echo port number should be greater than 1024 and less than 65535
          set returnValue=FAIL
          exit /b 1
          )
          :: ... other checks for port number
          set returnValue=OK
          exit /b 0





          share|improve this answer



















          • 1




            It would be a better answer if you explained why this works when the original code doesn't.
            – DavidPostill
            Dec 15 '18 at 18:26
















          1














          This reordered/reworked/reformatted code behaves as expected.



          Main problem with your code is that you only set returnValue=OK once at the beginning, if a subroutine set returnValue=Fail this does persist even if another sub does exit /B 0.



          :: Q:Test20181215SU_1383774.cmd
          @echo off
          :: ... main code..

          call :checkFileExists c:tmp || Echo returned errorlevel %errorlevel%
          echo %returnValue%

          call :checkFileExists c:tmp2 || Echo returned errorlevel %errorlevel%
          echo %returnValue%

          call :checkPortNumber 89 || Echo returned errorlevel %errorlevel%
          echo %returnValue%

          Goto :Eof

          :checkFileExists
          if not exist "%~1%~2" (
          echo %2 does not exist under %1
          set returnValue=FAIL
          exit /b 1
          )
          set returnValue=OK
          exit /b 0

          :checkPortNumber
          if %1 LSS 1024 (
          echo port number should be greater than 1024 and less than 65535
          set returnValue=FAIL
          exit /b 1
          )
          :: ... other checks for port number
          set returnValue=OK
          exit /b 0





          share|improve this answer



















          • 1




            It would be a better answer if you explained why this works when the original code doesn't.
            – DavidPostill
            Dec 15 '18 at 18:26














          1












          1








          1






          This reordered/reworked/reformatted code behaves as expected.



          Main problem with your code is that you only set returnValue=OK once at the beginning, if a subroutine set returnValue=Fail this does persist even if another sub does exit /B 0.



          :: Q:Test20181215SU_1383774.cmd
          @echo off
          :: ... main code..

          call :checkFileExists c:tmp || Echo returned errorlevel %errorlevel%
          echo %returnValue%

          call :checkFileExists c:tmp2 || Echo returned errorlevel %errorlevel%
          echo %returnValue%

          call :checkPortNumber 89 || Echo returned errorlevel %errorlevel%
          echo %returnValue%

          Goto :Eof

          :checkFileExists
          if not exist "%~1%~2" (
          echo %2 does not exist under %1
          set returnValue=FAIL
          exit /b 1
          )
          set returnValue=OK
          exit /b 0

          :checkPortNumber
          if %1 LSS 1024 (
          echo port number should be greater than 1024 and less than 65535
          set returnValue=FAIL
          exit /b 1
          )
          :: ... other checks for port number
          set returnValue=OK
          exit /b 0





          share|improve this answer














          This reordered/reworked/reformatted code behaves as expected.



          Main problem with your code is that you only set returnValue=OK once at the beginning, if a subroutine set returnValue=Fail this does persist even if another sub does exit /B 0.



          :: Q:Test20181215SU_1383774.cmd
          @echo off
          :: ... main code..

          call :checkFileExists c:tmp || Echo returned errorlevel %errorlevel%
          echo %returnValue%

          call :checkFileExists c:tmp2 || Echo returned errorlevel %errorlevel%
          echo %returnValue%

          call :checkPortNumber 89 || Echo returned errorlevel %errorlevel%
          echo %returnValue%

          Goto :Eof

          :checkFileExists
          if not exist "%~1%~2" (
          echo %2 does not exist under %1
          set returnValue=FAIL
          exit /b 1
          )
          set returnValue=OK
          exit /b 0

          :checkPortNumber
          if %1 LSS 1024 (
          echo port number should be greater than 1024 and less than 65535
          set returnValue=FAIL
          exit /b 1
          )
          :: ... other checks for port number
          set returnValue=OK
          exit /b 0






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 15 '18 at 19:02

























          answered Dec 15 '18 at 9:08









          LotPings

          4,6761722




          4,6761722








          • 1




            It would be a better answer if you explained why this works when the original code doesn't.
            – DavidPostill
            Dec 15 '18 at 18:26














          • 1




            It would be a better answer if you explained why this works when the original code doesn't.
            – DavidPostill
            Dec 15 '18 at 18:26








          1




          1




          It would be a better answer if you explained why this works when the original code doesn't.
          – DavidPostill
          Dec 15 '18 at 18:26




          It would be a better answer if you explained why this works when the original code doesn't.
          – DavidPostill
          Dec 15 '18 at 18:26


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Super User!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1383774%2fbatch-file-and-returning-values-from-functions%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Paul Cézanne

          UIScrollView CustomStickyHeader Resize height generates problems when scroll is too fast

          Angular material date-picker (MatDatepicker) auto completes the date on focus out