SET /p crashing batch file process - batch-file

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

Related

How do I create an update function in Batch?

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:

Set /P not capturing input in IF statement

Everything in this batch script works fine, but when I enter the IF statement, for some reason set /p id= doesn't actually capture anything. In fact shortly after it will echo:
You chose session %id%.
but that will return a blank, as though nothing was entered for ID.
Any advice would be greatly appreciated.
#echo off
echo Please be sure CMD is being run as an administrator.
echo.
:loop
set /p targetpc="Which PC would you like to query for users (Hostname or IP)?: "
echo.
echo Querying %targetpc%...
echo.
quser /server:%targetpc%
echo.
set /p choice="Would you like to log a user off of %targetpc%? [Y/N]: "
echo.
IF /I "%choice%" EQU "Y" (
echo Enter user's session ID:
set /p id=
echo.
echo You chose session %id%.
echo.
logoff %id% /server:%targetpc% /V
echo.
echo Done!
echo.
goto loop
)
IF /I "%choice%" EQU "N" (
goto loop
)
You are using the %id% value within the same block where it is set. This being the case, you need to use delayed expansion.
Add these lines to the top and bottom of your script, respectively:
SETLOCAL EnableDelayedExpansion
<Your script>
ENDLOCAL
Now use delayed expansion notation with your block (! around variables instead of %):
IF /I "%choice%" EQU "Y" (
echo Enter user's session ID:
set /p id=
echo.
echo You chose session !id!.
echo.
logoff !id! /server:%targetpc% /V
echo.
REM Note, replacing with a period here.
echo Done.
echo.
goto loop
)
There are tons of other questions on this site regarding delayed expansion, so a quick search within the batch-file tag should yield lots of additional info if you need it.

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%

Kick command for batch chat program

i am working on a batch chat program... because it's fun.. but i have run into a problem.
I want the users to be able to chat, or use commands from the same prompt and this method works:
set /p m=Message:
set tm="%m%"
if %tm% == "KICK" goto kick
echo %time% ^<%u%^>: %m% >> %log%
However to make the command work i need to have another step somewhere else in the batch file:
:kick
set /p person=Who to kick?
del %dir%\%person%
How can i make it so that someone can just type "KICK John" to kick someone?
set /p m=Message:
set tm=%m%
if /i "%tm:~0,4%"=="KICK" goto kick
echo %time% ^<%u%^>: %m% >> %log%
:kick
set person=%tm:~5%
if not defined person set /p person=Who to kick?
if not defined person goto kick
del %dir%\%person%
Naturally, the destination label for the final if not defined is debatable.
Fix : remove quotes surrounding %m%
Additional 20130924T0736Z
Here's my test batch:
:: #ECHO OFF
SETLOCAL
SET u=whatever
SET log=u:\log.log
set /p m=Message:
set tm=%m%
if /i "%tm:~0,4%"=="KICK" goto kick
echo %time% ^<%u%^>: %m% >> %log%
TYPE %log%
GOTO :eof
:kick
set person=%tm:~5%
if not defined person set /p person=Who to kick?
if not defined person goto kick
ECHO del %dir%\%person%
GOTO :EOF
And run-report:
>SETLOCAL
>SET u=whatever
>SET log=u:\log.log
>set /p m=Message:
Message:
>set tm=Hello Friend :P
>if /I "Hell" == "KICK" goto kick
>echo 15:33:12.77 <whatever>: Hello Friend :P 1>>u:\log.log
>TYPE u:\log.log
15:33:12.77 <whatever>: Hello Friend :P
>GOTO :eof
So - given that I've no idea of what your setting for u or log or dir happens to be, and that it works for me as shown above, I can only conclude that the quotes ARE required somewhere in the rest of your code - which you haven't shown.
There's no ban on using your quotes - once the presence of "kick" is determined, so
set tm="%m%"
directly after the IF /i... line should fix your problem.
Well, you could try this:
Setlocal Enableextensions
set /p m=Message:
set tm="%m%"
if /i %m:~0,4% == "KICK" call kick "%m:~4%"
echo %time% ^<%u%^>: %m% >> %log%
:kick
del %dir%\%1
goto :eof
Im not too familiar with this sort of Batch scripting (I'd normally use C#), but that should work fine. Tell me if it doesn't work, along with the errormessage. There are other ways of doing it as well.
Mona

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