Validating user input in batch - batch-file

I've got a simple batch script:
#echo off
set cash=500
:a
set name=
set /p input=Enter your name:
echo %name%
goto :a
I know batch input is prone to exploitation,and I've read a lot of articles on that. I can stop the user from just typing input&&set cash=100000 but how do i stop the batch file from closing if the user enters the | character?

The problem is the echo command, not the input routine.
Double quotes fixes that - there are other ways.
#echo off
set cash=500
:a
set name=
set /p name=Enter your name:
echo "%name%"
goto :a

Related

SET /p crashing batch file process

In a batch file I'm getting information from the user for later database work.
I first ask if the user is using Windows Auth and then depending on the answer, I ask for username & password, or set a variable and continue on.
But for the life of me, I can't figure out why one of the SET /p lines is causing the batch file to crash, when another SET /p line isn't.
Here's my code:
#ECHO OFF
#SETLOCAL ENABLEDELAYEDEXPANSION
CHOICE /M "Will you be using Windows Authentication to connect to the database?"
ECHO ERRORLEVEL: %ERRORLEVEL%
#PAUSE
IF ERRORLEVEL 2 (
ECHO ERRORLEVEL should be 2: %ERRORLEVEL%
#PAUSE
SET _USINGWINAUTH=FALSE
SET /p _USERNAME=User name:
REM SET /p _PASSWORD=Password (NOTE: Password will be displayed as you type):
) ELSE (
ECHO ERRORLEVEL should be 1: %ERRORLEVEL%
#PAUSE
SET _USINGWINAUTH=TRUE
)
ECHO.
ECHO _USINGWINAUTH: %_USINGWINAUTH%
ECHO _USERNAME: %_USERNAME%
ECHO _PASSWORD: %_PASSWORD%
#PAUSE
As is, with the second SET /p commented out, regardless if you answer Y or N the script runs fine.
But if the second SET /p line is not commented, regardless of what the answer is, the script instantly closes right after hitting a key at the pause before the ECHO ERRORLEVEL.
I just don't see why that's happening!
As per last time, whilst using a caret works, it would be better if you were to rework the logic.
One example:
#ECHO OFF
CHOICE /M "Will you be using Windows Authentication to connect to the database"
IF "%ERRORLEVEL%"=="1" SET "_USINGWINAUTH=TRUE" & GOTO NEXT
SET "_USINGWINAUTH=FALSE"
SET /P "_USERNAME=User Name: "
SET /P "_PASSWORD=Password (will be displayed as you type): "
:NEXT
ECHO(
SET _USINGWINAUTH
SET _USERNAME 2>NUL
SET _PASSWORD 2>NUL
PAUSE

Test for and remove substring in user input

I want to create a batch file that echoes text that it receives as part of a user-inputted command.
#echo off
Echo execute a command
Set /p command=
if "%command%"=="echo 'some text'" goto echo
::my aim is to make "echo" ignored by System and Only read "'some text'"
:: like "set text_to_echo='some text'
:echo
echo %text_to_echo%
Asking the user for the command first and then asking the user what to echo, like below, is not an option
#echo /p
set /p text=
if "%text%=="echo"
:echo
set /p tex1="Enter text to echo"
echo "%tex1%"
You could use this:
#echo off
Echo execute a command
Set /p "command="
if NOT "%command%"=="%command:echo =%" goto echo
pause
:echo
SET "text_to_echo=%command:~5%"
echo.%text_to_echo%
pause
Note that I also fixed your if, and put a pause after your if so you know whether it goes to :echo or not.
You want to split your Input into a first word (token) and the rest. You can do this with a for:
#echo off
Echo execute a command
Set /p "commandstring=# "
for /f "tokens=1,* delims= " %%a in ("%commandstring%") do (
set "command=%%a"
set "params=%%b"
)
if /i "%command%"=="echo" goto :_echo
...
:_echo
echo %params%
REM also possible (at least for "echo", but I guess, that's just an example):
%command% %params%

Batch File Game Accounts

I want to make a batch adventure game (not text-based adventure) that gives you options and lets you choose to progress the story.
Problem is I don't know how to save or load progress in the game and I want to make (possibly) multiple accounts you can access to start where you left off.
I know it's possible, since I've seen it in a batch game (who's file I lost). So, anyone know to make it possible?
#echo off
:menu
cls
echo 1.Sign in
echo 2.Register
echo 3.Exit
echo.
set /p input=What would you like to do:
if %input%==1 goto log
if %input%==2 goto reg
if %input%==3 goto exit
goto menu
:reg
cls
set /p user=Enter your desired username:
set /p pass="Enter your desired password:
echo %pass% >> %user%.txt
:: This will prompt for a username and password and then
:: output the user variable as the name of a .txt file that contains the
:: password
goto menu2
:log
set /p user="Enter your username: "
set /p pass="Enter your password: "
set /p password=<%user%.txt
pause >nul
:: This will check if the password entered is equal to the password in
:: the .txt file
if %pass% equ %password% goto menu2
goto menu
:menu2
::Enter your script here
The system is much shorter when you get rid of the comments. :)
Later on in your game you could use the same system as used in the :reg label to create a batch file with player stats and maybe an inventory in it such as set /a armor=2 and set /a sword=1 then call on that file when you want to show the player their statistics/inventory. Just an idea... Anyway, hope this helps at least a little bit.
Im making a game and this is the login signup this is part of the code
#echo off
color f
:logsign
title whattodo?
cls
echo What will you do?
echo.
echo 1) Login
echo 2) Sign in
echo 3) Exit
echo.
set /p web=Type 1 or 2?
if "%web%"=="1" goto login
if "%web%"=="2" goto signup
if "%web%"=="3" exit
:notvalid
echo you're username is not valid.
echo.
echo please try again or sign up.
pause
cls
goto logsign
:notvalid1
echo you're password is not valid.
echo.
echo please try again or sign up.
pause
cls
goto logsign
:signup
title Sign Up
cls
echo What will be your username?
echo.
set /p username=Username:
echo %username% >%username%.bat
cls
echo What will be your password?
echo.
set /p password=Password:
echo %password%>>%username%.bat
cls
echo Go back to log in menu then log in.
pause
cls
goto logsign
:login
title login
cls
echo Let's start with name.What is it?
echo.
set /p username=Type username:
if not exist %username%.bat (goto notvalid)
pause
cls
echo Now what is your password?
echo.
set /p password=Type password:
if not exist %username%.bat (goto notvalid1)
pause
goto home
The sign up saves the user and pass in a .bat file
possibly you could save stats to a file named after the player profile and read off of that file to see what level they where on look around over here.How to read file contents into a variable in a batch file?

