0

I have a batch file and when I execute it, it got different variable. I don't know where I fail. It has two choices, first selecting on what game then set it to variable game then another choice to select what PC then it uses go to command. but the result is different. This is my code:

@ECHO OFF
CLS
ECHO Copy Game Files From Choices Below
ECHO 1.Crossfire
ECHO 2.Dota2
ECHO 3.Dragon Nest
ECHO 4.Fortnite
ECHO 5.Rules Of Survival
ECHO 6.World Of Tantra
ECHO.

CHOICE /C 123456 /M "Enter your choice:  "
:: Note - list ERRORLEVELS in decreasing order
IF ERRORLEVEL 6 Set Game=WorldOfTantra
IF ERRORLEVEL 5 Set Game=RulesOfSurvival
IF ERRORLEVEL 4 Set Game=Fortnite
IF ERRORLEVEL 3 Set Game=DragonNest
IF ERRORLEVEL 2 Set Game=Dota2
IF ERRORLEVEL 1 Set Game=Crossfire


::=====================================

ECHO Select PC to Copy Files
ECHO 1.PC1
ECHO 2.PC2
ECHO 3.PC3
ECHO 4.PC4
ECHO.

CHOICE /C 1234 /M "Enter your choice:  "

:: Note - list ERRORLEVELS in decreasing order
IF ERRORLEVEL 4 SET PCCode=JeraldPunx04 GOTO CopyFiles
IF ERRORLEVEL 3 SET PCCode=JeraldPunx03 GOTO CopyFiles
IF ERRORLEVEL 2 SET PCCode=JeraldPunx02 GOTO CopyFiles
IF ERRORLEVEL 1 SET PCCode=JeraldPunx01 GOTO CopyFiles

::=====================================

:CopyFiles
echo "Test Variable: %Game% in %PCCode%"
if %Game%==WorldOfTantra (
echo "Copying %Game% in %PCCode%"
)
if %Game%==RulesOfSurvival (
echo "Copying %Game% in %PCCode%"
)
if %Game%==Fortnite (
echo "Copying %Game% in %PCCode%"
)
if %Game%==DragonNest (
echo "Copying %Game% in %PCCode%"
)
if %Game%==Dota2 (
echo "Copying %Game% in %PCCode%"
)
if %Game%==Crossfire (
echo "Copying %Game% in %PCCode%"
)

GOTO End

::=====================================


:End
pause

It's just a simple but yet I'm having hard time to trace this.

Storm Spirit
  • 1,440
  • 4
  • 19
  • 42
  • Use quotes (to avoid trouble with white-spaces or other special characters): `if "%Game%"=="WorldOfTantra" ( ... )` – aschipfl Aug 27 '18 at 13:37
  • `If ErrorLevel 6` will work as expected, unfortunately, all the `ErrorLevel`'s `5..1` will also be true for an `ErrorLevel` of `6` too!, Because `If ErrorLevel 1` means if the error level is `1` or higher. Change it to this syntax instead, `If "%ErrorLevel%"=="6" Set "Game=WorldOfTantra"` etc. instead. – Compo Aug 27 '18 at 13:42
  • Why you don't directly use `%errorlevel%` value? Suppose you have the N variable that may have values between 1 and 6, and you want to add it to SUM variable. What would you do: `IF %N% EQU 1 SET /A SUM=SUM+1` & `IF %N% EQU 2 SET /A SUM=SUM+2`, etc, or just a simple `SET /A SUM=SUM+%N%`? **`:/`** See [my answer](https://stackoverflow.com/a/52043335/778560) below... – Aacini Aug 27 '18 at 16:24

4 Answers4

1

In your code, ErrorLevel was not compared and every time you will get the value which was set in the latest set statement. For example, Game=Crossfire so Game will always return Crossfire. Replace your code with below code:

Please see the syntax below: Variables are used like %ERRORLEVEL% and we should use EQU operator for comparison in the batch file.

@ECHO OFF
CLS
ECHO Copy Game Files From Choices Below
ECHO 1.Crossfire
ECHO 2.Dota2
ECHO 3.Dragon Nest
ECHO 4.Fortnite
ECHO 5.Rules Of Survival
ECHO 6.World Of Tantra
ECHO.

CHOICE /C 123456 /M "Enter your choice:  "
:: Note - list ERRORLEVELS in decreasing order
IF %ERRORLEVEL% EQU 6 Set Game=WorldOfTantra
IF %ERRORLEVEL% EQU 5 Set Game=RulesOfSurvival
IF %ERRORLEVEL% EQU 4 Set Game=Fortnite
IF %ERRORLEVEL% EQU 3 Set Game=DragonNest
IF %ERRORLEVEL% EQU 2 Set Game=Dota2
IF %ERRORLEVEL% EQU 1 Set Game=Crossfire

::=====================================

ECHO Select PC to Copy Files
ECHO 1.PC1
ECHO 2.PC2
ECHO 3.PC3
ECHO 4.PC4
ECHO.

CHOICE /C 1234 /M "Enter your choice:  "

:: Note - list ERRORLEVELS in decreasing order
IF %ERRORLEVEL% EQU SET PCCode=JeraldPunx04 GOTO CopyFiles
IF %ERRORLEVEL% EQU SET PCCode=JeraldPunx03 GOTO CopyFiles
IF %ERRORLEVEL% EQU SET PCCode=JeraldPunx02 GOTO CopyFiles
IF %ERRORLEVEL% EQU SET PCCode=JeraldPunx01 GOTO CopyFiles


