I'm trying to make a Batch file that will increment a variable by 1 each time it loops, and then check if the variable is equal to 5, and if it isn't, it loops again. I know there's probably a while loop for this, but I didn't know how to do that, and I'm just enjoying learning Batch for fun right now
Here's the code, it doesn't work the way it should, it just displays a 0: and then does nothing else. So how would I go about fixing it? I have a feeling I'm setting and incrementing the variable wrong, and maybe it's confused about the 2 if statements? (Does it have an else if....?) Anyways, thanks for the help
#echo off
set /p i=0:
goto A
:A
set /p i=i+1:
if i != 5 goto C
if i == 5 goto B
:C
echo Test :D
:B
pause>nul
Note: I don't know a lot of Batch and I'm not a pro, but I like to learn and I'm just doing this for future reference, and because I enjoy it. So, this code probably isn't good, but I want to know how I can accomplish this.
This is a way to simulate the while loop you are trying to accomplish. Only one goto is needed:
#echo off
set /a x=0
:while
if %x% lss 5 (
echo %x%
pause>nul
set /a x+=1
goto :while
)
echo Test :D
You can do that with a simple FOR command :
for /l %%x in (0,1,100) do (
echo %%x
)
You can replace 100 by the number you want
To set a numerical value to a variable, you may use the /a switch:
The /A switch specifies that the string to the right of the equal sign
is a numerical expression that is evaluated.
(Type SET /? for all the help).
Second, check your goto flow - this never loops back to A.
Third, check the syntax of the if expression (!= doesn't exist in batch).
This should work:
#echo off
set var1=0
:loop
set /a var1=%var1%+1
echo %var1%
if %var1% EQU 5 (
goto :end
) else (
goto :loop
)
:end
pause
#echo off
set a=0
:Count
set /a a=a+1
echo %a%
goto Count
try this:
#if (#CodeSection == #Batch) #then
#echo off
:a
cls
color c
echo --------------
echo start:
echo --------------
set /p start=
cls
echo --------------
echo start: %start%
echo --------------
echo --------------
echo stop:
echo --------------
set /p stop=
cls
echo --------------
echo start: %start%
echo --------------
echo --------------
echo stop: %stop%
echo --------------
echo.
echo.
echo Start in:
timeout /t 2 /nobreak >nul
echo. 5
timeout /t 1 /nobreak >nul
echo. 4
timeout /t 1 /nobreak >nul
echo. 3
timeout /t 1 /nobreak >nul
echo. 2
timeout /t 1 /nobreak >nul
echo. 1
timeout /t 1 /nobreak >nul
cls
echo --------------
echo start: %start%
echo --------------
echo --------------
echo stop: %stop%
echo --------------
echo.
echo.
echo.
echo ============================================
set SendKeys=CScript //nologo //E:JScript "%~F0"
%SendKeys% ">----"
%SendKeys% "{enter}"
:while
echo %start%
%SendKeys% "%start%"
%SendKeys% "{enter}"
set /a start=start+1
timeout /t 1 /nobreak >nul
if %start% leq %stop% goto :while
goto :end
:end
echo ============================================
%SendKeys% ">----"
%SendKeys% "{enter}"
:c
echo count again? Y/N
set /p return=
if %return% == Y goto :a
if %return% == N goto :b
goto :c
:b
#end
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));
Related
I am trying to make an "admin" login and it isn't working.
#echo off
title RPG BETA
color 0b
set /p player=Welcome to Xero, what's your name?:
if %player% == Admin goto admin
:intro
echo Hello %player%!
pause
goto start
:admin
set /p password:What is the admin password?:
if %password% == Insertanypassword goto true
if %password not insertanypassword goto false
:true
cls
echo Welcome, Admin!
set /p af=Would you like to enable admin features?[y/n]:
if %af% == y goto true1
if %af% == n goto start
:true1
cls
set CPU=20
set CPUN=Scorpion
set CPUD=3
set gun=9999999
set gund=9999999
set playerh=9999999
goto fight
:start
cls
echo In the distant future, the world was on the brink of destrucion.
timeout 3 >nul
echo In the midst of the wasteland, a single man, named %player%, will overcome the odds
pause
goto BFight
:BFight
cls
set CPU=20
set CPUN=Scorpion
set CPUD=3
set gun=1
set gund=10
set playerh=20
:Fight
cls
if %CPU% leq 0 goto win
if %playerh% == 0 goto lose
cls
echo You encounter a %CPUN%!
echo %CPUN% Health: %CPU%
echo.
echo Your Health: %playerh%
echo [1]Shoot (%gund% damage) (%gun%)
echo [2]Punch (3 damage)
echo [3]Flee
set /p fp=What do you do?
if %fp% == 1 goto gun
if %fp% == 2 goto punch
if %fp% == 3 goto flee
:gun
cls
if '%gun%'=='0' goto egun
set /a gun = gun - 1
echo You fire at the %CPUN%
timeout 4 >nul
echo It hits!
set /a CPU = CPU - gund
pause
goto cpufightp
:punch
cls
echo You punch the %CPUN%
timeout 4 >nul
echo It hits!
set /a CPU = CPU - 3
pause
goto cpufightp
:egun
echo You have no bullets!
pause
goto Fight
:cpufightp
if %CPU% leq 0 goto win
cls
echo %CPUN% Health: %CPU%
echo.
echo Your Health: %playerh%
pause
cls
goto cpufight
:cpufight
cls
echo The %CPUN% Attacks!
timeout 4 >nul
echo It hits!
set /a playerh = playerh - CPUD
pause
goto fight
:flee
goto losef
:losef
cls echo you have fled
pause
goto suggestion
:lose
cls
echo You died
pause
cls
:win
cls
echo Congradulations, %player%! You win!
pause
cls
:suggestion
set /p suggest=What should I add to Xero?:
echo %player%: %suggest% >> xerosuggest.word
Whenever I type in "Admin" instead of going to the password screen, it just closes cmd, can anyone tell me why? i have experimented with ''s and other commands but I just can't figure it out.
The problem with respect to your specific issue is a typo.
You have:
set /p password:What is the admin password?:
Instead of:
Set /P "password=What is the admin password?: "
The doublequotes are simply good practice, your error was quite clearly that you used a : instead of a =.
I want to make is so that in my game it doesn't just continue the code if you press enter without entering a choice. How can I do this? By this I mean if you press enter without entering a 1, 2, 3, etc. Is there a command to allow me to do this?
#echo off
SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
set "DEL=%%a"
)
:start
echo You have been studying for years for this moment, to create a human master race, all you have to do is complete the circuit connecting the lightning rods to the speciman. Do you do it?
ping -n 2 1.1.1.1 > nul
echo.
echo 1) Connect the circuit.
echo 2) No, you take your work and burn it.
echo.
set /p Choice=Choose Now:
if "%Choice%"=="1" goto awaken
if "%Choice%"=="2" goto end1
:awaken
cls
echo 3
timeout /t 2 /nobreak >nul
cls
echo 2
timeout /t 2 /nobreak >nul
cls
echo 1
timeout /t 2 /nobreak >nul
cls
echo CRACK!!
timeout /t 2 /nobreak >nul
cls
echo Lightning strikes the rod and you see movement coming from the speciman under the sheet on the table.
pause
cls
echo The creature sits up. It is more disgusting then you ever could have imagined, you are terrified. It stares at you with mindless eyes, what do you do?
echo.
echo 1) Run for your life.
echo 2) Stay in the Room.
echo.
set /p Choice=Choose Now:
if "%Choice%"=="1" goto run4life
if "%Choice%"=="2" goto staycalm
:run4life [
cls
]
:staycalm [
cls
echo The monster stares at you.
timeout /t 4 /nobreak >nul
echo It screams
timeout /t 1 /nobreak >nul
call :colorEcho 0a "RAAAAUUUGGGHHH"
timeout /t 2 /nobreak >nul
echo.
echo 1) Run for your life.
echo 2) Stay in the Room.
echo.
set /p Choice=Choose Now:
if "%Choice%"=="1" goto run4life
if "%Choice%"=="2" goto staycalm2
]
:staycalm2
[
cls
timeout /t 3 /nobreak >nul
echo You hear a knock at the door.
call :colorEcho 0a " Hello, anyone there? Someone reported hearing a scream from your residence."
echo.
echo How should you react?
echo.
echo 1) Jump out the window.
echo 2) Answer the door.
echo.
set /p Choice=Choose Now:
if "%Choice%"=="1" goto run4life
if "%Choice%"=="2" goto staycalm3
]
:staycalm3
[
cls
call :colorEcho 0c "Oh yes, I was just scared by a spider."
echo.
timeout /t 2 /nobreak >nul
echo.
call :colorEcho ob "uhhmmm..."
timeout /t 2 /nobreak >nul
echo.
echo.
call :colorEcho 0b "Well, do you mind if I come in just to check around?
]
:end 1
cls
echo Congratulations, you have completed the game without causing anyone to die!
echo.
echo 1) Exit
echo 2) Play Again!
echo.
set /p Choice=Choose Now:
if "%Choice%"=="1" goto
if "%Choice%"=="2" goto first
:colorEcho
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1i
This is one way:
echo.
:loop1
set "Choice="
set /p Choice=Choose Now:
if "%Choice%"=="1" goto run4life
if "%Choice%"=="2" goto staycalm
goto :loop1
I am trying to color specific parts of the text and have managed to succeed partially. I am using the call :colorEcho line to color the text. The line that doesn't work is line 72. It works the first time, in line 54, but not the next. I was just wondering if anyone here knows how to fix this.
BTW I got the code from here
#echo off
SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
set "DEL=%%a"
)
:start
echo You have been studying for years for this moment, to create a human master race, all you have to do is complete the circuit connecting the lightning rods to the speciman. Do you do it?
ping -n 2 1.1.1.1 > nul
echo.
echo 1) Connect the circuit.
echo 2) No, you take your work and burn it.
echo.
set /p Choice=Choose Now:
if "%Choice%"=="1" goto awaken
if "%Choice%"=="2" goto end1
:awaken
cls
echo 3
timeout /t 2 /nobreak >nul
cls
echo 2
timeout /t 2 /nobreak >nul
cls
echo 1
timeout /t 2 /nobreak >nul
cls
echo CRACK!!
timeout /t 2 /nobreak >nul
cls
echo Lightning strikes the rod and you see movement coming from the speciman under the sheet on the table.
pause
cls
echo The creature sits up. It is more disgusting then you ever could have imagined, you are terrified. It stares at you with mindless eyes, what do you do?
echo.
echo 1) Run for your life.
echo 2) Stay in the Room.
echo.
set /p Choice=Choose Now:
if "%Choice%"=="1" goto run4life
if "%Choice%"=="2" goto staycalm
:run4life [
cls
]
:staycalm [
cls
echo The monster stares at you.
timeout /t 4 /nobreak >nul
echo It screams
timeout /t 1 /nobreak >nul
call :colorEcho 0a "RAAAAUUUGGGHHH"
timeout /t 2 /nobreak >nul
echo.
echo 1) Run for your life.
echo 2) Stay in the Room.
echo.
set /p Choice=Choose Now:
if "%Choice%"=="1" goto run4life
if "%Choice%"=="2" goto staycalm2
]
:staycalm2
[
cls
timeout /t 3 /nobreak >nul
echo You hear a knock at the door.
call :colorEcho 0a " Hello, anyone there? Someone reported hearing a scream from your residence."
echo.
echo How should you react?
echo.
echo 1) Jump out the window.
echo 2) Answer the door.
echo.
set /p Choice=Choose Now:
if "%Choice%"=="1" goto run4life
if "%Choice%"=="2" goto staycalm3
]
:staycalm3
[
cls
call :colorEcho 0c "Oh yes, I was just scared by a spider."
echo.
timeout /t 2 /nobreak >nul
echo.
call :colorEcho ob "uhhmmm..."
timeout /t 2 /nobreak >nul
echo.
echo.
call :colorEcho 0b "Well, do you mind if I come in just to check around?
]
:end 1
cls
echo Congratulations, you have completed the game without causing anyone to die!
echo.
echo 1) Exit
echo 2) Play Again!
echo.
set /p Choice=Choose Now:
if "%Choice%"=="1" goto
if "%Choice%"=="2" goto first
:colorEcho
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1i
You are using a primitive version of the color print routine that cannot handle characters that are invalid in file names: \, /, :, ", ?, *, &, |, <, >. The version you are using attempts to create a file with a name equal to your displayed string, so it cannot work for your question string.
The top three answers at How to have multiple colors in a Windows batch file? have more sophisticated versions (more complicated), that can handle nearly any character.
The question mark in the text is screwing it up. By the way, I think you mean line 70 is the one that's failing.
Okay, so, I'm making a crude text-based batch game demo, its a sword-fighting game.... I've never used any sort of coding before, and I taught myself as best I could... I know this is a pretty primitive and easy to learn language, but I don't quite understand what I did wrong... It doesn't process damage to you OR the enemy, and the stat randomizer sometimes give out numbers equal to or below 0... Ive been tweaking, and changing code in ways I don't really understand, trying to fix it, so the code may be a little weird at some parts, feel free the constructively criticize, improve parts that MAY work, and PLEASE find the problems stated... (PS. its also programmed to recognize me as the "creator" and my girlfriend, and calls her by her nickname, but that works fine, so Ill remove it from this code.)
the code:
#echo off
color 02
cls
setlocal enabledelayedexpansion
set file=AdventureTextMusic.mp3
( echo Set Sound = CreateObject("WMPlayer.OCX.7"^)
echo Sound.URL = "%file%"
echo Sound.Controls.play
echo do while Sound.currentmedia.duration = 0
echo wscript.sleep 100
echo loop
echo wscript.sleep (int(Sound.currentmedia.duration^)+1^)*1000) >sound.vbs
start /min sound.vbs
title New Adventurer
echo.
echo Hello Adventurer!
echo.
echo may I ask your name?
echo.
set /p name=
echo.
if %name% equ Michelle goto bunni
if %name% equ Stuart goto kitty
if %name% neq Stuart goto random
:bunni
cls
echo.
echo Bunni? Welcome to my game! I worked hard on it, I don't know if it will be good... We'll see I guess!!
set /a gold=%random% %% 25-1
echo.
echo Here's %gold% gold to get you started! I hope it helps!
echo.
set name=Bunni
echo Good luck %name%!
pause
goto continue
:kitty
cls
echo.
echo Creator? Bug fixing I assume? I hope all is well in the land of Adventure Text! Good luck!
set /a gold=%random% %% 25-1
echo.
echo Here's %gold% gold to get you started! I hope it helps!
pause
goto continue
:random
cls
echo.
echo That name suits you! Welcome %name%! To the land of Adventure Text!
set /a gold=%random% %% 15-1
echo.
echo Here's %gold% gold to get you started! I hope it helps!
echo.
echo Good luck %name%!
pause
goto continue
:continue
cls
echo The controls are simple, when it asks for your choice of attack, pick an option.
echo.
echo Slash does your weapons default damage, minus enemy armor strength, and takes no stamina
echo.
echo Slice does your weapons default damage, ignoring armor strength, and takes 2 stamina
echo.
echo whereas Stab does 1.5 damage, ignoring armor strength, but takes 4 stamina
echo.
pause
cls
echo.
echo Would you like to begin, %name%?
pause
title %name%'s Quest
cls
set /a health=%random% %% 15-1 +2
set /a armor=%random% %% 2-1 +2
set /a damage=%random% %% 4-2 +2
set /a stamina=%random% %% 15-8 +2
echo.
echo Health: %health% Gold: %gold% Armor Strength: %armor% Damage per Hit: %damage% Stamina: %stamina%
pause
cls
echo.
set /a number=%random% %% 2-1
if %number% equ 1 goto Grunt
if %number% equ 2 goto Guard
:Guard
set enemy=Guard
set EHealth=12
set EArmor=1
set EDamage=1
goto battle1
:Grunt
set enemy=Grunt
set EHealth=10
set EArmor=0
set EDamage=2
goto battle1
:battle1
echo An enemy %enemy% has spotted you!
echo Enemy Health: %EHealth% Enemy Armor Strength: %EArmor% Enemy Damage: %EDamage%
pause
cls
echo.
goto turn1
:turn1
echo What would you like to do?
echo 1) Slash
echo 2) Slice
echo 3) Stab
set /p attack=
if %attack% equ 1 goto slash
if %attack% equ 2 goto slice
if %attack% equ 3 goto stab
:turn2
echo Health: !health! Stamina: !stamina!
if !EHealth! leq 0 goto end
echo Enemy Health: !EHealth!
echo Enemy turn!
pause
cls
goto enemy
:enemy
set /a EAttack=%random% %% 3-1
if %EAttack% equ 1 goto Eslash
if %EAttack% equ 2 goto Eslice
if %EAttack% equ 3 goto Estab
if %EAttack% neq 3 goto enemy
:Eslash
set health-=!EDamage!-!armor!
if !health1 leq 0 goto lose
goto turn1
:Eslice
set health-=!EDamage!
if !health! leq 0 goto lose
goto turn1
:Estab
set EDamage*=1.5
set health-=!EDamage!
set EDamage/=1.5
if !health! leq 0 goto lose
goto turn1
:slash
set EHealth-=!damage!-!EArmor!
goto turn2
:slice
if %stamina% lss 2 cls
if %stamina% lss 2 echo.
if %stamina% lss 2 echo You do not have enough stamina to Slice
if %stamina% lss 2 goto turn1
set EHealth-=!damage!
set stamina-=2
goto turn2
:stab
if %stamina% lss 4 cls
if %stamina% lss 4 echo.
if %stamina% lss 4 echo You do not have enough stamina to Stab
if %stamina% lss 4 goto turn1
set damage*=1.5
set EHealth-=!damage!
set damage/=1.5
set stamina-=4-2
goto turn2
:lose
cls
echo.
echo Im sorry %name%... but you lost...
pause
cls
echo This game is in pre-alpha, I will likely be updating it to add the full story.
echo Created by: SteweeBee
pause
taskkill /f /im "wscript.exe"
exit
:end
cls
echo.
echo ---- ---- ---- [----] ------- ----
echo \ \ / \ / / I I I \ I I
echo \ \ / \ / / I I I I\ \I I
echo \ \_/ /\ \_/ / I I I I \ I I
echo \ / \ / I I I I \ I
echo ------ ------ [----] I___I \___I
pause
cls
echo This game is in pre-alpha, I will likely be updating it to add the full story.
echo Created by: SteweeBee
pause
taskkill /f /im "wscript.exe"
exit
set /a health=%random% %% 15-1 +2
The line above is the same as
set /a health=%random% %% 16
and I think you want this:
set /a health=(%random% %% 15-1) +2
The line below uses delayed expansion but you don't use code that requires it (like in loops) - You could just use normal expansion like %health%
!health!
This code will work, unless another character is added when typing and then it will fail. The variable attack should also be initialised before the set /p because set /p will remember what it was the last time through if enter is pressed without input. Input like 1 2 with the space will cause it to fail too (ditto with &).
set /p attack=
if %attack% equ 1 goto slash
if %attack% equ 2 goto slice
if %attack% equ 3 goto stab
It should really have a check for invalid input at the end - just a goto :get_input to loop back for input will do.
i have a question . It is possible to make a batch menu to accept multiple commands at the same time?
Example : ( my code )
#ECHO OFF
set tries=6
:top
cls
set /a tries=%tries% -1
if %tries%==0 (
goto penalty
)
Echo You have %tries% attempts left.
Echo Please enter your password to proceed
set /p password=
if %password%==Parola ta (
echo Welcome Your Name
ping localhost -n 5 >nul
cls
Echo CONNECTED!
C:
CD\
CLS
:MENU
CLS
ECHO ============= MENU NAME =============
ECHO -------------------------------------
ECHO 1. System Information TXT file
ECHO 2. Selection 2
ECHO 3. Selection 3
ECHO 4. Selection 4
ECHO 5. Selection 5
ECHO 6. Selection 6
ECHO 7. Selection 7
ECHO -------------------------------------
ECHO 8. Selection 8
ECHO -------------------------------------
ECHO 9. Selection 9
ECHO -------------------------------------
ECHO ==========PRESS 'Q' TO QUIT==========
ECHO.
SET INPUT=
SET /P INPUT=Please select a number:
IF /I '%INPUT%'=='1' GOTO Selection1
IF /I '%INPUT%'=='2' GOTO Selection2
IF /I '%INPUT%'=='3' GOTO Selection3
IF /I '%INPUT%'=='4' GOTO Selection4
IF /I '%INPUT%'=='5' GOTO Selection5
IF /I '%INPUT%'=='6' GOTO Selection6
IF /I '%INPUT%'=='7' GOTO Selection7
IF /I '%INPUT%'=='8' GOTO Selection8
IF /I '%INPUT%'=='9' GOTO Selection9
IF /I '%INPUT%'=='Q' GOTO Quit
CLS
ECHO ============INVALID INPUT============
ECHO -------------------------------------
ECHO Please select a number from the Main
echo Menu [1-9] or select 'Q' to quit.
ECHO -------------------------------------
ECHO ======PRESS ANY KEY TO CONTINUE======
PAUSE > NUL
GOTO MENU
:Selection1
systeminfo.exe>systeminfo.txt
ECHO ============INVALID INPUT============
ECHO -------------------------------------
ECHO Please select a number from the Main
echo Menu [1-9] or select 'Q' to quit.
ECHO -------------------------------------
ECHO ======PRESS ANY KEY TO CONTINUE======
PAUSE > NUL
GOTO MENU
:Selection2
Call cleanup.bat
:Selection3
and in here too...
:Selection4
and so on
:Selection5
and so on
:Selection6
and so on
:Selection7
and so on
:Selection8
and so on
:Selection9
and so on
:Quit
CLS
ECHO ==============THANKYOU===============
ECHO -------------------------------------
ECHO ======PRESS ANY KEY TO CONTINUE======
PAUSE>NUL
EXIT
pause
cls
) else (
goto top
)
goto top
How to make the program accept more than 1 command?
For example 1,3,5 to execute in the same time?
or another question, how its possible to undo .exe back to .bat?
there such a program?
Suggestion: make call from goto and concatenate the commands with &:
IF /I '%INPUT%'=='1' GOTO:Selection1
IF /I '%INPUT%'=='2' CALL:Selection2&CALL:Selection2&CALL:Selection4
IF /I '%INPUT%'=='3' CALL:Selection3&CALL:Selection3
IF /I '%INPUT%'=='4' CALL:Selection4&CALL:Selection4
IF /I '%INPUT%'=='5' CALL:Selection5
IF /I '%INPUT%'=='6' CALL:Selection6
IF /I '%INPUT%'=='7' CALL:Selection7
IF /I '%INPUT%'=='8' CALL:Selection8
IF /I '%INPUT%'=='9' CALL:Selection9
IF /I '%INPUT%'=='Q' GOTO:Quit
To get this work you must alse add goto:eof (eof=end of file) after the jump labels, eg.:
:Selection2
Call cleanup.bat
goto:eof
:Selection3
and in here too...
goto:eof
:Selection4
and so on
goto:eof
:Selection5
and so on
goto:eof
...
..
.
goto:eof returns control to the main program.
Is this what you were looking for?
File Menu.cmd
#echo off
setlocal
set quit=false
set /p InputChoices=Enter Choice(s) (A,B,C)
echo %InputChoices%
call :executeChoices %InputChoices%
endlocal
goto :eof
:executeChoices
if [%1]==[] goto :eof
call :Step%1
shift
goto :executeChoices
goto :eof
:StepA
echo Step A
goto :eof
:StepB
echo Step B
goto :eof
:StepC
echo Step C
goto :eof
Works like this:
c:\>Menu.cmd
Enter Choice(s) (A,B,C) b c a
b c a
Step B
Step C
Step A
You can use the start "" command keyword and each command will run in it's own window, but initiated by your menu batch file.