How to detect invalid user input in a Batch File?

I want to use a batch file to ask for a password to continue, i have very simple code that works.
#echo off
:Begin
cls
echo.
echo Enter Password
set /p pass=
if %pass%==Password goto Start
:Start
cls
echo What would you like me to do? (Date/Chrome/Lock/Shutdown/Close)
set /p task=
if %task%==Date goto Task=Date
if %task%==Chrome goto Task=Chrome
if %task%==Lock goto Task=Lock
if %task%==Shutdown goto Task=Shutdown
if %task%==Close goto Task=Close
I need to detect when the user entered an invalid password, i have spent an hour researching but i found nothing. I'm not advanced in any way so try and keep it very simple like the code above.
Please help me.
You were missing exactly one line of code!
#echo off
cls
:Begin
echo.
echo Enter Password
set /p pass=
if %pass%==Password goto Start
cls
Echo %pass% is not the PASSWORD
goto :Begin
:Start
cls
echo What would you like me to do? (Date/Chrome/Lock/Shutdown/Close)
set /p task=
if %task%==Date goto Task=Date
if %task%==Chrome goto Task=Chrome
if %task%==Lock goto Task=Lock
if %task%==Shutdown goto Task=Shutdown
if %task%==Close goto Task=Close
If the password is invalid the code will simply continue to the next line which now is goto :Begin. This will restart the sequence. I changed the order around a bit so that the screen was cleared as well and the error was printed.
Similar to Monacraft's answer, but using if-else statements:
#echo off
cls
:Begin
echo.
echo Enter Password
set /p pass=
if %pass%==Password (goto Start) else *echo Invalid password. && goto :Begin)
:Start
cls
echo What would you like me to do? (Date/Chrome/Lock/Shutdown/Close)
set /p task=
if %task%==Date goto Task=Date
if %task%==Chrome goto Task=Chrome
if %task%==Lock goto Task=Lock
if %task%==Shutdown goto Task=Shutdown
if %task%==Close goto Task=Close
What this does is it checks for the correct password. If it isn't the correct password, it will display an error message and go back to :Begin, which restarts the sequence.

Programming set /p input

I have two scripts. First script is asking user for some input and calling another script giving it the input as a parameter. Second script is again asking user for the same input.
first.bat
set /p input=Insert your input:
call second.bat %input%
second.bat
if %1 == "Y" input=%1 & goto skipInput
set /p input=Insert the same input:
:skipInput
echo Skipped user input
Is it possible to set second user input with first input value without user pressing the same input value? Problem is that set /p in second.bat cannot be skipped like in upper example.
Updated solution:
first.bat
set /p input=Insert your input:
echo %input% | (cd path/to/file & second.bat)
echo %input% | (cd path/to/file & second.bat)
pause >nul
I'm not 100% sure what you are trying to do, but maybe this
First.bat
set /p input=Insert your input:
call second.bat %input%
Second.bat
if %1=="Y" set input=%1
Which will set the variable input in the second script to the value of input in the first script.
Update
If you only have access to the initial batch that calls the others then try this
#echo off
set /p input=Insert your input:
echo %input%| second.bat
pause >nul
After running that I managed to get the prompt in the second batch which just has the set /p line to be filled in using the pipe redirection.
:first.bat
cls
set /p input=Enter input.
if "%input%" equ "" goto first.bat
call seccond.bat %input%
What happens here is if the user's input is equal to "NUL" go back to the beginning.
:seccond.bat
cls
set input=%*
if /i "%input%" equ "Y" do command
Put your code below for SECCOND.bat

Resources