Home > database >  Why does my errorlevels don't list my echo messages in my batch script? They just list them all
Why does my errorlevels don't list my echo messages in my batch script? They just list them all

Time:07-04

I have made a list of names of which I wish to choose with several numbers, and the do loop confirms these numbers. But for some reason I cannot get these chosen numbers aligned to the GOTO echo statement. Instead they just list them all? I have to admit I am new to batch scripting but I'm sure there is an abundance of experts who can see where I am going wrong, pls pls help me?

The code i have is here along with the output I get in command prompt

:START

setlocal EnableDelayedExpansion

@echo OFF

 

echo.  [RUN BVTS]

ECHO 1.RS_CommonDataSet

ECHO 2.RS03_PackageManager

ECHO 3.RS04_SequencerCalendars

ECHO 4.RS06_EditorTestsPerformance

ECHO 5.RS06_NewEditorTests
ECHO OFF

set /p BVTSUITE="What BVT suite do you wish to run? "
rem This lists all the BVT's that you want to run with spaces

set n=0
for %%a in (%BVTSUITE%) do (
set /A n =1
set "BVTSUITE[!n!]=%%~a"
)

 rem List the file names

for /L %%i in (1,1,%n%) do (
echo %%i- !BVTSUITE[%%i]!
)

 
IF %errorlevel%==1 set goto 1
:1
ECHO "You have chosen number RS_CommonDataSet"

IF %errorlevel%==2 set goto 2
:2
ECHO "You have chosen number RS03_PackageManager"

IF %errorlevel%==3 set goto 3
:3
ECHO "You have chosen number RS04_SequencerCalendars"

IF %errorlevel%==4 set goto 4
:4
ECHO "You have chosen number RS06_EditorTestsPerformance"

IF %errorlevel%==5 set goto 5
:5
ECHO "You have chosen number RS06_NewEditorTests"

The output I get in command prompt is:

 [RUN BVTS]

1.RS_CommonDataSet

2.RS03_PackageManager

3.RS04_SequencerCalendars

4.RS06_EditorTestsPerformance

5.RS06_NewEditorTests

What BVT suite do you wish to run? 1 3 (I chose these numbers)

1- 1

2- 3

"You have chosen number RS_CommonDataSet"

"You have chosen number RS03_PackageManager"

"You have chosen number RS04_SequencerCalendars"

"You have chosen number RS06_EditorTestsPerformance"

"You have chosen number RS06_NewEditorTests"

As you can see my 1 and 3 was chosen but it is not in the ouput, it just shows the entire list?

CodePudding user response:

I've added comments to the parts that I've added, but basically, your entire script runs because you never told it not to. Batch scripts are just a list of commands and the order to run them in, so unless you use a goto, a call, or an exit, each line will run one after the next.

If you use subroutines, you'll be able to control which parts of the script get executed. I've also added super rudimentary input validation because sooner or later, the user will enter something incorrectly. It's not foolproof, but it's good enough that I'm comfortable posting it.

@echo off
setlocal EnableDelayedExpansion

echo.  [RUN BVTS]
echo 1.RS_CommonDataSet
echo 2.RS03_PackageManager
echo 3.RS04_SequencerCalendars
echo 4.RS06_EditorTestsPerformance
echo 5.RS06_NewEditorTests

:get_suite_list
set /p "BVTSUITE=What BVT suite do you wish to run? "
rem This lists all the BVTs that you want to run with spaces
if not defined BVTSUITE goto :get_suite_list

set n=0
for %%a in (%BVTSUITE%) do (
    set /A n =1
    set "BVTSUITE[!n!]=%%~a"
)

REM List the file names
for /L %%i in (1,1,%n%) do (
    echo %%i - !BVTSUITE[%%i]!
    
    REM Double-check that all given BVT suites are valid and the user didn't
    REM provide an invalid value like 7 or Q or something
    find ":suite_!BVTSUITE[%%i]!" "%~f0" >nul 2>nul || (
        echo Invalid suite !BVTSUITE[%%i]! given.
        goto :get_suite_list
    )
)

REM Actually call the different suites.
for /L %%i in (1,1,%n%) do (
    call :suite_!BVTSUITE[%%i]!
)

REM At this point, the main script ends and returns to the command prompt
exit /b

REM These are subroutines. You'll never be able to reach them unless you use
REM the CALL command to tell cmd to jump to here, and then once the exit /b
REM command is executed, flow returns to where it was when CALL was executed.
REM Also, I've prepended the string suite_ in front of each label because
REM having a single number as a label is a terrible idea since it's absolutely
REM meaningless.
:suite_1
echo You have chosen number RS_CommonDataSet
exit /b

:suite_2
echo You have chosen number RS03_PackageManager
exit /b

:suite_3
echo You have chosen number RS04_SequencerCalendars
exit /b

:suite_4
echo You have chosen number RS06_EditorTestsPerformance
exit /b

:suite_5
echo You have chosen number RS06_NewEditorTests
exit /b

CodePudding user response:

You don't need to identify each file name via the given suite individually. Just define an array of file names and select the appropiate element via the given value:

@echo OFF
setlocal EnableDelayedExpansion


rem Define array with the names of the suites
set numSuites=0
for %%a in (RS_CommonDataSet RS03_PackageManager RS04_SequencerCalendars RS06_EditorTestsPerformance RS06_NewEditorTests) do (
   set /A numSuites =1
   set "BVTNAME[!numSuites!]=%%a"
)

rem Show the menu
echo   [RUN BVTS]
echo/
for /L %%i in (1,1,%numSuites%) do (
   echo %%i.!BVTNAME[%%i]!
)
echo/

set /p "BVTSUITE=What BVT suite do you wish to run? "
rem This lists all the BVT's that you want to run with spaces (and file names)

set n=0
for %%i in (%BVTSUITE%) do (
   set /A n =1
   if defined BVTNAME[%%i] (
      echo !n!-  %%i.!BVTNAME[%%i]!
   ) else (
      echo !n!-  Invalid suite: %%i
   )
)
  • Related