Reputation: 1510
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.
Upvotes: 0
Views: 205
Reputation: 522
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
Upvotes: 1
Reputation: 67256
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
Upvotes: 0
Reputation: 38718
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
Upvotes: 0
Reputation: 12804
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
Upvotes: 0