B
B
Brendan Castaneda2021-01-21 16:26:53
cmd/bat
Brendan Castaneda, 2021-01-21 16:26:53

How to write a condition in a bat file?

I am running a bat file.
If an error occurs during execution, I throw it on the error label .
ping -invalid-arg || goto error
But how to make a condition: if there is no error at this moment, transfer it to the next label?

those. I need a solution
if the file gives an error when executed, throw it on the error label .
if the file does not give an error, either we transfer it to the next label or, even more simply, we continue execution.
The main task is to catch the error and throw it on the label for further work of the file.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
B
Brendan Castaneda, 2021-01-21
@ae_ph

I found a solution!
We check for errors, if there are any, then ERRORLEVEL 1 will work if there is no ERRORLEVEL 0 .
Accordingly, we assign different labels for each of them.

if ERRORLEVEL 1 goto answer1
if ERRORLEVEL 0 goto answer0

D
dollar, 2021-01-21
@dollar

Add the next line.

ping -invalid-arg || goto error
goto next

W
wisgest, 2021-02-09
@wisgest

ping -invalid-arg && goto GOOD || goto BAD
And besides, for some internal commands, this is the only way, since they do not set ERRORLEVEL , but the success of their execution is checked using &&/ ||.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question