::=====================================

:CopyFiles
echo "Test Variable: %Game% in %PCCode%"
if %Game%==WorldOfTantra (
echo "Copying %Game% in %PCCode%"
)
if %Game%==RulesOfSurvival (
echo "Copying %Game% in %PCCode%"
)
if %Game%==Fortnite (
echo "Copying %Game% in %PCCode%"
)
if %Game%==DragonNest (
echo "Copying %Game% in %PCCode%"
)
if %Game%==Dota2 (
echo "Copying %Game% in %PCCode%"
)
if %Game%==Crossfire (
echo "Copying %Game% in %PCCode%"
)

GOTO End

::=====================================
:End
pause
Yogen Darji
  • 3,230
  • 16
  • 31
Trupti J
  • 512
  • 1
  • 4
  • 16
0

If you rename your file to .cmd, it works as expected.

Batch files use a slightly different syntax. If you insist on keeping it a .BAT, I would rewrite your choice blocks like this:

CHOICE /C 123456 /M "Enter your choice:  "
SET ErrLvl=%errorlevel%
IF %ErrLvl%==6 Set Game=WorldOfTantra
IF %ErrLvl%==5 Set Game=RulesOfSurvival
IF %ErrLvl%==4 Set Game=Fortnite
IF %ErrLvl%==3 Set Game=DragonNest
IF %ErrLvl%==2 Set Game=Dota2
IF %ErrLvl%==1 Set Game=Crossfire
UnhandledExcepSean
  • 12,504
  • 2
  • 35
  • 51
0

My comment as an answer, showing both the modification I mentioned and the ErrorLevel method you used in the second Choice command, which is safe due to the GoTo commands following them:

@Echo Off
ClS
Echo Copy Game Files From Choices Below
Echo 1. Crossfire
Echo 2. Dota2
Echo 3. Dragon Nest
Echo 4. Fortnite
Echo 5. Rules Of Survival
Echo 6. World Of Tantra
Echo(

Choice /C 123456 /M "Enter your choice"
If "%ErrorLevel%"=="6" Set "Game=WorldOfTantra"
If "%ErrorLevel%"=="5" Set "Game=RulesOfSurvival"
If "%ErrorLevel%"=="4" Set "Game=Fortnite"
If "%ErrorLevel%"=="3" Set "Game=DragonNest"
If "%ErrorLevel%"=="2" Set "Game=Dota2"
If "%ErrorLevel%"=="1" Set "Game=Crossfire"

::=====================================

Echo Select PC to Copy Files
Echo 1. PC1
Echo 2. PC2
Echo 3. PC3
Echo 4. PC4
Echo(

Choice /C 1234 /M "Enter your choice"
If ErrorLevel 4 Set "PCCode=JeraldPunx04" & GoTo CopyFiles
If ErrorLevel 3 Set "PCCode=JeraldPunx03" & GoTo CopyFiles
If ErrorLevel 2 Set "PCCode=JeraldPunx02" & GoTo CopyFiles
If ErrorLevel 1 Set "PCCode=JeraldPunx01" & GoTo CopyFiles

::=====================================

:CopyFiles
Echo "Test Variable: %Game% in %PCCode%"

If "%Game%"=="WorldOfTantra" (
    Echo "Copying %Game% in %PCCode%"
)
If "%Game%"=="RulesOfSurvival" (
    Echo "Copying %Game% in %PCCode%"
)
If "%Game%"=="Fortnite" (
    Echo "Copying %Game% in %PCCode%"
)
If "%Game%"=="DragonNest" (
    Echo "Copying %Game% in %PCCode%"
)
If "%Game%"=="Dota2" (
    Echo "Copying %Game% in %PCCode%"
)
If "%Game%"=="Crossfire" (
    Echo "Copying %Game% in %PCCode%"
)

::=====================================

:End
Pause
Compo
  • 36,585
  • 5
  • 27
  • 39
0

This is the way I would do this, using an array, a couple for commands and a direct use of %errorlevel% value:

@ECHO OFF
rem Next command in needed to process array elements
setlocal EnableDelayedExpansion

rem Define "games" array at same time the menu is displayed
CLS
ECHO Copy Game Files From Choices Below
set "i=0"
for %%a in (Crossfire Dota2 "Dragon Nest" Fortnite "Rules Of Survival" "World Of Tantra") do (
   set "game=%%~a"
   set /A i+=1
   echo !i!.!game!
   set "games[!i!]=!game: =!"
)
ECHO/

CHOICE /C 123456 /M "Enter your choice:  "
rem Assign the corresponding element from "games" array
Set "Game=!games[%errorlevel%]!"

::=====================================

ECHO Select PC to Copy Files
for /L %%i in (1,1,4) do ECHO %%i.PC%%i
ECHO/

CHOICE /C 1234 /M "Enter your choice:  "
SET PCCode=JeraldPunx0%errorlevel%

::=====================================

:CopyFiles

rem "Game" variable can only have one of the previously assigned values,
rem so it is not necessary to check any value

echo "Copying %Game% in %PCCode%"

GOTO End

::=====================================

:End
pause
Aacini
  • 65,180
  • 12
  • 72
  • 108