how to give a .bat code line a time limit? - batch-file

I want to put a 30s time limit for the :choice Y/N/P and after the time is up goto :start
The code I have need help for the timeing thing
#echo off
:start
echo AmishCraft will start
TIMEOUT /T 5
echo (%time%)
java -Xms2048M -Xmx4096M -jar server.jar
call C:\Program Files (x86)\Common Files\Oracle\Java\javapath\java.exe
ping 1.1.1.1 -n 1 -w 3000 >nul
:choice
set /P a=do you want to restart? Yes No Pause [Y/N/P]?
if /I "%a%" EQU "Y" goto :restart
if /I "%a%" EQU "N" goto :stop
if /I "%a%" EQU "P" goto :pause
goto :start
:restart
cls
echo server will restart
cls
goto :start
:stop
cls
echo closing server
TIMEOUT /T 5
exit
cls
echo server is paused
:pause
:choice
set /P a=do you want start? Restart Stop [R/S]?
if /I "%a%" EQU "R" goto :restart
if /I "%a%" EQU "S" goto :stop
goto :start
pause

/T is the timeout switch for choice.
/D is the switch to define the default errorlevel / option to set if the
time Elapses.
Example:
CHOICE /T 5 /N /C 1234 /M "Select Option 1,2,3 or 4" /D 1
Applies a timeout of 5 seconds, with the errorlevel being set to option 1, equal to errorlevel 1 in this instance.
/N Hides the default Choice Prompt String.
/M Allows you to Define your own Prompt string
/C Allows alphanumerical characters to be defined as Choice options
Note:
Errorlevel is Set from Left to Right with regards to listed options.
After the Choice Command Errorlevel Needs to be Assessed From Highest to lowest
OR
Used Directly; Such as in a Goto :LabelName%errorlevel% Command
* Response to comment *
CHOICE /C 123 /T Timeout 25 /D goto :start /M 1 choice menu 25s
IF %ERRORLEVEL% EQU 1 goto :choice1
There are multiple errors in the above.
/T Timeout 25 should be: /T 25
Timeout is implicit in the /T switch and does NOT form a part of correct usage of the choice command.
/D goto :start should be: /D 1 OR /D 2 OR /D 3
Only the defined /C options should be used following the /D switch
/M 1 choice menu 25s is incorrect.
The prompt after /M should be encased in Doublequotes: "[1] Option 1. [2] Option 2. [3] Option 3."
Errorlevel Assessment should be done on the Line After the CHOICE Command.
Again, to be clear, Assesment should be done from Highest to Lowest. When errorlevel is Assessed following Choice it is actually interpreted as If ERRORLEVEL GTR n , Despite being scripted Using If ERRORLEVEL n
An example of the Correct usage of all of the above:
#echo off
:menu
cls
CHOICE /N /T 25 /C 123 /M "[1] Option 1. [2] Option 2. [3] Start." /D 3
IF ERRORLEVEL 3 (
GOTO :start
) else (
GOTO :choice%errorlevel%
)
:start
ECHO( You are at the start
Pause
GOTO :menu
:choice1
ECHO( You are at option 1
Pause
GOTO :menu
:choice2
ECHO( You are at option 2
Pause
GOTO :menu

Related

Y or N statements in Batch files

I am a dabbler in batch files so my knowledge is limited to my experiences. What I am trying to do is limit the "Y or N" inputs to just that Y or N. Right now you can put anything in the fields and the code progresses. What I am attempting to do is create a hotspot using a batch file. I have yet to figure out how to "save" the created network but that isn't really an issue.
I have included what I have, the lines being the start and finish, If anyone happens to see anything that can be improved upon or made less bulky feel free to comment.
#echo off
:: BatchGotAdmin
:-------------------------------------
REM --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
pushd "%CD%"
CD /D "%~dp0"
:--------------------------------------
#echo off
:SSID
set /P inputA="Input desired Network SSID:"
echo.
set /P c=Is %inputA% correct? [Y/N]?
echo.
if /I "%c%" EQU "Y" goto :PSWD
if /I "%c%" EQU "N" goto :SSID
:PSWD
set /P inputB="Input desired 8 to 63 character Network Password:"
echo.
set /P c=Is %inputB% correct? [Y/N]?
echo.
if /I "%c%" EQU "Y" goto :SETUP
if /I "%c%" EQU "N" goto :PSWD
:SETUP
netsh wlan set hostednetwork mode=allow ssid=%inputA% key=%inputB% >NUL
#echo Creating Network...
echo.
timeout /t 5 /nobreak > NUL
#echo Network Created!
echo.
timeout /t 1 /nobreak > NUL
set /P c=Would you like to start your new Network? [Press "Y" to continue/Press "N" to abort]
if /I "%c%" EQU "Y" goto :START
if /I "%c%" EQU "N" goto :BYE
:START
netsh wlan start hostednetwork
timeout /t 5 /nobreak > NUL
#echo Your Network has started!
pause
:BYE
Exit
Instead of using set /p, use the choice command. I, personally, would use:
choice /m Correct?
if %errorlevel% equ 1 goto PSWD
if %errorlevel% equ 2 goto SSID
This will display: Continue? [Y/N]?. If the hit y, it will go to :PSWD. If they hit n, it will go to :SSID.
The help section of the choice command (brought up in Command Prompt by choice /?)
CHOICE [/C choices] [/N] [/CS] [/T timeout /D choice] [/M text]
Description:
This tool allows users to select one item from a list
of choices and returns the index of the selected choice.
Parameter List:
/C choices Specifies the list of choices to be created.
Default list is "YN".
/N Hides the list of choices in the prompt.
The message before the prompt is displayed
and the choices are still enabled.
/CS Enables case-sensitive choices to be selected.
By default, the utility is case-insensitive.
/T timeout The number of seconds to pause before a default
choice is made. Acceptable values are from 0 to
9999. If 0 is specified, there will be no pause
and the default choice is selected.
/D choice Specifies the default choice after nnnn seconds.
Character must be in the set of choices specified
by /C option and must also specify nnnn with /T.
/M text Specifies the message to be displayed before
the prompt. If not specified, the utility
displays only a prompt.
/? Displays this help message.
NOTE:
The ERRORLEVEL environment variable is set to the index of the
key that was selected from the set of choices. The first choice
listed returns a value of 1, the second a value of 2, and so on.
If the user presses a key that is not a valid choice, the tool
sounds a warning beep. If tool detects an error condition,
it returns an ERRORLEVEL value of 255. If the user presses
CTRL+BREAK or CTRL+C, the tool returns an ERRORLEVEL value
of 0. When you use ERRORLEVEL parameters in a batch program, list
them in decreasing order.
Examples:
CHOICE /?
CHOICE /C YNC /M "Press Y for Yes, N for No or C for Cancel."
CHOICE /T 10 /C ync /CS /D y
CHOICE /C ab /M "Select a for option 1 and b for option 2."
CHOICE /C ab /N /M "Select a for option 1 and b for option 2."

Running commands in the background while another is processing?

#echo off
:start
cls
color e
echo YOU HAVE WON $1,000,000! WHAT WILL YOU DO?
echo.
echo =================
echo -Take it (1)
echo -Leave it (2)
echo -Double it (3)
echo =================
echo.
timeout /t 1 >nul 2>&1
color a
timeout /t 1 >nul 2>&1
color b
timeout /t 1 >nul 2>&1
color e
timeout /t 1 >nul 2>&1
color a
timeout /t 1 >nul 2>&1
color b
timeout /t 1 >nul 2>&1
color e
timeout /t 1 >nul 2>&1
color a
timeout /t 1 >nul 2>&1
color b
timeout /t 1 >nul 2>&1
color e
timeout /t 1 >nul 2>&1
color a
timeout /t 1 >nul 2>&1
color b
timeout /t 1 >nul 2>&1
color e
set /p INPUT=Please specify your answer:
If /i "%INPUT%" == "1" goto 1
If /i "%INPUT%" == "2" goto 2
If /i "%INPUT%" == "3" goto 3
I have this code above, and there is a really annoying thing that I can't figure out. So you can see I am repeatedly changing the colors, but you can see it gets in the way of the next command. Is there a way I can make it like in the background until the answer has been chosen? (1, 2, or 3).
Using start /B with an aux script does the trick.
The aux script exits when it finds that a temporary file exists. Lame but works.
my mockup main routine (call it main.bat)
#echo off
:start
cls
color e
echo YOU HAVE WON $1,000,000! WHAT WILL YOU DO?
echo.
echo =================
echo -Take it (1)
echo -Leave it (2)
echo -Double it (3)
echo =================
echo.
start /B %~dp0\color_cycling.bat
:err
set /p INPUT=Please specify your answer:
If /i "%INPUT%" == "1" goto 1
If /i "%INPUT%" == "2" goto 2
If /i "%INPUT%" == "3" goto 3
goto err
:1
echo.> %TEMP%\stopcol
echo take it
set /p SURE=are you sure?
pause
:2
echo.> %TEMP%\stopcol
echo leave it
pause
My color_cycling.bat routine
#echo off
del %TEMP%\stopcol 2>NUL >NUL
:lab
for %%i in (a b e) do (
timeout /t 1 >nul 2>&1
color %%i
if exist %TEMP%\stopcol exit
)
goto lab
Nice effect from year 1977 indeed!

Automatically make a selection after 20 seconds

I made a custom start script for my Minecraft server so i could use more RAM.
When you stop the server, it asks if you want to restart or if you want to stop.
I can't get it to restart it automatically if you don't answer after 20sec
(in case of a crash). Please help.
#echo off
title minecraft-server-1.8.3
color 0A
prompt [server]:
cls
:start //starts the server
echo loading server...
java -Xms3G -Xmx3G -jar minecraft_server.1.8.3.jar nogui
cls
[code to restart the server when it didn't get an anwser in 20sec]
:choice //what to do when
set /P a=do you want to restart[Y/N]? the server stops
if /I "%a%" EQU "Y" goto :restart
if /I "%a%" EQU "N" goto :stop
goto :choice
:restart
cls
echo server will restart
TIMEOUT /T 5
cls
goto :start
:stop
cls
echo closing server
TIMEOUT /T 5
exit
Instead of set /p use choice:
choice /c YN /t 20 /d Y /m "Do you want to restart "
/c YN states the answer set (Y or N, case insensitive by the way).
/t 20 states a timeout of 20 seconds.
/d Y states the default answer Y once the timeout expires.
/m "..." states a prompt.
choice sets errorlevel to the index of the input (Y:1, N:2) so to check the user input use:
if %errorlevel% equ 1 goto :restart
if %errorlevel% equ 2 goto :stop
You can find more information about choice at http://ss64.com/nt/choice.html or type choice/?

Using CHOICE with CMD script

Having trouble getting this CHOICE script to work. Can anyone provide any insight?
#echo off
CHOICE /C:IRCQSH /T 10 /N /M "Waiting with choice..." /D H
IF ERRORLEVEL 0 ECHO "Default choice: Health"
IF ERRORLEVEL 1 ECHO "Install"
IF ERRORLEVEL 2 ECHO "Remove"
IF ERRORLEVEL 3 ECHO "Console"
IF ERRORLEVEL 4 ECHO "Quit"
IF ERRORLEVEL 5 ECHO "Start"
IF ERRORLEVEL 6 ECHO "Health"
pause
You need to change your syntax to treat ERRORLEVEL as a variable, and use the CMD equality statements, such as:
IF %ERRORLEVEL% EQU 0 ECHO "Default choice: Health"
IF %ERRORLEVEL% EQU 1 ECHO "Install"
IF %ERRORLEVEL% EQU 2 ECHO "Remove"
IF %ERRORLEVEL% EQU 3 ECHO "Console"
IF %ERRORLEVEL% EQU 4 ECHO "Quit"
IF %ERRORLEVEL% EQU 5 ECHO "Start"
IF %ERRORLEVEL% EQU 6 ECHO "Health"
The reason your code is failing is, taken from here:
IF ERRORLEVEL n statements should be read as IF Errorlevel >= number
i.e.
IF ERRORLEVEL 0 will return TRUE when the errorlevel is 64
A couple points here:
The default choice does NOT return an ERRORLEVEL of zero, but the number of the choice selected. In your case, that is H, the default is equal to press H with an ERRORLEVEL of 6
The right way to take the value of ERRORLEVEL is enclosing it in percents and use the EQU comparison, as LittleBobbyTables said in his answer. However, there are other ways to achieve the same result.
The IF ERRORLEVEL Number Command test if the errorlevel value is Greater or Equal than the given number, so you may also use this form:
.
#echo off
CHOICE /C:IRCQSH /T 10 /N /M "Waiting with choice..." /D H
FOR %%E IN (6 5 4 3 2 1) DO IF ERRORLEVEL %%E GOTO LABEL-%%E
:LABEL-1
ECHO "Install"
GOTO CONTINUE
:LABEL-2
ECHO "Remove"
GOTO CONTINUE
:LABEL-3
ECHO "Console"
GOTO CONTINUE
:LABEL-4
ECHO "Quit"
GOTO CONTINUE
:LABEL-5
ECHO "Start"
GOTO CONTINUE
:LABEL-6
ECHO "Health"
:CONTINUE
pause
Perhaps the simplest way to achieve the same thing is defining an array and show the appropriate element using the errorlevel value as index:
.
#echo off
setlocal EnableDelayedExpansion
rem Create an array with the desired messages (selected by numeric index)
set index=0
for %%a in ("Install" "Remove" "Console" "Quit" "Start" "Health") do (
set /A index+=1
set elem[!index!]=%%a
)
CHOICE /C:IRCQSH /T 10 /N /M "Waiting with choice..." /D H
echo !elem[%ERRORLEVEL%]!
pause
For a further description of Batch arrays, see: Arrays, linked lists and other data structures in cmd.exe (batch) script

DOS: find a string, if found then run another script

I want to find a string in a file using DOS:
For example
find "string" status.txt
And when it is found, I want to run a batch file.
What is the best way to do this?
It's been awhile since I've done anything with batch files but I think that the following works:
find /c "string" file
if %errorlevel% equ 1 goto notfound
echo found
goto done
:notfound
echo notfound
goto done
:done
This is really a proof of concept; clean up as it suits your needs. The key is that find returns an errorlevel of 1 if string is not in file. We branch to notfound in this case otherwise we handle the found case.
C:\test>find /c "string" file | find ": 0" 1>nul && echo "execute command here"
#echo off
cls
MD %homedrive%\TEMPBBDVD\
CLS
TIMEOUT /T 1 >NUL
CLS
systeminfo >%homedrive%\TEMPBBDVD\info.txt
cls
timeout /t 3 >nul
cls
find "x64-based PC" %homedrive%\TEMPBBDVD\info.txt >nul
if %errorlevel% equ 1 goto 32bitsok
goto 64bitsok
cls
:commandlineerror
cls
echo error, command failed or you not are using windows OS.
pause >nul
cls
exit
:64bitsok
cls
echo done, system of 64 bits
pause >nul
cls
del /q /f %homedrive%\TEMPBBDVD\info.txt >nul
cls
timeout /t 1 >nul
cls
RD %homedrive%\TEMPBBDVD\ >nul
cls
exit
:32bitsok
cls
echo done, system of 32 bits
pause >nul
cls
del /q /f %homedrive%\TEMPBBDVD\info.txt >nul
cls
timeout /t 1 >nul
cls
RD %homedrive%\TEMPBBDVD\ >nul
cls
exit
We have two commands, first is "condition_command", second is "result_command".
If we need run "result_command" when "condition_command" is successful (errorlevel=0):
condition_command && result_command
If we need run "result_command" when "condition_command" is fail:
condition_command || result_command
Therefore for run "some_command" in case when we have "string" in the file "status.txt":
find "string" status.txt 1>nul && some_command
in case when we have not "string" in the file "status.txt":
find "string" status.txt 1>nul || some_command
As the answer is marked correct then it's a Windows Dos prompt script and this will work too:
find "string" status.txt >nul && call "my batch file.bat"
That's more straightforward:
find /c "string" file
if %errorlevel% not equ 1 goto something

Resources