I am trying to write a batch script to get a string in a variable and check if it is empty, and if it is empty then it is directed to a loop. The below code represents the problem
:loop
set /p cide=
IF NOT "a%1"=="a" (set cide="%1")
IF [%1]==[] goto loop
ELSE
echo IDE entered
TIMEOUT 5 > NUL
The program starts to loop again even if i give a string.
I tried to put IF [%cide%]==[] goto loop or IF %cide%==[] goto loop it gave an error stating "ELSE" not recognized.
Any help is appreciated. Thanks
You can try something like that :
#echo off
:loop
cls & Color 0A
echo Type what you want !
set /p "cide="
IF "%cide%"=="" (
Cls & Color 0C
echo You must enter something
Timeout /T 2 /NoBreak>nul
goto loop
) ELSE (
Goto Next
)
:Next
Cls
echo %cide% is entered
pause
#echo off
:loop
cls & Color 0A
echo Type what you want !
set /p "cide="
IF [%cide%]==[] (
Cls & Color 0C
echo You must enter something
choice /d y /t 2 > nul
goto loop
) ELSE (
Goto Next
)
:Next
Cls
echo %cide% is entered
pause
Related
So I'm making a very ambitious game using batch to challenge myself. I've been working on the "shop" segment of this game but for some reason when I enter 'b' or 'B' it doesn't send me back to the menu, but reads it as I wanted to buy a medkit and sends me to the "medkit purchased" line of code. I've tried to add pause >nul and pause between the shop line of code and the purchased line of code, but it didn't help. I also tried double checking to see if I typed menu right many times. There has to be something I'm missing, help appreciated, I've been messing with it for almost 2 hours now.
Edit: I have not entered the other events yet, only medkit and go back to menu.
Edit2: I've added some more variables and instead of taking me to them it takes me to the medkit code again.
Edit: I removed the percents, you guys can stop bugging me bout it. The issue still is not fixed and I do not have choice.exe so choice commands don't work.
Here is the code:
:shop
cls
echo Welcome to the shop!
echo Money: $%money%
echo.
echo 1. Buy Med Kit -$30
echo 2. Buy Shotgun Ammo (1) -$15
echo 3. Buy Assult Rifle Ammo (5) -$20
echo 4. Buy Missle (1) -$50
echo 5. View Armor
echo 6. View Modifiers
echo.
echo Enter 'B' to go back.
echo.
set /p %shopOp%=
if '%shopOp%' == '1' goto medkit1
if '%shopOp%' == '2' goto sg1
if '%shopOp%' == '3' goto ar1
if '%shopOp%' == '4' goto rpg1
if '%shopOp%' == '5' goto armors1
if '%shopOp%' == '6' goto mods1
if '%shopOp%' == 'B' goto menu
if '%shopOp%' == 'b' goto menu
:medkit1
cls
set /a money=%money%-30
if %money% LSS 0 goto noBuy1
set /a medK=%medK%+1
echo You bought 1 medkit for $30!
echo You now have %medK% med kits now!
echo.
echo Press enter to continue
pause >nul
goto shop
:noBuy1
cls
set /a money=%money%+30
echo You dont have enough money!
echo You have $%money%. You need $30.
echo.
echo Press enter to continue
pause >nul
goto shop
Okay so you don't have choice. How about xcopy?
A version adapted from the link in my comment that substitutes the choice utility with xcopy to return keypress:
#Echo off
Set Echo.ID=Call :ID.Label "%%~1" "%%~2"
Setlocal EnableDelayedExpansion
Title Menu Navigation Example
:Bar
Set "Left.Date="
IF /I "!Left.Bar!" == "Yes" Goto :Date
Call :[+Menu] "Bar" "Drink Fight Chat Leave"
Goto :Bar
:Date
Set "Left.Bar="
IF /I "!Left.Date!" == "Yes" Goto :Bar
Call :[+Menu] "Date" "Dinner Flirt Leave"
Goto :Date
:[+Menu] <Location / Core Menu option> <Sub Actions / Menu Items as list>
::: - Build menu and choice list for display
CLS
Set "CHOICES="
Set "Actions="
For %%A in (%~2) Do (
Set "Option=%%~A"
Set Actions=!Actions! "%%~A"
Set CHOICES=!CHOICES! "!Option:~0,1!"
Set "Option=[!Option:~0,1!]!Option:~1,100!"
Echo !Option!
)
::: - Return key literal from xcopy command and call relevent menu item with Identifying Params
Set "Option="
Set Key=
For /F "Delims=" %%A in ('xcopy /w "%~f0" "%~f0" 2^>nul') do (
If Not Defined Key Set "Key=%%A"
)
Set "Key=!Key:~-1!"
::: - Restrict keypress to available options, fall through to 'Goto :[Menu]' when no Call is made (Invalid keypress)
For %%O in (%~2) Do (
Set "Option=%%~O"
Set "Option=!Option:~0,1!"
If /I "!Option!" == "!Key!" Call :[%~1_%%~O-!Key!] "%%~1 %%~O" "%~1"
)
Goto :[+Menu]
:[Bar_Drink-D]
%Echo.ID%
Goto :!Loc!
:[Bar_Fight-F]
%Echo.ID%
Goto :!Loc!
:[Bar_Chat-C]
%Echo.ID%
Goto :!Loc!
:[Bar_Leave-L]
%Echo.ID%
Set "Left.Bar=Yes"
Goto :!Loc!
:[Date_Dinner-D]
%Echo.ID%
Goto :!Loc!
:[Date_Flirt-F]
%Echo.ID%
Goto :!Loc!
:[Date_Leave-L]
Set "Left.Date=Yes"
%Echo.ID%
Goto :!Loc!
:ID.Label <%~1> <%~2>
Title %~1
::: - Identify return Label
Set "Loc=%~2"
Exit /B
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
)
So I'm programming a mini-game about writing an id code, and then letting someone guess it. The problem i am facing, is that whenever someone tries to change the font color using the game syntax, just shows an error: "( was unexpected at this time." and just instantly crashes. This is my code at the moment:
#echo off
cls
:play
set /p id=Identification code:
if %id% == color:0 (
color 0
cls
goto play
) else if %id% == color:a (
color a
cls
goto play
) else if %id% == color:b (
color b
cls
goto play
) else if %id% == color:c (
color c
cls
goto play
) else if %id% == color:d (
color d
cls
goto play
) else if %id% == color:e (
color e
cls
goto play
) else if %id% == color:f (
color f
cls
goto play
) else (
cls
echo Identification stored.
pause
cls
set /p conf=Enter identification:
if %conf% == %id% (
cls
echo Access granted.
pause
exit
) else if %conf% == #cheat.code:(acss_grnt.admn)$ (
color c
cls
echo Cheating system..
timeout 3 >nul
tree
dir
timeout 1 >nul
cls
echo Access granted.
pause
exit
else (
cls
echo Access denied.
pause
exit
)
You should be aware from COLOR /? "Color attributes are specified by TWO hex digits"; you are just using one. You have not added any code to determine what the current foreground colour is. "The COLOR command sets ERRORLEVEL to 1 if an attempt is made to execute the COLOR command with a foreground and background color that are the same". Don't assume the foreground color attribute, if you're assuming it is currently 8 then add 8 after the background attribute in your COLOR commands.
You do not need to use all those ELSE IF combinations:
#ECHO OFF
:PLAY
SET/P "ID=Identification code: "
IF /I "%ID%"=="color:0" (
COLOR 08
GOTO PLAY
)
IF /I "%ID%"=="color:a" (
COLOR A8
GOTO PLAY
)
IF /I "%ID%"=="color:b" (
COLOR B8
GOTO PLAY
)
IF /I "%ID%"=="color:c" (
COLOR C8
GOTO PLAY
)
IF /I "%ID%"=="color:d" (
COLOR D8
GOTO PLAY
)
IF /I "%ID%"=="color:e" (
COLOR E8
GOTO PLAY
)
IF /I "%ID%"=="color:f" (
COLOR F8
GOTO PLAY
)
I have removed all of those CLS commands, just begin :PLAY with CLS instead.
You are missing a closing parenthesis for this block :
else if %conf% == #cheat.code:(acss_grnt.admn)$ (
color c
cls
echo Cheating system..
timeout 3 >nul
tree
dir
timeout 1 >nul
cls
echo Access granted.
pause
exit
You are also missing a closing parenthesis for this else block :
else (
cls
echo Identification stored.
pause
cls
set /p conf=Enter identification:
You should try to keep the structure simple computers/programs are there to avoid tiresome repetitions so the color thing is put in one logical line with a line continuation char ^ at the end and using conditional execution on success && or fail || of the result of the findstring.
I don't know if I could always follow your logic, so try it out.
#echo off
Set "Wait=timeout /T 3 /nobreak>NUL"
:play
cls
set "id="
set /p id=Identification code:
If "%id%"=="" Echo no input&timeout 3>NUL&goto play
IF /i "%id:~0,6%"=="color:" Echo:%id%|findstr /I "^color:[0a-f]$" >NUL 2>&1 ^
&&(color %id:~-1%&Goto play)||(Echo wrong color&%Wait%&goto play)
cls
echo Identification stored.
%Wait%
cls
set "conf="
set /p conf=Enter identification:
if "%conf%"=="%id%" (
cls
echo Access granted.
%Wait%
exit
) else if /i "%conf%"=="#cheat.code:(acss_grnt.admn)$" (
color c&cls&echo Cheating system..
%Wait%
tree&dir
timeout 1 >nul
cls&echo Access granted.
%Wait%
exit
)
cls&echo Access denied.
%Wait%
exit
The following code is supposed to do a division computation. It should ask the user to enter the first number, wait for input, and then repeat with the second number.
(Some extra info: The user is NOT allowed to enter the number 22 or 0)
So far, after you enter the first number it exits on me. I have no idea why.
Any help would be appreciated!
#echo off
cls
:START
set /p FN = Enter first number:
if %FN% == 22 GOTO EXIT
GOTO SECONDNUMBER
:EXIT
exit /b
:SECONDNUMBER
set /p SN = Enter second number:
if "%SN%" == 22 exit /b
if "%SN%" == 0 GOTO ZEROERROR
GOTO DIVIDE
:ZEROERROR
echo Sorry! You CAN NOT divide by ZERO. Please enter a new number.
pause
cls
GOTO SECONDNUMBER
:DIVIDE
set /a RESULT = %FN%/%SN%
echo %FN% divided by %SN% = %RESULT%
pause
cls
GOTO START
You can try something like that :
#echo off
Title Division Computation
:START
cls
Color 0A
set /p "FN=Enter first number : "
if "%FN%" EQU "22" Exit
::*******************************
:SECONDNUMBER
Color 0A
set /p "SN=Enter second number : "
if "%SN%" EQU "22" Exit
if "%SN%" EQU "0" GOTO ZEROERROR
GOTO DIVIDE
::*******************************
:DIVIDE
set /a RESULT=%FN% / %SN%
echo %FN% divided by %SN% = %RESULT%
pause
cls
GOTO START
::*******************************
:ZEROERROR
Color 0C
echo Sorry! You CAN NOT divide by ZERO. Please enter a new number.
pause
cls
GOTO SECONDNUMBER
::*******************************
And this is a bonus Calculator with a TypeWriter and Speaking Voice from me to you just for fun :)
#echo off
Title Calculator with a TypeWriter and Speaking Voice by Hackoo 2016
Color 0A & Mode con cols=80 lines=3
Call :TypeWriter "Hello, Welcome to my calculator. First off would you like to tell me your name ? "
echo(
Call :TypeWriter "So, Whats your name ? "
set /p name=
Call :TypeWriter "Ah, Nice to meet you %name% !"
::*************************************************************
:start
Call :TypeWriter "What is the first number you want to use ? "
set /p no=
Call :TypeWriter "What Operation do you want to use ? "
set /p op=
Call :TypeWriter "What is the second number you would like to use ? "
set /p no2=
Call :TypeWriter "%no% %op% %no2% "
set /a ans=%no% %op% %no2%
Call :TypeWriter " Well, That was an easy one the answer is %no% %op% %no2% = %ans% "
Call :TypeWriter " Would you like to do another calculaton ? Y(Yes) or N(No) ?"
set /p Question=
If /I "%Question%"=="Y" (goto :start) else (Goto :end)
::*************************************************************
:TypeWriter
Cls
echo(
(
echo strText=wscript.arguments(0^)
echo intTextLen = Len(strText^)
echo intPause = 150
echo For x = 1 to intTextLen
echo strTempText = Mid(strText,x,1^)
echo WScript.StdOut.Write strTempText
echo WScript.Sleep intPause
echo Next
echo Set Voice=CreateObject("SAPI.SpVoice"^)
echo voice.speak strText
)>%tmp%\%~n0.vbs
#cscript.EXE /noLogo "%tmp%\%~n0.vbs" "%~1"
exit /b
::**************************************************************
:end
Exit
::*************************************************************
I'm trying to make a helpful batch file for work and for it, I'm trying to input notes/text to a .txt file via a variable. It works great with just entering text without spaces (ex: "test"), but once you type something with at least 1 space in it, the cmd closes out. (ex: "test test") I can't figure out why this is happening so I'm left with you guys here. Any/all help would be appreciated!
#echo off
color 9F
title Notes
CLS
del %UserProfile%\Documents\Notes.txt
echo Issue/Request: >> %UserProfile%\Documents\Notes.txt
:StartCallNotes
CLS
echo ================== Notes =================
type %UserProfile%\Documents\Notes.txt
echo ==========================================
set /p NotesEnter=Enter Notes:
set NewNote="%NotesEnter%"
if %NotesEnter% == CLS goTo :CLS
echo %NotesEnter% >> "%UserProfile%\Documents\Notes.txt"
goTo :StartCallNotes
:CLS
del %UserProfile%\Documents\Notes.txt
echo Issue/Request: >> "%UserProfile%\Documents\Notes.txt"
goTo :StartCallNotes
exit
I think the problem is when you compare the user's input with CLS.
Try to put quotes in %NotesEnter% when compare the value like this :
#echo off
color 9F
title Notes
CLS
del %UserProfile%\Documents\Notes.txt
echo Issue/Request: >> %UserProfile%\Documents\Notes.txt
:StartCallNotes
CLS
echo ================== Notes =================
type %UserProfile%\Documents\Notes.txt
echo ==========================================
set /p NotesEnter=Enter Notes:
set NewNote="%NotesEnter%"
REM if user doesn't input the value echo a new line
if "%NotesEnter%" == "" echo. >> "%UserProfile%\Documents\Notes.txt"
if "%NotesEnter%" == CLS goTo :CLS
if NOT "%NotesEnter%" == "" echo %NotesEnter% >> "%UserProfile%\Documents\Notes.txt"
REM reset the variable value
set NotesEnter=
goTo :StartCallNotes
:CLS
del %UserProfile%\Documents\Notes.txt
echo Issue/Request: >> "%UserProfile%\Documents\Notes.txt"
goTo :StartCallNotes
exit