How can I run this on one line?
#echo off
SET /P fp="Read pages "
SET /P lp=" to "
pause
What I would like to get: Read pages 1 to 5
Your question is a little unclear but this provides the sort of thing you are asking:
#echo off
set start=1
set end=5
SET /P "fp=Read pages %start% to %end%: "
pause
Batch isn't designed to get two sets of input on the same line, and print text between them. You could do it by getting input on the top line and using a CLS and printing the second input line immediately afterward. Try this:
#echo off
cls
set /P "fp=Read pages "
cls
set /P "lp=Read pages %fp% to "
echo %fp% %lp%
pause
Related
I'm new to batch and trying to format the way my file runs, so the user input following the line below is not in the same line with the "What's your name?" part, if that makes sense.
set /p "Playername=What's your name?"
I tried using:
/f "eol=\" tokens=3 delims=:{ " %%A in (SType_txt.sys) do echo %%A
(copy-pasted from somewhere online without fully understanding the contents) and using " \ " as a EOL as shown below,
set /p "Playername=What's your name?" \
but that just doesn't work, no error-message or anything, just not doing what I wanted.
What can I do to stop the input being positioned right after the question but in the line below?
The prompt portion of a set /p command is optional, so you can simply
echo What's your name?
set /p "Playername="
and the user will enter their response on the line underneath of the displayed text.
Alternatively create a variable containing a new line character:
#Echo Off
SetLocal EnableExtensions EnableDelayedExpansion
(Set \n=^
% 0x0A %
)
:GetName
Set "PlayerName="
Set /P "PlayerName=What is your name?!\n!>" || GoTo GetName
Okay, so I'm trying to automate an ARM based CPU stress test program (it runs through the command prompt, and requires quite a bit of user input). What I'd like to do, is use for /f to watch the output, and run a few different sendkeys scripts when it see's their respective prompt strings. I've tried making two very barebones batch files to test this out:
The first is a simple batch file that asks for 3 separate inputs
#echo off
REM This is a file that asks for inputs
set /p q1="Please press 1: "
echo.
set /p q2="Please press 2: "
echo.
set /p q3="Please press 3: "
echo.
echo All buttons have been pressed,
echo.
echo button 1 was: %q1%
echo button 2 was: %q2%
echo button 3 was: %q3%
echo.
set /p foo="Press Enter to finish..."
The second is a batch file that runs the first (^) and looks for "Please Press 1: " in the output
#echo off
echo We will now launch the input command
echo.
timeout .5
echo in 5...
timeout 1
echo 4...
timeout 1
echo 3...
timeout 1
echo 2...
timeout 1
echo 1...
timeout 1
echo Launching...
for /f "delims= " %%i in ('Input.bat ^| find /i "Please press 1:"') do (
echo we did it
)
echo Did you make the right decisions?
set /p foo=
What I get as a result of this is a blank command prompt right after the "Launching..." Echo. If I press Enter four times, it comes back with the "we did it" echo along with the "Did you make the right decisions?" echo. So, finally on to the meat of my question. Is there any way to keep for /f from redirecting the stdout, as well as, is there any way to get for /f () do () to happen while the command is running?
So based on your request, sounds like you're trying to read strings from your batch1 script from a new batch2 script. To do this you will have to export your variables to a text document. From there, we can read the text document and gather the variables. If I am completely wrong on your request (It's a little hard to understand your request) then my oligopolies, hope those few tips can help you out at least.
To export files you need to use >>
For example: Echo This will be line one! >> Yourfile.txt
As another note, when you are done with the script, use goto :eof to exit.
Here is your 1st batch file:
#ECHO OFF
#DEL /Q %~dp0\strings.txt
REM This batch file asks for inputs
set /p q1="Please press 1: "
echo %q1% >> strings.txt
echo.
set /p q2="Please press 2: "
echo %q2% >> strings.txt
echo.
set /p q3="Please press 3: "
echo %q3% >> strings.txt
echo.
echo All buttons have been pressed,
echo.
echo button 1 was: %q1%
echo button 2 was: %q2%
echo button 3 was: %q3%
echo.
set /p foo="Press Enter to finish..."
goto :eof
Here is your 2nd batch file:
#ECHO OFF
echo We will now launch the input command.
echo.
echo in 5...
PING localhost -n 2 >nul
echo 4...
PING localhost -n 2 >nul
echo 3...
PING localhost -n 2 >nul
echo 2...
PING localhost -n 2 >nul
echo 1...
PING localhost -n 2 >nul
CLS
echo Launching...
:: Do action for each string. Use %%G to call the variable.
for /f "delims== tokens=*" %%G in (strings.txt) do (
echo Working on string: %%G
)
echo Did you make the right decisions?
pause > nul
DEL /Q %~dp0\strings.txt
goto :eof
Please keep in mind that instead of using timeout 1 you can actually use PING localhost -n 2 >nul so it does not actually freeze the prompt it's self.
I'm trying to create a script (probably VBS?) that runs a start executable command, but with a variable (Number) at the end of it.
So, I want a script that makes a input box to appear, where i can enter a number and it will execute the default command + add the number i write.
The command i use atm:
start G:\testfolder\test.exe 1234
ATM i have a .BAT file that i edit manually in notepad++ when i need a different number, but i want a single file where i can just input the number inn.
Does anybody know how I can do this?:
start G:\testfolder\test.exe "INPUT NUMBER"
You can give a try for this batch file :
#echo off
Call :inputbox "Please enter a number:" "Box Title"
echo You entered %Input%
Start "" G:\testfolder\test.exe %Input%
pause
exit /b
:InputBox
set "input="
set message=%~1
set Title=%~2
echo wscript.echo inputbox(WScript.Arguments(0),WScript.Arguments(1)) >"%temp%\input.vbs"
for /f "tokens=* delims=" %%a in ('cscript //nologo "%temp%\input.vbs" "%message%" "%Title%"') do set "input=%%a"
exit /b
I'm creating a batch file that will launch when I login to my user account. I followed this tutorial to create a batch file with a menu. It works, however, if the user enters a number that is not listed, I want it to go back to the menu. How would I implement that?
Side note: I understand I could use something more flexible like Powershell, however, I prefer batch files.
Here is what I have so far:
#ECHO OFF
CLS
:MENU
echo Welcome %USERNAME%
echo 1 - Start KeePass
echo 2 - Backup
echo 3 - Exit
SET /P M=Type 1,2,3 then press Enter:
IF %M%==1 GOTO StarKeePass
IF %M%==2 GOTO Backup
IF %M%==3 GOTO :EOF
:StarKeePass
SET keePass="%USERPROFILE%\KeePass\KeePass-2.30\KeePass.exe"
SET kdb="%USERPROFILE%\KeePass\PasswordDatabase\PasswordDatabase.kdbx"
echo I'll start KeePass for You
START "" %keePass% %kdb%
GOTO MENU
:Backup
SET backup="%USERPROFILE%\backup.bat"
call %backup%
GOTO MENU
To build a menu using set /P, I recommend the following changes:
after all the if queries, put a goto :MENU to catch unintended entries;
reset the selection variable by something like set "SELECT=" before the set /P command, because set /P keep the former value of the variable in case the user presses just ENTER;
put quotes "" around the expressions in the if statements to avoid syntax errors in case the variable is empty or it contains some characters that have special meanings (^ & ( ) < > |; the " character still causes problems though);
use the quoted syntax set [/P] "SELECT=anything" to avoid trouble with special characters;
in case letters are queried, add the /I switch to if to force case-insensitive comparison;
if you wish, you could put a cls command after the :MENU label to let the screen be built up every time you return to the menu;
Here is an example that demonstrates what I am talking about:
#echo off
:MENU
cls
echo --- MAIN MENU ---
echo 1 - do something
echo 2 - do something else
echo Q - quit
set "SELECT="
set /P "SELECT=Type 1, 2, or Q <Enter>: "
if "%SELECT%"=="1" GOTO :SOMEWHERE
if "%SELECT%"=="2" GOTO :SOMEWHERE_ELSE
if /I "%SELECT%"=="Q" GOTO :EOF
goto :MENU
:SOMEWHERE
rem do something here...
echo.
echo Print some text.
pause
goto :MENU
:SOMEWHERE_ELSE
rem do something else here...
echo.
echo Print some other text.
pause
goto :MENU
To avoid the above mentioned troubles with the " character, modify the set /P block as follows:
set "SELECT=""
set /P "SELECT=Type 1, 2, or Q <Enter>: "
set "SELECT=%SELECT:"=%"
if "%SELECT%"=="1" GOTO :SOMEWHERE
if "%SELECT%"=="2" GOTO :SOMEWHERE_ELSE
if /I "%SELECT%"=="Q" GOTO :EOF
goto :MENU
I am trying to make a Shutdown dialogue in Batch and I have run into a slight problem. I don't like how Windows 8 asks you for the time in seconds when you would like to remote shutdown your own computer with a timer and I am trying to make a batch file that converts a given number (minutes) into seconds.
I have searched the vast majority of the interwebs and cannot find a way to multiply two whole numbers in a batch file.
Here is what I have so far:
#echo off
echo Enter a number:
set /p %num1%=
echo Enter another:
set /p %num2%=
set /a sum1=%num1%*%num2%
echo The total is %sum1%
pause
Could some kind soul please tell me where I have gone wrong?
Thanks
Charlie B
fix to
#echo off
echo Enter a number:
set /p num1=
echo Enter another:
set /p num2=
set /a sum1="num1 * num2"
echo The total is %sum1%
pause
This will do what you want:
#echo off
Echo Time to Shutdown:
set /p "min=Time(Min): "
set /a sec=min*60
shutdown /t %sec%
That doesn't handle invalid input, but for your program that won't be a problem. (If you want it to error handle comment so).
Mona
You don't have to put the "%" in the declaration of your variables
#echo off
echo Enter a number:
set /p num1=
echo Enter another:
set /p num2=
set /a sum1=%num1%*%num2%
echo The total is %sum1%
pause