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
Related
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
I would like to create an update function that works just after the user is prompted to type into the "ChatBox" so that other users on my school network can type and all other users can see it without having to restart the program or typing space to reload the .txt file
here is the code I have written so far;
:enter
cls
type cblog.txt
echo.
set /p text=
echo %text% >> cblog.txt
goto enter
I like this topic, so I wrote a fully working prototype:
#echo off
setlocal
if "%~1" equ "" echo You must give your username as parameter & goto :EOF
set "user=%~1"
set "now=%time%"
echo %now%: User %user% entered the chat room>> msgQueue.txt
call :chatRoom 3< msgQueue.txt
goto :EOF
:chatRoom
rem Omit previous messages
:omitMsg
set /P "msg=" <&3
if "%msg:~0,12%" neq "%now%:" goto omitMsg
echo %msg%
echo/
echo Press S to send a message or eXit to end
echo/
:msgLoop
rem Check for new messages received
:showMsg
set "msg="
set /P "msg=" <&3
if not defined msg goto send
echo %msg%
goto :showMsg
rem Check to send a new message
:send
ver > NUL
choice /C SNX /N /T 3 /D N > NUL
if errorlevel 3 goto end
if errorlevel 2 goto msgLoop
rem Send a message
echo -----------------------------------
set /P "msg=Message: "
echo -----------------------------------
echo %user%: %msg% >> msgQueue.txt
goto msgLoop
:end
echo %time%: User %user% leaved the chat room>> msgQueue.txt
The response time may be adjusted in /T 3 parameter of choice command: shorter times makes the chat more responsive, but it consume more CPU time.
Below is an image that show a test with four users in the Chat Room:
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%
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
so in this code
:chat
cls
findstr /v "sdlkfjsdlkfs98dfu9sd8f6ysd954" \\Cap\FileServer\Recive\chatroom.chatfile
echo.
echo ----------------------------------------------------------
echo.
color 0b
goto chat1
:chat1
ping localhost -n 3 >nul
set /p text=Text:
echo %name% : %text% >>\\Cap\FileServer\Recive\chatroom.chatfile
goto chat
So i was wondering if i can make it not wait for the Text: and go on to refreshing the chat file if there is no input.
You cannot do this with Batch as once you have prompted the user, the execution will wait until input is received.
Perhaps as a workaround, you can "refresh" when empty input is received:
:chat
cls
findstr /v "sdlkfjsdlkfs98dfu9sd8f6ysd954" \\Cap\FileServer\Recive\chatroom.chatfile
echo.
echo ----------------------------------------------------------
echo.
color 0b
goto chat1
:chat1
REM Reset any existing text value.
set "text="
ping localhost -n 3 >nul
set /p text=Text:
REM Check for input.
IF NOT "%text%"=="" (
REM Input was given. Write it to the file.
echo %name% : %text% >>\\Cap\FileServer\Recive\chatroom.chatfile
)
goto chat
So in the above, if the user just presses Enter at the prompt, nothing will be written to the chatfile and the loop will start over.