title: Best Practice - Testing Batch Commands for ErrorLevel
body: <p>My batch file runs several registry edit commands, and I want to implement a conditional if said registry edit commands return error levels. My question is: should I wrap these commands in a function, or create the error levels conditional after EVERY registry command? My concern regarding wrapping these commands into a function is that not every command will get checked. I want to ensure that each command gets checked, and returns an error level.</p>
  
  <p>The code chunk for wrapping the commands in a function could be incorrect or missing pieces. Not sure if I need to include an EXIT at the conclusion of each function? This is apart of a multi-step batch script.</p>
  
  <pre><code>TITLE Disable UAC
  ECHO STEP 1: Disable User Account Control
  reg add HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA /t REG_DWORD /d 0 /f
  reg add HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA /t REG_DWORD /d 0 /f
  
  IF %ERRORLEVEL% NEQ 0 ECHO An error was found w/ error code of: %ERRORLEVEL%
  </code></pre>
  
  <p>or</p>
  
  <pre><code>:disable_uac
  TITLE Disable UAC
  ECHO STEP 1: Disable User Account Control
  reg add HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA /t REG_DWORD /d 0 /f
  reg add HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA /t REG_DWORD /d 0 /f
  
  call :disable_uac
  IF %ERRORLEVEL% NEQ 0 ECHO An error was found w/ error code of: %ERRORLEVEL$
  </code></pre>
  
  <p>Regarding the first code chunk, I assume that the ERRORLEVEL conditional is only going to return the error code of the LAST command rather than the both?</p>
  
  <p>Updated Code:</p>
  
  <pre><code>@echo off
  goto ErrorCheck
  
  SetLocal EnableDelayedExpansion
  
  ECHO STEP 1: Copy Folder
  robocopy "C:\Users\!username!\Documents\WindowsSetup\Training_Videos" "C:\Users\!username!\Desktop\Training_Videos" /e /copyall
  CMD /K
  
  :ErrorCheck
  title Disable UAC
  for /F "useback skip=2 delims= eol=" %%I in ("%~f0") do (
      IF "%%~I" == ":ErrorCheck" exit /B
      ECHO %%I
      %%I &gt;&gt;"C:\errorlog.log"
      if errorlevel 1 exit /B &amp; ECHO You have encountered an issue. Look at Error Log for more information
  )
  </code></pre>