Trouble in call command - batch-file

I'm usually stuck on making a game now...I can't solve the the call command problem This is an example :::
:verifile1
cls
echo.
echo Before you can continue give out the following information...
echo.
echo What is your username?
echo.
set /p name1=Username:
if not exist "%name1%_1.bat" (
echo Invalid Username
pause>nul
goto welcome
)
echo.
echo Your password?
echo.
set /p pass1=Password:
call label %name1%_1.bat
if not %password1% EQU %pass1% (
echo Password entered do not match
pause>nul
goto welcome
)
goto Story
Please help me with this case

if you are calling a label then call it thus
Call :Label arg

call label %name1%_1.bat
calls an utility named label (yes, there happens to be one...) and gives it %name1%_1.bat as parameter. Probably not quite what you want.
To call a label inside your batchfile, use:
call :label
But I guess, you simply want to call your second batchfile %name1%_1.bat. To do so, simply:
call %name1%_1.bat

Related

Require user to enter correct code to continue

I have this batch program. I want a verification system where you type the code, and it lets you continue. If you type the wrong code it won't let you continue.
Anyone got some code I can use for this?
say if you wanted the password to be "1234" you would put:
#echo off
echo Enter the correct code:
set /p InputPass=Code:
if '%InputPass%'=='1234' goto :AccessGranted
goto :AccessDenied
:AccessGranted
cls
echo Access Granted!
timeout 3 /nobreak >nul
goto :continue
:AccessDenied
cls
echo Access Denied!
echo Program on halt!
pause >nul
goto :AccessDenied
:continue
And then you would put in the code you want to execute if the user got it right, under the last line (which says :continue)

How to check for certain text input in batch file?

I have a batch file that lets the user type in something then takes what they typed in and puts it as a value, let's say the value is called %input%.
I want the batch file to check for certain words in the %input% value and then use the goto command.
Anyone got any solutions?
Thanks.
EDIT
Here is some code that I am using:
#echo off
title Test
:Loop
Set /p Input=""
if "%input%"=="word *" goto function
I just need help figuring out how to check for "word" in the %input% value.
Hopefully this clears some things up.
Give a try for this code :
#echo off
:MainLoop
Color 0B
Cls
set /p "Input=Type something here "
echo %Input%|findstr /i "\<hello\>">nul && goto HelloFunction || goto NoHelloFunction
:HelloFunction
cls
Color 0A
echo I am into The Hello Function
pause
Goto MainLoop
:NoHelloFunction
cls
Color 0C
echo I am not into the Hello Function
pause
Goto MainLoop
Explanation
\< and \> means "Word boundaries", this avoids false positives (Helloo, Chello,...)
>nul redirects the found string to nul to keep the screen clear.
&& executes the set command only, if previous command (findstr) was successful.
|| means else and goto to another function

BATCH program crashes after goto command

This code is part of a chat program that I am currently working on. The 'else' part of my program is the one that doesn't work. The program quits instead of going to :home
:join
cls
if not exist "C:/Users/Public/room.cmd" (
echo No room has been found.
echo.
set /p choiceretry=Do you want to retry? y/n
if "%choiceretry%"=="y" goto join
if "%choiceretry%"=="n" goto home
) else (
cls
"C:/Users/Public/room.cmd"
echo A room has been found.
pause >nul
echo Joining
set roomjoined=1
echo %roomjoined%
goto home
)
:home
echo this finally works
pause
I have tried changing the code several times starting from 'echo Joining'
Anyone know why cmd quits?...
:) :) :)
Thanks in advance
The problem is the way you run room.cmd; you must use call to return from it:
call "C:/Users/Public/room.cmd"
Otherwise, execution will not return from room.cmd to the original batch file that ran it.
Hint: Consider to use choice instead of set /P for Y/N decisions.
Firstly, please don't left justify your code blocks. It's much easier to read code that's properly indented.
Secondly, when retrieving values within a code block, you need delayed expansion. See setlocal /? in a cmd prompt for more information. This is the reason for the unexpected behavior. Your variables retrieved within the same parenthetical code block in which they were set won't contain the values you expect unless you retrieve them with delayed expansion syntax. As an alternative, you could use the choice command and if errorlevel, which would result in a bit nicer user experience I think.
Thirdly, when testing user input, you should use the /i switch in your if statements for case-insensitivity. This isn't relevant if using choice / if errorlevel though.
Fourthly, Windows paths use backslashes, not forward slashes.
I'd fix it this way:
#echo off
setlocal
:join
cls
if errorlevel 1 set /P "=Retrying... "<NUL
if not exist "C:\Users\Public\room.cmd" (
echo No room has been found.
echo.
choice /c yn /n /m "Do you want to retry? [y/n] "
if errorlevel 2 goto home
goto join
) else (
"C:\Users\Public\room.cmd"
echo A room has been found.
pause >nul
echo Joining
set roomjoined=1
)
:home
echo this finally works
pause

Batch - If not defined a variable

What can I do if I want to echo a warning if the variable favnum is not filled?
I already tried this:
echo What's your favorite color?
echo.
set /p favnum=">"
if not defined favnum(
goto nope
)
give it a space between favnum and (
As you wrote it, if checks for a variable named favnum(. It then misses a command to execute and spits a syntax error.
As pointed out previously you missed out a space between favnum and (
You could also use
echo What's your favorite color?
echo.
set /p favnum=">"
if "%favnum%"=="" (
goto nope
)
or put is the variable not set on one line
echo What's your favorite color?
echo.
set /p favnum=">"
if "%favnum%"=="" goto nope
which would make your example
echo What's your favorite color?
echo.
set /p favnum=">"
if not defined favnum goto nope

I need a way to prompt a user for an input using batch

I noticed a post on how to make a batch that opens another batch upon an input. However, I need a way that will make the code pause until the correct input is entered, then continue.
So for example, the code would ask what is the access code?
Then the user would input the correct code, for example 123.
Then the code would say "Welcome!"
Then it would execute another question like "What do you want to do today?"
There would be a list of options:
A. Game
B. Browse
C. Nevermind
The user would in put a, b, or c and the script would start either a game or web browser. If the user selected C, then it would exit.
Thanks for the help!
#echo off
echo Welcome!
echo What do you want to do today?
echo.
echo A. Game
echo.
echo B. Browse
echo.
echo C. Nevermind
echo.
choice /C:ABC /N /M "Enter your choice: "
if "%errorlevel%"=="1" goto :game
if "%errorlevel%"=="2" goto :browse
if "%errorlevel%"=="3" goto :nevermind
goto :error
I think a little bit modified version of code should work just fine.
#Echo off
:Start
cls
echo Welcome !
echo To Greet press one.
echo For Goodbye press two.
echo To Abort press 3
ECHO.
ECHO.
SET /p Option=Choice:
if "%Option%"=="1" GOTO Sub_MenuA
if "%Option%"=="2" GOTO Sub_MenuB
if "%Option%"=="3" GOTO Sub_MenuC
if "%Option%"=="quit" GOTO EOF
Goto Start
:Sub_MenuA
echo Hi There!
pause
Goto Start
:Sub_MenuB
echo tatas !
pause
Goto Start
:Sub_MenuC
echo Aborted
Pause
Goto Start
:EOF

Resources