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
Related
I'm trying to create a batch file that creates a batch file, but as it seems I can't get the first batch file to also send over percentage value's (e.g. %example%). How do I make the first batch file, which creates the second batch file ? But the generated file should contain % ? Also if you find any other errors in my script please correct it :)
Also while I'm here, as you can see I've put an "example, please help!" underneath the ":prep" I can't seem to figure out the ">nul" thing. No matter how many "^" I put it won't save to the last sector (which is "gnome.bat"). It will however save to the "setup.bat" but not any further. Please also help me with that!
The error is under ":Prep"
here's my full script: (not even close to being done with the script...)
#echo off
title Annoying Menu
mode 150
color a
:begin
cls
echo Welcome to "Annoying Menu"
echo This menu is made to prank your friends
echo All pranks that you make on your friends are completely your own responsibility
echo Please be aware that this menu contains what could be consideret "Virus"
echo No harm will however be done to either your, or your freinds pc
echo Please read through all the instructions before use, of the menu...
echo ---------------------------------------------------------------------------------------
echo 1. Please select a password to cancel the prank, by pressing "3" (only numbers)
echo 2. Please make sure that the setup files are ready to be transferred, by pressing "1"
echo 3. Now you are ready to send off the setup file to one of your freinds
echo ---------------------------------------------------------------------------------------
echo type "1" to create setup file
echo type "2" to prepare setup files for use
echo type "3" to download it on your own pc
echo type "4" to select password to cancel the prank (please only use numbers)
echo type "5" to test password
echo type "exit" to close the menu
echo ---------------------------------------------------------------------------------------
echo.
set /p opt= Option:
if %opt%==1 goto create
if %opt%==2 goto prep
if %opt%==3 goto UD
if %opt%==4 goto pass_select
if %opt%==5 goto testpass
if %opt%==exit goto end
cls
echo please enter a valid number to continue...
echo.
pause
goto begin
:create
break>"C:\Users\%Username%\Desktop\setup.bat"
echo.
echo setup file created, please prepare the setup file before use, by pressing "2" in the main menu
echo.
pause
goto begin
:prep
#echo #echo off> setup.bat
#echo title quick_setup>> setup.bat
#echo color a>> setup.bat
#echo mode 150>> setup.bat
#echo :begin>> setup.bat
#echo break^>"C:\Users\%Username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\gnome.bat">> setup.bat
#echo cd C:\Users\%Username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup>> setup.bat
(Example, please help!)
#echo #echo ping localhost -n 2 ^>nul^>^> gnome.bat>> setup.bat)
#echo #echo set /a time=%clock%-1^>^> gnome.bat>> setup.bat
#echo #echo if %clock% EQU 0 goto Timesup^>^> gnome.bat>> setup.bat
#echo #echo :Timesup^>^> gnome.bat>> setup.bat)
goto begin
(Not Done!)
:UD
(Not Done!)
:pass_select
cls
echo Please enter a password that will be used to cancel the prank (please only use numbers)
echo.
set /p pas= Set Password:
set /a pss=%pas%
cls
echo the password has now been set to %pss%
echo to enter a new password simply select "3" again and choose a new password
echo.
pause
goto begin
:testpass
cls
echo we are now ready to test your newly set password (if not you've set a password, please do so first)
echo.
echo type "1" to start the test
echo type "2" to set a password/new password
echo type "exit" to go back to the main menu
echo.
set /p lll= Option:
if %lll%==1 goto test
if %lll%==2 goto pass_select
if %lll%==exit goto begin
cls
echo please enter a valid number to continue...
echo.
pause
goto testpass
:test
cls
echo please enter password to continue...
echo type "exit" to go back to main menu
echo.
set /p tes= Password:
if %tes%==%pss% goto completetest
if %tes%==exit goto begin
cls
echo either you typed the wrong password or it didn't work, please try to set a new password if the same thing happens twice...
echo.
pause
goto test
:completetest
cls
echo The test was successful!
echo you're password works as intended
echo.
pause
goto begin
:end
In order to have percentages in your second script you need to put %%. Then the you will have the % in the output script:
#echo cd C:\Users\%%Username%%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup>>setup.bat
Escaping% with another % is a documented feature as described here in chapter Escape Percent : https://ss64.com/nt/syntax-esc.html
Here's your prep section rewritten as required.
:prep
( Echo #Title quick_setup
Echo #Color A
Echo #Mode 125
Echo :begin
Echo #CD /D "%%AppData%%\Microsoft\Windows\Start Menu\Programs\Startup"
Echo #CD.^>"gnome.bat"
Echo ( #Echo #Timeout 1 /NoBreak^^^>NUL
Echo #Echo #Set /A time=clock-1
Echo #Echo #If %%%%clock%%%% EQU 0 GoTo timesup
Echo #Echo :timesup
Echo ^)^>^>"gnome.bat"
)>>"%UserProfile%\Desktop\setup.bat"
GoTo begin
Please note however, that I have written the content as you had, (other than using Timeout to replace the very hacky Ping command for your 1 second delay and my replacement of Break for CD. in creating the empty file). You will therefore need to account for the fact that your gnome.bat code uses a variable %clock% which has not been pre-defined anywhere in that code, and you need to be very sure that you're wanting to modify the content of the system variable %TIME%.
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.
Following is the batch file that I have created, for any input its only showing is "NO INPUT PROVIDED" string, I tried searching on google and tried many things but non solved my problem.
#echo off
ECHO Please provide input... Valid input is :: Y/y (For changing path) or N/n (For not changing the path).
SETLOCAL
SET /p change=
if "%~1" equ "" GOTO ERROREND
if /I "%change%" equ "Y" GOTO YES
if /I "%change%" equ "N" GOTO NO
:YES
ECHO Y SELECTED.
GOTO SUCCESS
:NO
ECHO N SELECTED.
GOTO SUCCESS
:ERROREND
ECHO Input not recognized.
GOTO FAILURE
:SUCCESS
ECHO Task completed succcess fully.
pause
:FAILURE
ECHO NO INPUT PROVIDED.
pause
Help needed. Thank you for reading this question.
if "%~1" equ "" GOTO ERROREND
means 'if the first parameter to the batchfile is empty', so if you were to run this batch from the prompt as
yourbatchname something
then something is the first parameter.
You probably menat to use "%change%", not `"%~1".
Note: if not defined change is probably a better test.
Note: Simply pressing Enter when prompted by a set /p does not set the variable to empty. It leaves the value unchanged. Best to use
set "var="
set /p var=....
Note: batch simply charges on, instruction by instruction. It has no concept of 'end-of-procedure'. Consequently, if your entry is neither Y nor N (once you've got the %~1 issue resolved) batch will simply continue execution past the if statements to the next - the ECHO Y SELECTED.
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
Is there a way of making it so instead of saying the same echo that you set every time, you can give a list of echos and it chooses a random one to say each time it reaches that echo command?
Yep. Here's a proof of concept.
#echo off
setlocal enabledelayedexpansion
set string[0]=This is the first random line.
set string[1]=This is the second random line.
set string[2]=This is the third random line.
set /a idx=%random% * 3 / 32768
echo !string[%idx%]!
Here's more info on generating random numbers in Windows batch scripting.
#echo OFF
SETLOCAL
SET message0=message zero
SET message1=message one
SET message2=message two
SET message3=message three
SET message4=message four
:: running 10 times
FOR /l %%i IN (1,1,10) DO CALL :showme
GOTO :eof
:showme
SET /a select=%RANDOM% %% 5
CALL SET message=%%message%select%%%
ECHO %message%
GOTO :eof