I have the following code: @echo off set RETRIES=5 set COUNT=0 setlocal…
- User
I have the following code:
@echo off set RETRIES=5 set COUNT=0
setlocal enabledelayedexpansion
REM check if python is installed python --version >nul 2>&1 IF %ERRORLEVEL% NEQ 0 ( REM python is not installed echo [ERROR !DATE! !TIME!] Python installation not found. Exiting silently. exit /B 1 ) ELSE ( REM python is installed, upgrading package DataWarehouseLoadingRoutine echo *************** Starting installation *************** echo [!DATE! !TIME!] Pip installing/upgrading package DataWarehouseLoadingRoutine ... :RETRY IF %COUNT% GEQ %RETRIES% GOTO FAILED set /A COUNT+=1
for /f "delims=" %%i in ('pip install --upgrade DataWarehouseLoadingRoutine') do ( echo [!DATE! !TIME!] %%i )
IF %ERRORLEVEL% EQU 0 ( REM installation successful echo [!DATE! !TIME!] Package DataWarehouseLoadingRoutine installation successful. goto END ) ELSE ( REM installation failed echo [ERROR !DATE! !TIME!] Package DataWarehouseLoadingRoutine installation failed with error code %ERRORLEVEL%. echo [!DATE! !TIME!] Retrying in 30 seconds ... timeout /t 30 >nul goto RETRY ) :FAILED echo [ERROR !DATE! !TIME!] Package DataWarehouseLoadingRoutine installation failed after %RETRIES% attempts. Exiting silently. echo *************** Ending installation *************** timeout /t 6 >nul & exit /b :END echo *************** Ending installation *************** ) timeout /t 6 >nul & exit /b
This code does not work if an error occur in pip because at the exit of the for loop, %ERRORLEVEL% has value zero despite a failure in pip. how to fix it
- Assistant
Instead of checking the value of %ERRORLEVEL% immediately after the for loop, you can capture the error code using the "||" operator and then check the value of %ERRORLEVEL% after the command has finished. Here's the modified code:
@echo off set RETRIES=5 set COUNT=0
setlocal enabledelayedexpansion
REM check if python is installed python --version >nul 2>&1 IF %ERRORLEVEL% NEQ 0 ( REM python is not installed echo [ERROR !DATE! !TIME!] Python installation not found. Exiting silently. exit /B 1 ) ELSE ( REM python is installed, upgrading package DataWarehouseLoadingRoutine echo *************** Starting installation *************** echo [!DATE! !TIME!] Pip installing/upgrading package DataWarehouseLoadingRoutine ... :RETRY IF %COUNT% GEQ %RETRIES% GOTO FAILED set /A COUNT+=1
- User
it does not work, I get || was unexpected at this time.
- Assistant
I apologize for the confusion. It seems like the error was caused by the command prompt interpreting the "||" operator as a conditional operator. Here's an updated solution that should work:
@echo off set RETRIES=5 set COUNT=0 setlocal enabledelayedexpansion REM check if Python is installed python --version >nul 2>&1 IF %ERRORLEVEL% NEQ 0 ( REM Python is not installed echo [ERROR !DATE! !TIME!] Python installation not found. Exiting silently. exit /B 1 ) ELSE ( REM Python is installed, upgrading package DataWarehouseLoadingRoutine echo *************** Starting installation *************** echo [!DATE! !TIME!] Pip installing/upgrading package DataWarehouseLoadingRoutine ... :RETRY IF %COUNT% GEQ %RETRIES% GOTO FAILED set /A COUNT+=1 for /f "delims=" %%i in ('cmd /c "pip install --upgrade DataWarehouseLoadingRoutine 2>&1 || echo %%ERRORLEVEL%%"') do ( echo [!DATE! !TIME!] %%i ) IF !ERRORLEVEL! EQU 0 ( REM Installation successful echo [!DATE! !TIME!] Package DataWarehouseLoadingRoutine installation successful. goto END ) ELSE ( REM Installation failed set INSTALL_ERROR=!ERRORLEVEL! echo [ERROR !DATE! !TIME!] Package DataWarehouseLoadingRoutine installation failed with error code !INSTALL_ERROR!. echo [!DATE! !TIME!] Retrying in 30 seconds ... timeout /t 30 >nul goto RETRY ) :FAILED echo [ERROR !DATE! !TIME!] Package DataWarehouseLoadingRoutine installation failed after %RETRIES% attempts. Exiting silently. echo *************** Ending installation *************** timeout /t 6 >nul & exit /b :END echo *************** Ending installation *************** ) timeout /t 6 >nul & exit /b - User
After the loop: for /f “delims=” %%i in (‘cmd /c “pip install --upgrade DataWarehouseLoadingRoutine 2>&1 || echo %%ERRORLEVEL%%”’) do ( echo [!DATE! !TIME!] %%i ) Even if the pip command fails, it seems like the ERRORLEVEL is 0 which lead to the execution of the following code: IF !ERRORLEVEL! EQU 0 ( REM Installation successful echo [!DATE! !TIME!] Package DataWarehouseLoadingRoutine installation successful. goto END
How to fix that?
- Assistant
To ensure that the correct error code is captured after the pip command has finished, we need to modify the for loop slightly. Here's the modified code:
for /f "delims=" %%i in ('cmd /c "python -c "import sys;sys.exit(0 if not __import__('pip')._internal.main(['install', '--upgrade', 'DataWarehouseLoadingRoutine']) else 1)" 2>&1 || echo %%ERRORLEVEL%%"') do ( echo [!DATE! !TIME!] %%i ) IF !ERRORLEVEL! EQU 0 ( REM Installation successful echo [!DATE! !TIME!] Package DataWarehouseLoadingRoutine installation successful. goto END ) ELSE ( REM Installation failed set INSTALL_ERROR=!ERRORLEVEL! echo [ERROR !DATE! !TIME!] Package DataWarehouseLoadingRoutine installation failed with error code !INSTALL_ERROR!. echo [!DATE! !TIME!] Retrying in 30 seconds ... timeout /t 30 >nul goto RETRY )