ask set /p variable on a different line - batch-file

I've seen in batch code that you can ask for user input on a separate line or continue while asking or something. For example it would look something like this;
enter your name
name:_(input would be here)
enter name above
And the code might look like;
echo Enter your name
set /p /(continue) name=name:_
echo enter name above
Or maybe:
echo enter your name
(line 2)
echo enter your name above
set /p /line:2 name=name:_

#echo off
setlocal
cls
echo enter your name
echo name:
echo enter name above
rem Move cursor to one line below screen size,
rem you must adjust this value to fit your screen.
for /L %%i in (1,1,34) do echo/
rem Move cursor to screen home and hide timeout "Waiting..." message
for /F %%a in ('timeout /T 1 ^> CON') do rem
set /P "name=name: "
echo/
echo/
echo/
echo Name read: "%name%"
Further details at this post

Related

I want to have multiple echo and echo. below a pause and below a set /p variable= befor pressing any key in a batch file

I want the line of code below to work or something that will do the same and i want the echo and echo. to show befor i press any key
set /p variable=
echo.
echo test
echo.
echo test
::ect
and I want the line of code below to work or something that will do the same and i want the echo and echo. to show befor i press any key
pause
echo.
echo test
echo.
echo test
::ect
I tried this (I have not tried it with pause)
#echo off
for /f %%A in ('echo prompt $E^| cmd') do set "esc=%%A"
cls
echo *other text*%ESC%[2A
set /p variable=
echo %ESC%[1B
I was only able to have one echo or one echo. after set /p variable=, but I expected to be able to have multiple echo below set /p variable= and multiple echo. below set /p variable=
If i try to have more then one line of text then it takes the line of text above set /p variable= and moves it down below set /p variable= and combines it with the text i want below set /p variable=
So i want the set /p variable= and pause to pause after all the echo and echo.
I want it to look like for example this when i open the .bat file
Type the number of your choosing:
*other text*
Or this
Press any key to continue . . .
*other text*
sorry for posting the same thing so many times i promis that this is the last time
You are using ANSI escape codes. Just use them at the right place and with the proper parameters (numbers):
#echo off
for /f %%A in ('echo prompt $E^| cmd') do set "esc=%%A"
cls
echo/
echo/
echo *other text*
echo *additional text*
echo *some more text*
echo %ESC%[5A
set /p "variable=Enter var: "
echo %ESC%[2B
echo Thank you for your input.
Output on screen (where Hello is the manual input:
Enter var: Hello
*other text*
*additional text*
*some more text*
Thank you for your input.
You need enough space "above" set /p. It doesn't work at the very first line (after CLS) because you can't move the cursor to "line zero", so you need (at least) two (empty) lines (echo/).

How to display text beneath "set /p variable="

In this senario,
echo Write your name below.
echo.
set /p name="Enter here: "
echo.
echo *other text*
is there a way where the set /p name="Enter name: " doesn't pause the screen and also displays the text below.
I figured that since its a top down language it probably cannot, but there are many people on here with more experience who might no otherwise.
If you're using Windows 10, you can use VT100 escape sequences to move the cursor up and down, printing everything first and then getting user input afterwards.
#echo off
:: Generate an escape character. Normally I'd just use ALT+027,
:: but SO doesn't display those properly.
for /f %%A in ('echo prompt $E^| cmd') do set "esc=%%A"
:: The trick here is to write out everything and then move the cursor
:: back to where it needs to be
echo Write your name below.
echo(
echo(
echo(
echo *other text*%ESC%[2A
set /p "name=Enter here: "
:: When we're done, move the cursor to where it would have been if
:: we had written everything top-down
echo %ESC%[1B
You can store new line character in a variable and print it out
#echo off
setlocal EnableDelayedExpansion
(set LF=^
%=empty=%
)
echo Write your name below.
echo.
set /p name="Enter here:!LF!*other text*!LF!"

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.

How do I save a file name to a variable in cmd?

#echo off
title Music
color 0f
:main
cls
echo Welcome to you Music!
echo.
set /p var=What would you like to do:
if %var%==find goto find
if %var%==play goto play
goto main
:find
cls
set /p song=What song would you like me to check for (Case Sensitive):
if exist music\"*%song%*.mp3" goto exist
if not exist music\"*%song%*.mp3" goto notExist
:exist
cls
echo %song% is here.
echo.
pause
goto main
:notExist
cls
echo %song% is not here. Check the spelling and letter case for errors.
echo.
pause
goto main
:play
cls
set /p song=Enter the name of a song to play:
if exist music\"*%song%*.mp3" goto play2
if not exist music\"*%song%*.mp3" goto notExist
goto main
:play2
cls
set songName = [file name]
:playing
:: Will put more code later.
The purpose of this program is to search my music for a specific song and be able to play it from command line. I will later add code to control the music but for now I just want to be able to find the song and play it. I am using the '' so someone could enter "Another Brick in the Wall" and let it find "Another Brick in the Wall Part 2" The issue is when saving a variable, it can not use '' or they will be saved as part of the string.
your question isn't very clear. The following should answer what I guessed from your question and comments:
set /p "song=What song would you like me to check for (Case insensitive): "
REM if you are afraid of qoutes entered by the user, remove them:
set song=%song:"=%
<nul set /p "=Songs found: "
dir /b "music\*%song%*.mp3"|find /c /v ""
echo the names are:
dir /b "music\*%song%*.mp3"
echo executing them...
for /f "delims=" %%i in ('dir /b /s "music\*%song%*.mp3"') do (
echo ... executing %%i
REM whatever you use to play 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%

Resources