为什么我的变量没有设置? 。蝙蝠

时间:2018-08-27 13:25:40

标签: batch-file

我有一个批处理文件,当我执行它时,它得到了不同的变量。我不知道我在哪里失败。它有两种选择,一种是选择哪种游戏,然后将其设置为变量game,另一种选择是选择哪种PC,然后使用go to命令。但是结果却不同。这是我的代码:

@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

这很简单,但是我很难找到它。

4 个答案:

答案 0 :(得分:1)

在您的代码中,未比较ErrorLevel,并且每次您都会获得最新set语句中设置的值。例如,Game = Crossfire,因此Game将始终返回Crossfire。 将您的代码替换为以下代码:

请参见以下语法: 变量的使用方式类似于%ERRORLEVEL%,我们应该使用EQU运算符在批处理文件中进行比较。

@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

答案 1 :(得分:0)

如果将文件重命名为.cmd,它将按预期工作。

批处理文件使用略有不同的语法。如果您坚持将其保留为.BAT,我将这样重写您的选择块:

lavalink

答案 2 :(得分:0)

我的评论作为回答,同时显示了我提到的修改和第二个ErrorLevel命令中使用的Choice方法,由于GoTo命令而安全跟随他们

@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

答案 3 :(得分:0)

这是我使用array,几个for命令和直接使用%errorlevel%值的方式:

@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