i have looked for an answer and have not found one.
#echo off
:a
cls
set /p %a% =
if %a% == 1 goto b
goto c
:b
echo.
echo worked
pause
:C
echo.
echo dident work
pause
i set a = 1 and is said that the goto command was unexpected at this time. any help would be great
Changed
ok it has been change and a new problem occurs. now when a = 1 it always goes to c not b
#echo off
:a
cls
set /p a =
if "%a%" == "1" goto b
goto c
:b
echo.
echo worked
pause
:C
echo.
echo wierd
pause
#echo off
echo.
set /p a=
echo.
if %a%==1 goto b
if not %a%==1 goto c
:b
echo worked
echo.
pause
exit
:c
echo dident work
echo.
pause
exit
change this:
set /p %a% =
to
set /p a=
and
if %a% == 1 goto b
to
if "%a%" EQU "1" goto b
#echo off&cls
set /p a=Enter a number :
if "%a%" EQU "1" goto:b
echo dident work
exit/b
:b
echo Result = 1
Related
I enjoy coding, scripting and other stuff, and today my friend showed me how to make a batch file. I made one, so I run it. it works well, until I get to the one part, where it just closes.
It's supposed to go through the slides, it does that well. But when it gets to the part that it does goto t1win it just closes the .bat file.
Here is my code:
#echo off
cls
:loop
:menu
echo hello
color 0a
color 0
cls
echo This is for scripture stuff.
set /p=
goto scrn1
:scrn2
cls
echo Whats your partner's name?
set /p p2=
goto scrn3
:scrn5
cls
echo Time for the questions!
set /p=
goto scrn6
:t1win
cls
echo Yay! %p1% and %p2%, you did it!
set /p=
exit
:scrn7
cls
echo 2.
echo.
echo %p2%, How did the barges have light? (a= The barges had windows b= Jesus touched stones and the stones gave off light c= The people used lanterns)
echo.
set /p t1q2=
if %q1t2% == a goto scrn7
if %q1t2% == b goto t1win
if %q1t2% == c goto scrn7
if not defined start (
cls
goto loop
)
:scrn4
cls
echo Open the book 'Book of Mormon Stories', to page #145. Read until page #148, but still read #148. Press enter when you are done reading.
set /p=
goto scrn5
:scrn6
cls
echo 1.
echo.
echo Hey %p1%! What did the brother of Jared build? (a=barges b=boats c=planes)
echo.
set /p t1q1=
if %t1q1% == a goto scrn7
if %t1q1% == b goto scrn6
if %t1q1% == c goto scrn6
:scrn1
cls
echo Whats your name?
set /p p1=
goto scrn2
:scrn3
cls
echo Hello, %p1% and %p2%!
set /p=
goto scrn4
Please help.
BTW I don't really care if it jumps around like the sample.
this is the first question I asked, so don't judge meh!
here's the code! :D
#echo off
cls
:menu
echo hello
color 0a
color 0
cls
echo This is for scripture stuff.
set /p=
goto scrn1
:scrn2
cls
echo Whats your partner's name?
set /p p2=
goto scrn3
:scrn5
cls
echo Time for the questions!
set /p=
goto scrn6
:t1win
cls
echo Yay! %p1% and %p2%, you did it!
set /p=
exit
:scrn7
cls
echo 2.
echo.
echo %p2%, How did the barges have light? (a= The barges had windows b= Jesus touched stones and the stones gave off light c= The people used lanterns)
echo.
set /p t1q2=
if %t1q2% == a goto scrn7
if %t1q2% == b goto t1win
if %t1q2% == c goto scrn7
if not defined start (
cls
goto loop
)
:scrn4
cls
echo Open the book 'Book of Mormon Stories', to page #145. Read until page #148, but still read #148. Press enter when you are done reading.
set /p=
goto scrn5
:scrn6
cls
echo 1.
echo.
echo Hey %p1%! What did the brother of Jared build? (a=barges b=boats c=planes)
echo.
set /p t1q1=
if %t1q1% == a goto scrn7
if %t1q1% == b goto scrn6
if %t1q1% == c goto scrn6
:scrn1
cls
echo Whats your name?
set /p p1=
goto scrn2
:scrn3
cls
echo Hello, %p1% and %p2%!
set /p=
goto scrn4
Hi I make simple batch file now what I tried to add correct answers number at the end but it keeps saying zero because the variable values are not changing when the answers are chosen. Here is my code below
#echo off
title Game One
color 1f
::#############################
:one
set correctn=0
set correctn2=0
cls
echo What is 2 + 2?
echo.
echo.
echo A) 6
echo B) 4
echo C) 49
echo D) 17
echo.
echo.
echo Type the correct answer.
set /p ch1=
echo.
echo.
if not defined ch1 (goto one)
if %ch1%==A goto no
if %ch1%==A correctn=0
if %ch1%==B goto yes
if %ch1%==B correctn=1
if %ch1%==C goto no
if %ch1%==C correctn=0
if %ch1%==D goto no
if %ch1%==D correctn=0
pause>null
::#########################################
:no
cls
echo Sorry, that answer is incorrect.
echo.
echo.
echo The correct choice was B, which was 4.
pause>null
goto two
::#########################################
:yes
cls
echo You are correct. Congratulations.
echo Press any key to continue.
pause>null
goto two
::##########################################
:two
cls
echo What is 100 divided by 2?
echo A) 45
echo B) 50
echo C) 90
echo D) 17
echo.
echo.
set/p ch2=
echo.
echo.
if not defined ch2 (goto two)
if %ch2%==A goto no2
if %ch2%==A correctn2=0
if %ch2%==B goto yes2
if %ch2%==B correctn2=1
if %ch2%==C goto no2
if %ch2%==C correctn2=0
if %ch2%==D goto no2
if %ch2%==D correctn2=0
echo Invalid Choice, Please Try Again!
pause>null
::#################################
:no2
cls
echo Sorry, that answer is incorrect.
echo.
echo.
echo The correct choice was B, which was 50.
pause>null
::########################################
:yes2
cls
echo You are correct. Congratulations.
echo Press any key to continue.
pause>null
goto end
::######################################
:end
set/a correct=%correctn% + %correctn2%
echo Number of answers correct was %correct%
pause>null
So how to change variable value in if statement if already variable exists?
You need to set a variable first and then goto. As currently written, your script goes to a label yes first. Hence, if %ch1%==B set correctn=1 line is never reached:
if %ch1%==B goto yes
if %ch1%==B set correctn=1
if %ch1%==B goto yes
Moreover, IF command string comparison is case sensitive without /I switch; try the following:
if /I %ch1%==B (set correctn=1&goto yes)
I'd suggest using (Windows native) CHOICE.EXE instead of set /p for a single key-press user input, e.g as follows:
CHOICE /C abcd
IF %errorlevel%==2 (
set correctn=1
goto yes
) else (
set correctn=0
goto no
)
#echo off
:start
cls
color e
echo YOU HAVE WON $1,000,000! WHAT WILL YOU DO?
echo.
echo =================
echo -Take it (1)
echo -Leave it (2)
echo -Double it (3)
echo =================
echo.
set /p INPUT=Please specify your answer:
If /i "%INPUT%" == "1" goto 1
If /i "%INPUT%" == "2" goto 2
If /i "%INPUT%" == "3" goto 3
If /i "%INPUT%" == "Jonah" goto Jonah
If /i "%INPUT%" == "" goto Wrong
I have a strange problem. Above is a code and for some reason, when I type in "INPUT" something like 'Aiden' it would think it meant '1'. Is there a way to make every wrong answer goto wrong. (Wrong answers like Aiden which isn't even specified there. But not only Aiden, any other thing).
After your if statements you need to put a catch-all GOTO statement so if none of the others work, it will go to wrong instead of just continuing into the block right below
#echo off
:start
cls
color e
echo YOU HAVE WON $1,000,000! WHAT WILL YOU DO?
echo.
echo =================
echo -Take it (1)
echo -Leave it (2)
echo -Double it (3)
echo =================
echo.
set /p INPUT=Please specify your answer:
If /i "%INPUT%" == "1" goto 1
If /i "%INPUT%" == "2" goto 2
If /i "%INPUT%" == "3" goto 3
If /i "%INPUT%" == "Jonah" goto Jonah
goto Wrong
I recently followed some tutorials and learnt how to create and program batch files. I started making a game meant to simulate fighting monsters. One of the mechanics of this game was to be an inventory where once programmed, you'd be able to buy certain items from a store and put them in your inventory which you can already access in battle. The only programmed level so far is the tutorial and only the start of it. What you should be able to do is select an inventory slot when asked, then the computer should run the inventory function which it doesn't seem to do. Some error shows up, then closes the window automatically.If anyone could explain in simple terms why this isn't working and how i can fix it, it would really help me out. Thanks :D Here's my code...
::SETUP
#echo off
title Platformer
color 0a
::Variables
set my_level=0
set money=0
set inventory_slot_1=
set inventory_slot_2=
set inventory_slot_3=
set inventory_slot_4=
set inventory_slot_5=
set current_level=-1
set pin=0
::MONSTER VARIABLES
set monster_level = 0
::MAIN MENU
:main_menu
cls
echo.
echo PPPPP l aaaaa ttttt fffff ooooo rrrr mmmmm eeeee rrrr
echo P P l a a t f o o r r m m m e r r
echo PPPPP l aaaaa t ffff o o rrrr m m m eeee rrrr
echo P l a a t f o o r r m m m e r r
echo P l a a t f ooooo r r m m eeeee r r
echo.
echo 1990 inc.
echo.
echo LVL.%my_level% $%money%
echo ________________________________________________________________________________
echo.
echo 1 - Play
echo 2 - Save
echo 3 - Load
echo.
set /p input=">>> "
if %input%==1 goto level_select
if %input%==2 goto save
if %input%==3 goto load
pause >nul
::LEVEL SELECT
:level_select
cls
echo LVL.%my_level% $%money%
echo ________________________________________________________________________________
echo.
echo 1 - Tutorial
echo 2 - Level 1
echo 3 - level 2
echo 4 - level 3
echo 5 - Boss
echo 6 - BACK
echo.
set /p input=">>> "
if %input%==1 goto tutorial
if %input%==2 goto level_select
if %input%==3 goto level_select
if %input%==4 goto level_select
if %input%==5 goto level_select
if %input%==6 goto main_menu
goto main_menu
pause >nul
::INVENTORY
:inventory
if %~1==rock set %damage%=2
if %~1==mercy set mercy_chance=%RANDOM%*%monster_leve%/32768+1
EXIT /b
::TUTORIAL
:tutorial
cls
echo LVL.%my_level% $%money%
echo ________________________________________________________________________________
echo.
set /a current_level = 0
call:summon_dragon
pause >nul
::Monster -- Dragon
:summon_dragon
set /a monster_level=%current_level%+1
set /a rand=%RANDOM%%%3+1
set /a monster_hp=%monster_level%*3+%rand%
echo A Lvl.%monster_level% Dragon Has Been Summoned
echo What Will You Do?
echo.
echo HP: %monster_hp%
echo.
echo 1 - [ %inventory_slot_1% ]
echo 2 - [ %inventory_slot_2% ]
echo 3 - [ %inventory_slot_3% ]
echo 4 - [ %inventory_slot_4% ]
echo 5 - [ %inventory_slot_5% ]
echo 6 - [ Mercy ]
echo.
set /p input=">>> "
if input==1 callinventory %inventory_slot_1%
if input==2 call:inventory %inventory_slot_2%
if input==3 call:inventory %inventory_slot_3%
if input==4 call:inventory %inventory_slot_4%
if input==5 call:inventory %inventory_slot_5%
if input==6 (
call:inventory mercy
if %mercy_chance%==1 (
set my_level=%my_level%+1
goto win
)
)
set monster_hp=%monster_hp%-%damage%
if monster_hp<=0(
set my_level=%my_level%+1
set current_level=%current_level%+1
goto win
)
EXIT /b
::WIN
:win
cls
echo YOU WIN!
pause >nul
::SAVE
:save
cls
echo LVL.%my_level% $%money%
echo ________________________________________________________________________________
echo.
set /p pin="Pin: "
echo.
if pin==nul(set /p pin = Change Pin: goto save)
(
echo #echo off
echo set my_level=%my_level%
echo set money=%money%
) >> game_saves\%pin%.cmd
echo SAVED
pause >nul
goto main_menu
::LOAD
:load
cls
echo LVL.%my_level% $%money%
echo ________________________________________________________________________________
echo.
set /p pin="Pin: "
echo.
if pin==nul( pause >Invalid Pin goto load)
call game_saves\%pin%.cmd
echo Loaded
pause >nul
goto main_menu
if %~1==rock set %damage%=2
Is probably incorrect. If %~1 is empty, then there will be a syntax error.
You are attempting to set the variable that is the contents of damage to 2. Probably this is incorrect. If damage is not set at this time, you will get an error as cmd attempts to parse set =2
Go to Start>Programs>Accessories and set yourself a shortcut to Command Prompt.
Clicking that will put you in to cmd and you can then navigate about and run the batch by changing to the appropriate directory with cd c:\wherever\your\batch\is and simply running nameofyourbatchfile.
To exit cmd, type exit and to get help on commands type commandname /?
All acions are terminated with Enter
I wonder if there is a simple way to branch execution in a Windows batch file depending on the value of one single expression. Something akin to switch/case blocks in C, C++, C#, Java, JavaScript, PHP, and other real programming languages.
My only workaround is a plain if/else block where the same expression is repeatedly checked for equality against different values:
IF "%ID%"=="0" (
REM do something
) ELSE IF "%ID%"=="1" (
REM do something else
) ELSE IF "%ID%"=="2" (
REM do another thing
) ELSE (
REM default case...
)
So dumb. Is there a better solution?
I ended up using label names containing the values for the case expressions as suggested by AjV Jsy. Anyway, I use CALL instead of GOTO to jump into the correct case block and GOTO :EOF to jump back. The following sample code is a complete batch script illustrating the idea.
#ECHO OFF
SET /P COLOR="Choose a background color (type red, blue or black): "
2>NUL CALL :CASE_%COLOR% # jump to :CASE_red, :CASE_blue, etc.
IF ERRORLEVEL 1 CALL :DEFAULT_CASE # If label doesn't exist
ECHO Done.
EXIT /B
:CASE_red
COLOR CF
GOTO END_CASE
:CASE_blue
COLOR 9F
GOTO END_CASE
:CASE_black
COLOR 0F
GOTO END_CASE
:DEFAULT_CASE
ECHO Unknown color "%COLOR%"
GOTO END_CASE
:END_CASE
VER > NUL # reset ERRORLEVEL
GOTO :EOF # return from CALL
This is simpler to read:
IF "%ID%"=="0" REM do something
IF "%ID%"=="1" REM do something else
IF "%ID%"=="2" REM do another thing
IF %ID% GTR 2 REM default case...
Compact form for short commands (no 'echo'):
IF "%ID%"=="0" ( ... & ... & ... ) ELSE ^
IF "%ID%"=="1" ( ... ) ELSE ^
IF "%ID%"=="2" ( ... ) ELSE ^
REM default case...
After ^ must be an immediate line end, no spaces.
I guess all other options would be more cryptic. For those who like readable and non-cryptic code:
IF "%ID%"=="0" (
REM do something
) ELSE IF "%ID%"=="1" (
REM do something else
) ELSE IF "%ID%"=="2" (
REM do another thing
) ELSE (
REM default case...
)
It's like an anecdote:
Magician: Put the egg under the hat, do the magic passes ... Remove the hat and ... get the same egg but in the side view ...
The IF ELSE solution isn't that bad. It's almost as good as python's if elif else. More cryptic 'eggs' can be found here.
I searched switch / case in batch files today and stumbled upon this. I used this solution and extended it with a goto exit.
IF "%1"=="red" echo "one selected" & goto exit
IF "%1"=="two" echo "two selected" & goto exit
...
echo "Options: [one | two | ...]
:exit
Which brings in the default state (echo line) and no extra if's when the choice is found.
Hariprasad didupe suggested a solution provided by Batchography, but it could be improved a bit. Unlike with other cases getting into default case will set ERRORLEVEL to 1 and, if that is not desired, you should manually set ERRORLEVEL to 0:
goto :switch-case-N-%N% 2>nul || (
rem Default case
rem Manually set ERRORLEVEL to 0
type nul>nul
echo Something else
)
...
The readability could be improved for the price of a call overhead:
call:Switch SwitchLabel %N% || (
:SwitchLabel-1
echo One
goto:EOF
:SwitchLabel-2
echo Two
goto:EOF
:SwitchLabel-3
echo Three
goto:EOF
:SwitchLabel-
echo Default case
)
:Switch
goto:%1-%2 2>nul || (
type nul>nul
goto:%1-
)
exit /b
Few things to note:
As stated before, this has a call overhead;
Default case is required. If no action is needed put rem inside to
avoid parenthesis error;
All cases except the default one are executed in the sub-context. If
you want to exit parent context (usually script) you may use this;
Default case is executed in a parent context, so it cannot be
combined with other cases (as reaching goto:EOF will exit parent
context). This could be circumvented by replacing goto:%1- in
subroutine with call:%1- for the price of additional call overhead;
Subroutine takes label prefix (sans hyphen) and control variable. Without label
prefix switch will look for labels with :- prefix (which are valid) and
not passing a control variable will lead to default case.
Try by this way. To perform some list of operations like
Switch case has been used.
Checking the conditional statements.
Invoking the function with more than two arguments.
#echo off
:Start2
cls
goto Start
:Start
echo --------------------------------------
echo Welcome to the Shortcut tool
echo --------------------------------------
echo Choose from the list given below:
echo [1] 2017
echo [2] 2018
echo [3] Task
set /a one=1
set /a two=2
set /a three=3
set /a four=4
set input=
set /p input= Enter your choice:
if %input% equ %one% goto Z if NOT goto Start2
if %input% equ %two% goto X if NOT goto Start2
if %input% equ %three% goto C if NOT goto Start2
if %input% geq %four% goto N
:Z
cls
echo You have selected year : 2017
set year=2017
echo %year%
call:branches year
pause
exit
:X
cls
echo You have selected year : 2018
set year=2018
echo %year%
call:branches year
pause
exit
:C
cls
echo You have selected Task
call:Task
pause
exit
:N
cls
echo Invalid Selection! Try again
pause
goto :start2
:branches
cls
echo Choose from the list of Branches given below:
echo [1] January
echo [2] Feburary
echo [3] March
SETLOCAL
set /a "Number1=%~1"
set input=
set /p input= Enter your choice:
set /a b=0
set /a bd=3
set /a bdd=4
if %input% equ %b% goto N
if %input% leq %bd% call:Z1 Number1,input if NOT goto Start2
if %input% geq %bdd% goto N
:Z1
cls
SETLOCAL
set /a "Number1=%~1"
echo year = %Number1%
set /a "Number2=%~2"
echo branch = %Number2%
call:operation Number1,Number2
pause
GOTO :EOF
:operation
cls
echo Choose from the list of Operation given below:
echo [1] UB
echo [3] B
echo [4] C
echo [5] l
echo [6] R
echo [7] JT
echo [8] CT
echo [9] JT
SETLOCAL
set /a "year=%~1"
echo Your have selected year = %year%
set /a "month=%~2"
echo You have selected Branch = %month%
set operation=
set /p operation= Enter your choice:
set /a b=0
set /a bd=9
set /a bdd=10
if %input% equ %b% goto N
if %operation% leq %bd% goto :switch-case-N-%operation% if NOT goto Start2
if %input% geq %bdd% goto N
:switch-case-N-1
echo Januray
echo %year%,%month%,%operation%
goto :switch-case-end
:switch-case-N-2
echo Feburary
echo %year%,%month%,%operation%
goto :switch-case-end
:switch-case-N-3
echo march
echo %year%,%month%,%operation%
goto :switch-case-end
:switch-case-end
echo Task Completed
pause
exit
goto :start2
:Task
cls
echo Choose from the list of Operation given below:
echo [1] UB
echo [3] B
echo [4] C
echo [5] l
echo [6] R
echo [7] JT
echo [8] CT
echo [9] JT
SETLOCAL
set operation=
set /p operation= Enter your choice:
set /a b=0
set /a bd=9
set /a bdd=10
if %input% equ %b% goto N
if %operation% leq %bd% goto :switch-case-N-%operation% if NOT goto Start2
if %input% geq %bdd% goto N
:switch-case-N-1
echo Januray
echo %operation%
goto :switch-case-end
:switch-case-N-2
echo Feburary
echo %year%,%month%,%operation%
goto :switch-case-end
:switch-case-N-3
echo march
echo %year%,%month%,%operation%
goto :switch-case-end
:switch-case-end
echo Task Completed
pause
exit
goto :start2
If if is not working you use:
:switch case %n%=1
statements;
goto :switch case end
etc..
http://lallouslab.net/2016/12/21/batchography-switch-case/
It might be a bit late, but this does it:
set "case1=operation1"
set "case2=operation2"
set "case3=operation3"
setlocal EnableDelayedExpansion
!%switch%!
endlocal
%switch% gets replaced before line execution. Serious downsides:
You override the case variables
It needs DelayedExpansion
Might eventually be usefull in some cases.