lundi 30 septembre 2019

How to set a variable if set /a fails?

I would like to check if my set /a statement failed with a Missing operator. error or not. My current batch file, only sets the variable.

@echo off & @setlocal
set "rndtest=%random% %%2+1"
if "%rndtest%" == "1" set test=1()1
) else (
set test=1+1+1+1+1
)
set "test=1+1+1+1+1+1+1+1+1"
set /a "test_123_abc=%test%"
echo %test_123_abc%

I have tried the following solution, but to no avail :

@echo off & @setlocal
set "rndtest=%random% %%2+1"
if "%rndtest%" == "1" set test=1()1
) else (
set test=1+1+1+1+1
)
set /a "test_123_abc=%test%" | find "Missing operator."
if %errorlevel% equ 0 echo Test failed!
) else (
echo Test didn't fail!
)
exit /b 0

This always evaluates to Test didn't fail.

Aucun commentaire:

Enregistrer un commentaire