im messing around for a secret file that i can type in and just keep things to myself here is the code
#echo off
echo PLEASE ENTER THE PASSWORD TO CONTINUE
set /p password="hello"
IF %c%==hello goto top
IF NOT %c%==goto PASSERROR
:passsuccess
title matrix
color 0a
mode 1000
:top
echo %random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%
goto top
:passerror
echo try again
Your problem is that c is undefined. You are entering the password into password then checking c.
In case of spaces or no entry, use
if "%varentered%"=="somevalue" goto ...
For instance,
if "%c%"=="" goto paserror
or
if not defined c goto ...
as you have it, if %c% is not equal to goto, execute the executable passerror
Related
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)
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
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
I have a problem with my code. I am trying to make a "hacker tool" with the tree command. Here is my code:
#echo off
title $userOne ProxyMatrix
color a
echo Hello $userOne,
echo Please enter search function for today's commands:
set /p %commands%=""
:redo
echo Specify Storage Device
set /p %drive%=""
title $userOne ProxyMatrix: Running on %drive% drive at %random% bits per nano
color C
tree %drive% /f
:runagain
color a
echo Run again?
set /p %redo%=""
if %redo%="yes" goto redo
else if %redo%="y" goto redo
else if %redo%="Y" goto redo
else if %redo%="Yes" goto redo
else if %redo%="no" goto end
else if %redo%="No" goto end
else if %redo%="n" goto end
else if %redo%="N" goto end
else echo Thats not a valid answer!
pause
goto runagain
:end
echo Thank you for choosing InGen, inc.
pause
I realize that this won't "hack" anything, its more of a novelty. The problem is, the set /p %redo% and the if/else if statements don't work. They just quit the program. Can someone explain what i'm doing wrong? Thanks.
Syntax is set /p variable=prompt.
Instead of set /p %redo%="" write set /p redo="" or even better set /p "redo="
EDIT
your if syntax is broken too.
Syntax is: if value1==value2 command or if value1==value2 (command1) else (command2)
"Best Practice is to enclose both sides of the comparison with quotes (to avoid syntax errors with empty values or contained spaces):
if "%variable"=="value" echo yes
I would shorten the code to:
set /p %redo%=""
if /i "%redo:~0,1%"=="y" goto redo
if /i "%redo:~0,1%"=="n" goto end
else echo Thats not a valid answer!
/i tells if to ignore capitalization
%variable:~0,1% means "take a substring starting with the first letter (counting starts at 0) with length=1 (so it takes the first letter)
(there is no else needed)
i am new here so i'll try to be as good as i can.
So i am trying to make a RPG based on text-based MS-DOS, and i am going pretty well as i just saw that if the user puts an invalid input at set /p, like an empty answer (just pressing enter) or an answer which is not on the "IF", the batch just crashes, and I would like to fix that so it will be less crashy.
Here is one of the parts i'd like to fix:
#echo off
title "Wasteland Adventure"
color 0A
cls
:Menu
cls
echo.
echo.
echo Welcome to Wasteland Adventure
echo.
echo To start a new game, type NEW and press ENTER.
echo To see instructions for the game, type INSTRUCTIONS and press ENTER.
echo To quit, type QUIT and press ENTER.
set input=
set /p input=What do you want to do?
if %input%==new goto INTRO
if %input%==instructions goto INSTRUCTIONS
if %input%==quit goto EXIT
Thanks in advance
it's not the set /pthat crashes, but:
if %input%==new
if %input% is empty, this is parsed as:
if ==new
obviously a syntax error. To avoid this, use:
if "%input%"=="new"
An empty input will then be parsed as:
if ""=="new"
which works fine.
The same applies when the variable contains only spaces and/or tabs:
if == new (syntax error) versus if " " == "new" (running fine)
Complete code like this:
:Menu
set input=
set /p input=What do you want to do?
if "%input%"=="new" goto INTRO
if "%input%"=="instructions" goto INSTRUCTIONS
if "%input%"=="quit" goto EXIT
REM for any other (invalid) input:
goto :Menu