windows bat files: a %random% number as answer to an question - windows

i am making a simple little text game in a bat file.
now, i reached a point where i want the bat file to generate a random number (using %random%); and then it will popup for 2 secs (using echo and timeout). then you should enter it in a if /p .
if you enter the right one, it will goto sb3. if its not the right one, it will goto gameover.
is this even possible?
cls
echo remember these
set ran=%random%
echo %ran%
timeout 2
cls
set /p sb2=what was the numbers? if you fail the game will quit.
if %sb2%==%ran% goto sb3
currently, whatever you type will proceed to sb3.

Do you have a code path for the alternative? If you just have the if ... goto then it will just continue with the next statement if the condition wasn't met. If your code looks like this:
if %sb2%==%ran% goto sb3
:sb3
...
then it's not surprising that you'll always end up there.
You need to jump out explicitly:
if %sb2%==%ran% goto sb3
goto :eof
:sb3
...

Related

My script wont redirect to my other classes?

I created a variable and I assigned it to an input. But no matter what I input it just redirects back to :EditUser. I don't know why it is doing that.
This is what my code is. Can someone tell me why it wont direct to the other classes. This is the only redirection in my script that won't work.
:EditUser
cls
echo You chose to edit a user
echo ========================
echo 1. Change Password
echo 2. Change Account Type
echo 3. Go back to Users
echo.
set /p CHOICE6=What do you want to do?
if CHOICE6==1 goto PassChange
if CHOICE6==2 goto PrivChange
if CHOICE6==3 goto Users
goto EditUser
You forgot To wrap your variables with % to make recognise them as such. It should be %CHOICE6% Additionally, you should either enclose the if statement query in double quotes to eliminate possible whitespace if "%CHOICE6%"=="1" or even better seeing as it is numerical, use if %CHOICE6% equ 1
Anyway, seeing as you are working with the obvious of Choice then just use choice instead of set /p
:EditUser
cls
echo You chose to edit a user
choice /c 123 /m "Select (1) Change Password (2) change account type (3) go back to users"
goto :opt%errorlevel%
:opt1
echo change password stuff here.
goto :eof
:opt2
echo change account type stuff goes here
goto :eof
:opt3
goto :users
goto :eof
So what Really happens is that we just goto a label starting with opt appended by the errorlevel returned by choice
It also only allows one of the alphanumeric values entered only, anything else you type will not be acted on. So in this case, only 1, 2 or 3 will be acted on by cmd.
see choice /? for more detail.
Also, if you want to retain your echo layout and not have the options in one line, add the echo's and remove the message /m swirtch from choice:
:EditUser
cls
echo You chose to edit a user
echo ========================
echo 1. Change Password
echo 2. Change Account Type
echo 3. Go back to Users
echo(
choice /c 123
goto :opt%errorlevel%
:opt1
echo change password stuff here.
goto :eof
:opt2
echo change account type stuff goes here
goto :eof
:opt3
goto :users?
goto :eof
You simply forgot to wrap your variables in % when using them.
however, you woudl do well to use the good practice of wrapping SET commands in " as well
Also, you could simplify your life a little by using choice, although it's still possible to get the choice to error out so YMMV
:EditUser
cls
echo.You chose to edit a user
echo.========================
echo.1. Change Password
echo.2. Change Account Type
echo.3. Go back to Users
echo.
set /p "CHOICE6=What do you want to do? "
IF %CHOICE6% EQU 1 goto :PassChange
IF %CHOICE6% EQU 2 goto :PrivChange
if %CHOICE6% EQU 3 goto :Users
goto :EditUser
That said for best practice you should write functions using CALL and you can return choice variables to the main code to act on or do it from within the function as suites best.
CALL is "SAFE" as it will return to the current point of execution on a function or script ending, and you can write scripts in a better practice.
REM Example main function
:Main
REM ...
REM Call Edit User, Specify a variable name to be returned
CALL :EditUser "CALL_THIS"
REM Call the function we chose in the :Edit User Function and was returned to us here, and may be :PassChange, :PrivChange, :Users
CALL %CALL_THIS%
REM ...
GOTO :EOF
:EditUser
REM Clear the Variable Provided to the fucntion to be returned
SET "%~1="
REM Clear the Choice
SET "CHOICE6="
cls
echo.You chose to edit a user
echo.========================
echo.1. Change Password
echo.2. Change Account Type
echo.3. Go back to Users
echo.
CHOICE /C 123 /N /M "What do you want to do? "
SET "CHOICE6=%ERRORLEVEL%"
REM Check an arbitrary number of choices without having to write a full IF on each.
FOR %%_ IN (
1:PassChange
2:PrivChange
3:Users
) DO (
REM Split choices to test them:
FOR /F "Tokens=12 Delims=:" %%A (
IF %%A EQU %CHOICE6% (
REM Set The Variable given the function '%1' to a value to return to Main script when the Choice was matched.
SET "%~1=:%%B"
)
)
REM Exit Function when %~1 is defined.
IF DEFINED %~1 GOTO :EOF
)
REM Show screen again when not matched
GOTO :EditUser
Yes more code but as you can see now you have a reusable function that could be called an arbitrary number of times as your code requires, and allows the outer code whether in another function or in the main code to execute the next called function.
But even in places where you don't need to call another function you can use the loop logic here to store a list of choices and evaluate them without having to re-type all fo your if statements.
Further, you could write that more generically too and not have any multiple-choice variables re-using the choice function logic each time as the callable function, and allowing you to write less repetitive and smaller footprint code.

Changing variable values within If statements

I'm trying to take some user input defined variables to decide whether or not to enter the loop and execute a copy and rename followed by deletion of the original file as it will no longer be needed.
set /p multiTune="Does your tune file need to be shared with multiple
Element sequences? (y/n) "
Echo Multi Tune is %multiTune%
if "%multiTune%"=="y" (set /p tuneCount="How many sequences will need to share your tune file? ")
Echo Tune count is %tuneCount%
pause
if "%multiTune%"=="y" (SET /p tuneName="Enter the file letter/number combination for the R quant of your tune file. ") else (#ECHO The user specified there is no need for a second tune.)
Echo Tune Name is %tuneName%
pause
if "%multiTune%"=="y" (SET /a tuneCount+=1) else(set /a tuneCount-=tuneCount)
Echo Tune count is now %tuneCount%
pause
:loop
if "%tuneCount%"=="0" goto exitloop
Set /p seqNumber="Enter the number for one of the sequences."
copy %tuneName%.D.pdf "S%seqNumber%-TUN1_%tuneName%.D.pdf"
echo %tuneName%.D.pdf renamed to S%seqNumber%-TUN1_%tuneName%.D.pdf
pause
Set /a tuneCount-=1
if "%tuneCount%"=="1" DEL "%tuneName%.D.pdf"
if "%tuneCount%"==1 goto exitloop
Echo %tuneCount%
pause
goto loop
:exitloop
All of the echos and pauses are just for testing purposes to make sure I have the correct values in my variables.
The batch file runs fine with the variables containing the correct strings and values up until the line:
if "%multiTune%"=="y" (SET /a tuneCount+=1) else(set /a tuneCount-=tuneCount)
The file says something is unexpected and closes at this point so i havent gotten a chance to figure out if the looped portion even works. The point of the +1 is so that it enters the loop and executes the commands until it gets to 1 and skips the loop if it equals 0.
I read a bunch of information about setlocal delayedexpansion and using !'s around variables instead of %'s. I'm not sure how to implement this or if this applies to my problem at all. I know there is probably an easier way to do the if statements but I'm a novice and that was the easiest way for me to understand it as I've been learning on the fly through trial and error, and everything you see is the results of a single day of learning.
Any help would be much appreciated. I tried to be as detailed as possible about what it is I'm trying to do but if you have any questions I will do my best to answer.
I really think you are making the things so MUCH complicated...
Have a corrected piece of the code you provided us (Note: I did not touch the loop subroutine to be on-topic):
#echo off
choice /m "Does your tune file need to be shared with multiple element sequences? (y/n) " /C:yn /N
rem Echo Multi Tune is %errorlevel%
rem If errorlevel equals to 1 user input is "Y", it is 2 it is "N". (I commented the "echo" command as it changes the errorlevel value).
if errorlevel 2 goto question_N
if errorlevel 1 goto question_Y
:question_Y
set /p tuneCount="How many sequences will need to share your tune file? "
set /p tuneName="Enter the file letter/number combination for the R quant of your tune file. "
SET /a "tuneCount+=1"
goto loop
:question_N
set /p tuneCount="How many sequences will need to share your tune file? "
ECHO The user specified there is no need for a second tune.
set /a "tuneCount-=tuneCount"
goto loop
:loop
rem [Code you provided above]
I hope you are fine with this, testing it and it works!

Batch File: Getting user input during execution of other commands, then checking it later

What I want is a command (or series of commands) that works with Windows 7, 8.1, and 10. It needs to collect user input at any time during the execution of the batch file it's in, only to checked and interpreted later. I do not mind using an input text file.
I've already searched for an answer, but the closest I've come to it is <nul set /p "input=", but it requires the user to press a key and hit enter at the exact moment the command is run. Any help would be greatly appreciated.
This method utilizes GOTO to create a loop which checks the first line of input.txt every 6 seconds or so. You could replace the content of :DOSTUFF with anything you want. Let me know if you have questions.
#echo off
GOTO DOSTUFF
:CHECKINPUT
for /f %%a in (input.txt) do (
if %%a NEQ "" (
set "input=%%a"
GOTO GOTINPUT
)
exit /b
)
GOTO DOSTUFF
:GOTINPUT
echo Thanks for the input!
echo Here is what you entered:
echo %input%
GOTO ENDER
:DOSTUFF
echo I could be doing other things here, but instead I'm just waiting for input...
PING 127.0.0.1 -n 6 >nul
GOTO CHECKINPUT
:ENDER
pause
While this was running in one window, I just ran echo test>input.txt in another command prompt.
To make this more robust you might want to overwrite the file after you check it. This can easily be done with echo.>input.txt

Windows Batch file - getting user input but only the first command gets executed

I have windows batch script which is used to get user input. Its like a menu with multiple options shown to the user. When the first option is given it works fine. But when i give the second option too, it executes the first only. What is the issue here?
CODE:
#echo off
:Menu
echo 1. OPTION1
echo 2. OPTION2
echo.
set /P INPUT=Enter your choice: %=%
If "%INPUT%" =="1" goto ONE
If "%INPUT%" =="2" goto TWO
:ONE
some commands..........
goto end
PAUSE
:TWO
some commands..........
goto end
PAUSE
:end
pause
You have specified it to goto end in each instance regardless of answer 1 or 2, this means if you put an answer on 1 it will go to 1 then execute then goto end, if you put 2 the same would happen but it would goto 2 then to end.
FOUND IT! Was very simple. Its not accepting variable name in CAPS. When i changed it from INPUT to input, it works!

Batch File not working for the given input

Following is the batch file that I have created, for any input its only showing is "NO INPUT PROVIDED" string, I tried searching on google and tried many things but non solved my problem.
#echo off
ECHO Please provide input... Valid input is :: Y/y (For changing path) or N/n (For not changing the path).
SETLOCAL
SET /p change=
if "%~1" equ "" GOTO ERROREND
if /I "%change%" equ "Y" GOTO YES
if /I "%change%" equ "N" GOTO NO
:YES
ECHO Y SELECTED.
GOTO SUCCESS
:NO
ECHO N SELECTED.
GOTO SUCCESS
:ERROREND
ECHO Input not recognized.
GOTO FAILURE
:SUCCESS
ECHO Task completed succcess fully.
pause
:FAILURE
ECHO NO INPUT PROVIDED.
pause
Help needed. Thank you for reading this question.
if "%~1" equ "" GOTO ERROREND
means 'if the first parameter to the batchfile is empty', so if you were to run this batch from the prompt as
yourbatchname something
then something is the first parameter.
You probably menat to use "%change%", not `"%~1".
Note: if not defined change is probably a better test.
Note: Simply pressing Enter when prompted by a set /p does not set the variable to empty. It leaves the value unchanged. Best to use
set "var="
set /p var=....
Note: batch simply charges on, instruction by instruction. It has no concept of 'end-of-procedure'. Consequently, if your entry is neither Y nor N (once you've got the %~1 issue resolved) batch will simply continue execution past the if statements to the next - the ECHO Y SELECTED.

Resources