Several errorlevels in same script?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
KgOlve
Posts: 14
Joined: 30 Oct 2012 06:34

Several errorlevels in same script?

#1 Post by KgOlve » 19 Apr 2013 14:59

Hi,
I'm wondering if it's possible to have several %errorlevel% in the same script, or rather that %errorlevel% is overwritten by a new %errorlevel% as the script progresses?

I have a script which in 3 stages will search a text that's piped into "find" for 3 different specific strings. This works when there's only 1 stage searching for 1 string, but it won't work now that i added 3, the %errorlevel% won't update after the first stage, the result the errorlevel gets in the 1st stage remains throughout the script regardless of the 2 latter stages.

Here's a snippet of my script

Code: Select all

echo on
cd /D C:\
set fn=%~n1

mkvmerge.exe -i "%fn%.mkv" | find /I "Track ID 2: audio"
if %errorlevel% LEQ 0 (
goto:audiotrack2
)
if %errorlevel% GEQ 1 (
goto:extracttrack1
)

:extracttrack1
mkvmerge.exe -i "%fn%.mkv" | find /I "Track ID 1: audio"
if %errorlevel% LEQ 0 (
goto:audiotrack1
)
if %errorlevel% GEQ 1 (
goto:extracttrack3
)

:extracttrack3
mkvmerge.exe -i "%fn%.mkv" | find /I "Track ID 3: audio"
if %errorlevel% LEQ 0 (
goto:audiotrack3
)
if %errorlevel% GEQ 1 (
echo Error no audio track found
goto:exit
)

:audiotrack1
echo Audiotrack1

:audiotrack3
echo Audiotrack3

:audiotrack2
echo Audiotrack2

:exit
PAUSE


Ofcourse, piping from mkvmerge into a .txt file, and then using the find functions on the .txt file should be more resource efficient, however i believe i'd still get the same problem with the errorlevel.

Thanks for any help.

Regards,
KgOlve

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Several errorlevels in same script?

#2 Post by Endoro » 19 Apr 2013 15:45

The %errorlevel% changes its value after every command, that supports it.
To keep it put it in an other variable.


Example for mkvmerge without the need to use %errorlevel%:

Code: Select all

C:\Video>mkvmerge --output-charset utf-8 --ui-language en -i "input.mkv"
File 'input.mkv': container: Matroska
Track ID 0: video (V_MPEG4/ISO/AVC)
Track ID 1: audio (A_DTS)
Track ID 2: audio (A_DTS)
Track ID 3: subtitles (S_VOBSUB)
Track ID 4: subtitles (S_VOBSUB)
Chapters: 1 entries

C:\Video>for /f "delims=:" %i in ('mkvmerge -i "input.mkv" ^| find "audio"') do @echo %i
Track ID 1
Track ID 2

KgOlve
Posts: 14
Joined: 30 Oct 2012 06:34

Re: Several errorlevels in same script?

#3 Post by KgOlve » 20 Apr 2013 01:14

Hi,

Thanks for your help, i just noticed now that my errorlevel does change with each command like i thought it didn't. It was because i had mixed up some directories previous in the script.

Your script does indeed look like it might do what i wanted it to do too - With some editing to what it gives out, i don't want the ultimate result to be an echo afterall, i want it to start a process depending on which tracks it finds.

Thanks again.

Regards,
KgOlve

Post Reply