My goal is to first prompt, echo the line, issue the next prompt, then display press any key to exit....
I'm not sure why my batch-file isn't issuing the second prompt.
#echo off
::deploying to test
set /p tdeploy="Deploy to test: [y/n]"
IF /I "%tdeploy%"=="y"(
call :deploy_test
if /I "%ERRORLEVEL%" NEQ "0"(
echo Deploy test failed
)
)
::deploying to argos
set /p adeploy="Deploy to argos: [y/n]"
IF /I "%adeploy%"=="y"(
call :deploy_argos
if /I "%ERRORLEVEL%" NEQ "0"(
echo Deploy argos failed
)
)
set /p DUMMY=Press any key to exit...
:deploy_test
ECHO deploying test!
goto :eof
:deploy_argos
ECHO deploying argos!
goto :eof
:eof
set /p DUMMY=Press any key to exit.222..
I usually prefer to structure it as a nested IF ELSE to avoid a bunch of GOTO commands and having to figure out error levels:
#echo off
REM deploying to test
set /p tdeploy="Deploy to test: [y/n]"
if /i "%tdeploy%" == "y" (
echo deploying test!
) else (
if /i "%tdeploy%" == "n" (
echo deploy test cancelled
) else (
echo seriously, there were only two options...
)
)
)
REM deploying to argos
set /p adeploy="Deploy to test: [y/n]"
if /i "%adeploy%" == "y" (
echo deploying test!
) else (
if /i "%adeploy%" == "n" (
echo deploy test cancelled
) else (
echo seriously, there were only two options...
)
)
)
pause
The pause will be your "Press any key to continue..." - the rest of the spacing is just to make it more visible.
I would suggest you use choice.exe for your Y,N questions:
#Echo Off
Rem Deploying to test
Choice /M "Deploy to test"
If "%ERRORLEVEL%"=="1" (Call :deploy_test
If ErrorLevel 1 Echo Deploy test failed)
Rem Deploying to argos
Choice /M "Deploy to argos"
If "%ERRORLEVEL%"=="1" (Call :deploy_argos
If ErrorLevel 1 Echo Deploy test failed)
Echo Press any key to exit...
Timeout /T -1 >NUL
GoTo :EOF
:deploy_test
Echo Deploying test!
Timeout /T 3 /NoBreak >NUL
Exit /B 0
:deploy_argos
Echo Deploying argos!
Timeout /T 3 /NoBreak >NUL
Exit /B 1
In the example above, I have used two different exit codes, to simulate the returned error level for each of the two deployment options.
Related
:deleteaccount
cls
echo what account do you want to delete?
set /p dan=
if not exist %~dp0\database\%dan%\ (
echo this account doesn't exist & pause >nul & goto stage
)
else %~dp0\database\%dan%\ (
cls & echo password:
set /p dap=
call %~dp0\database\%dan%\%dan%.bat
if %dap% == %rpassword1% (
echo are you sure you want to delete this account? yes/no
set /p daq=
if %daq% == yes (
#RD /S /Q %~dp0\database\%daq%\
echo account succesfully deleted
pause >nul & goto stage)
if %daq% == no (goto stage)
)
)
After I type the correct password for the account I wanted to delete, it says < is unexpected for some reason.
For the purposes of saving screen real estate, here's an example of your script, modified to use the correct syntax and to include a logical structure.
#Echo Off
:DeleteAccount
ClS
Set "dan=:"
Set /P "dan=Which account do you want to remove? "
If Not Exist "%~dp0database\%dan%\" (
Echo The account does not exist.
"%__AppDir__%timeout.exe" /T 3 /NoBreak 1> NUL
GoTo Stage
)
ClS
If Not Exist "%~dp0database\%dan%\%dan%.bat" (
Echo The required batch file is missing for the account.
"%__AppDir__%timeout.exe" /T 3 /NoBreak 1> NUL
GoTo Stage
)
Set "dap="
Set /P "dap=Please enter your password: "
Call "%~dp0database\%dan%\%dan%.bat" 2> NUL
If ErrorLevel 1 (
Echo A error occurred running the batch file.
"%__AppDir__%timeout.exe" /T 3 /NoBreak 1> NUL
GoTo Stage
)
If "%dap%." == "%rpassword1%." (
"%__AppDir__%choice.exe" /M "Are you sure you want to remove this account"
If Not ErrorLevel 2 (
RD /S /Q "%~dp0database\%daq%" 2> NUL
If ErrorLevel 1 (
Echo An error occurred deleting the account.
) Else Echo Account successfully removed.
"%__AppDir__%timeout.exe" /T 3 /NoBreak 1> NUL
)
) Else (
Echo Incorrect password.
"%__AppDir__%timeout.exe" /T 3 /NoBreak 1> NUL
)
:Stage
Rem Rest of code goes here.
Please read and review it, taking particular notice of the included double-quotes which are used to enclose and protect special characters in strings. The first two and last two lines were included to keep this example independent, (as they were omitted from your posted snippet).
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
I tried to make a check to see if the EULA is set to true in eula.txt but when typing true it asks again and I have to type true again.
I tried on Windows 10 and using a .cmd file instead of .bat (the same thing)
#echo off
SET jarFileName=server.jar
SET jarFileUrl=https://cdn.getbukkit.org/spigot/spigot-1.12.2.jar
SET memory=4G
SET startColor=0F
SET errorColor=CF
rem Colors and things
color %startColor%
MODE CON COLS=136 LINES=36
rem Start server
:checkEULA1
if exist eula.txt (
goto checkEULA2
) else (
echo eula=false>eula.txt
goto checkEULA2
)
:checkEULA2
>nul findstr /c:"eula=true" eula.txt && (
rem EULA true
goto start
) || (
rem EULA not true
echo Do you accept the EULA? Type true if you do.
set /p eula=
echo eula=%eula% >eula.txt
goto checkEULA1
)
:start
pause
if exist %jarFileName% (
java -version >nul 2>&1|| cls&&color %errorColor%&&echo Please install Java and add it to PATH. Usually the 64 bit JDK works better. && goto exit
cls
java -Xmx%memory% -jar %jarFileName% nogui
) else (
cls
echo %jarFileName% not detected. Downloading...
powershell -Command "(New-Object Net.WebClient).DownloadFile('%jarFileurl%', '%jarFileName%')"
echo Done!
echo.
echo Press any key to start server... && pause>nul && cls && goto start
)
rem Exit
:exit
echo.
color %errorColor%
echo.
echo Press any key to exit... && pause>nul && exit
I want it to only ask the question once. No errors I can see.
I think that you could probably do this a little bit simpler:
#Echo Off
Set "startColor=0F"
Set "errorColor=CF"
Set "jarFileName=server.jar"
Set "jarFileUrl=https://cdn.getbukkit.org/spigot/spigot-1.12.2.jar"
Set "memory=4G"
FindStr /I "^eula=true$" "eula.txt" >NUL 2>&1||(Color %errorColor%
Choice /M "Do you accept the EULA"
If ErrorLevel 2 Exit /B 1
(Echo eula=true)>eula.txt)
ClS
Mode 136,36
Color %startColor%
Rem Start server
The problem is that your eula variable is only set after exiting the findstr function.
An easy solution is to create another function and goto it:
:checkEULA1
if exist eula.txt (
goto checkEULA2
) else (
echo eula=false>eula.txt
goto checkEULA2
)
:checkEULA2
findstr /c:"eula=true" "eula.txt" >nul 2>&1 && (
rem EULA true
goto start
) || (
rem EULA not true
set /p eula= Do you accept the EULA? Type true if you do:
goto setEULA
)
:setEULA
echo eula=%eula%>eula.txt
goto checkEULA1
Playing around with my Batch script again. It was brought to my attention that my update routine was not working correctly. Iteration 1 would result in the running script being deleted and not copying the new version from a folder I had selected. Iteration 2 now deletes the old script and copies the new script but with each line of code I change I end up with the currently running script closing, and the new updated script not launching.
Here's a snippet of the Update routine:
:Options
REM I'll set the variable here to what is set elsewhere in the script.
SET VERSION=Version 1.7
CLS
ECHO.
ECHO.
ECHO %VERSION%
ECHO.
ECHO.
ECHO ==================== OPTIONS =====================
ECHO.
ECHO 1. Update Script WIP
ECHO 2. Install entire script to Statup Folder
ECHO 3. Install individual items for auto-launch
ECHO 4. Install Windows Logout feature
ECHO 5. Uninstall
ECHO 6. Change Colors
ECHO 7. Changelog
ECHO.
ECHO =======PRESS 'Q' TO QUIT OR 'B' TO GO BACK========
ECHO.
SET OPTION=
SET /P OPTION=Select an Option:
IF /I '%OPTION%'=='1' GOTO Update
IF /I '%OPTION%'=='2' GOTO Install
IF /I '%OPTION%'=='3' GOTO I_Install
IF /I '%OPTION%'=='4' GOTO I_Logout
IF /I '%OPTION%'=='5' GOTO Uinstall
IF /I '%OPTION%'=='6' GOTO Colors
IF /I '%OPTION%'=='7' GOTO Changelog
IF /I '%OPTION%'=='q' GOTO quit
IF /I '%OPTION%'=='quit' GOTO quit
IF /I '%OPTION%'=='b' GOTO menu
IF /I '%OPTION%'=='back' GOTO menu
ECHO.
ECHO ============INVALID INPUT============
ECHO Please select a number
ECHO or select 'Q' or 'B'.
ECHO ======PRESS ANY KEY TO CONTINUE======
PAUSE > NUL
GOTO Options
:Update
ECHO.
IF EXIST "%appdata%\Microsoft\Windows\Start Menu\Programs\Startup\Shortcut Browser *.bat" (
GOTO Update_1
) ELSE (
ECHO You have not installed the script yet. Please install the script first.
)
PAUSE
GOTO Options
:Update_1
IF EXIST "%userprofile%\AppData\Local\Temp\Update.bat" (
GOTO Update_2
) ELSE (
REM Here the main script writes an update script to a batch file
ECHO DEL "%appdata%\Microsoft\Windows\Start Menu\Programs\Startup\Shortcut Browser *.bat" > "%userprofile%\AppData\Local\Temp\Update.bat"
REM I replaced the original file location with one on the desktop. The original location was on a network drive, location is confidential.
ECHO COPY "%userprofile\Desktop\Shortcut Browser *.bat" "%appdata%\Microsoft\Windows\Start Menu\Programs\Startup" >> "%userprofile%\AppData\Local\Temp\Update.bat"
ECHO CALL "%appdata%\Microsoft\Windows\Start Menu\Programs\Startup\Shortcut Browser *.bat" >> "%userprofile%\AppData\Local\Temp\Update.bat"
ECHO EXIT >> "%userprofile%\AppData\Local\Temp\Update.bat"
)
GOTO Update_2
REM Here the main script calls upon the update script
:Update_2
CALL "%userprofile%\AppData\Local\Temp\Update.bat"
REM The script seems to hang here. Either both the scripts will remain open or close or a combination depending on where CALL and START were used.
PAUSE
GOTO Options
I saw elsewhere I could have used the FC command. I didn't know about it until troubleshooting this problem. I've gotten this far and I feel I'm just missing some small trick to get the new updated script to launch.
If you need any more info let me know. I think I covered everything.
#ECHO OFF
SETLOCAL
TITLE Super Script 5000
:Options
REM I'll set the variable here to what is set elsewhere in the script.
SET VERSION=Version 1.7
CLS
ECHO.
ECHO.
ECHO %VERSION%
ECHO.
ECHO.
ECHO ==================== OPTIONS =====================
ECHO.
ECHO 1. Update Script WIP
ECHO 2. Install entire script to Statup Folder
ECHO 3. Install individual items for auto-launch
ECHO 4. Install Windows Logout feature
ECHO 5. Uninstall
ECHO 6. Change Colors
ECHO 7. Changelog
ECHO.
ECHO =======PRESS 'Q' TO QUIT OR 'B' TO GO BACK========
ECHO.
SET "OPTION="
SET /P "OPTION=Select an Option: "
IF /I "%OPTION%"=="1" GOTO Update
IF /I "%OPTION%"=="2" GOTO Install
IF /I "%OPTION%"=="3" GOTO I_Install
IF /I "%OPTION%"=="4" GOTO I_Logout
IF /I "%OPTION%"=="5" GOTO Uinstall
IF /I "%OPTION%"=="6" GOTO Colors
IF /I "%OPTION%"=="7" GOTO Changelog
IF /I "%OPTION%"=="q" GOTO quit
IF /I "%OPTION%"=="quit" GOTO quit
IF /I "%OPTION%"=="b" GOTO menu
IF /I "%OPTION%"=="back" GOTO menu
ECHO.
ECHO ============INVALID INPUT============
ECHO Please select a number
ECHO or select 'Q' or 'B'.
ECHO ======PRESS ANY KEY TO CONTINUE======
PAUSE > NUL
GOTO Options
:Update
ECHO.
IF EXIST "%appdata%\Microsoft\Windows\Start Menu\Programs\Startup\Shortcut Browser *.bat" (
GOTO Update_1
) ELSE (
ECHO You have not installed the script yet. Please install the script first.
)
PAUSE
GOTO Options
:Update_1
IF EXIST "%tmp%\update.bat" (
2>NUL DEL "%tmp%\update.bat" || GOTO Options
)
SETLOCAL
FOR %%A IN (
"%appdata%\Microsoft\Windows\Start Menu\Programs\Startup\Shortcut Browser *.bat"
) DO (
SET "old_version=%%~A"
SET "old_dir=%%~dpA"
SET "old_file=%%~nxA"
)
FOR %%A IN (
"%userprofile%\Desktop\Shortcut Browser *.bat"
) DO (
SET "new_version=%%~A"
SET "new_dir=%%~dpA"
SET "new_file=%%~nxA"
)
IF NOT DEFINED old_version (
ENDLOCAL
GOTO Options
) ELSE IF NOT DEFINED new_version (
ENDLOCAL
GOTO Options
)
(
ECHO #ECHO OFF
ECHO SETLOCAL
ECHO.
ECHO ECHO Update and restarting...
ECHO ^>NUL PING localhost -n 3
ECHO.
ECHO SET "old_version=%old_version%"
ECHO SET "new_version=%new_version%"
ECHO SET "updated_version=%old_dir%\%new_file%"
ECHO.
ECHO CALL :log /clear_log
ECHO.
ECHO IF NOT EXIST "%%new_version%%" (
ECHO CALL :log ERROR: Not exist "%%new_version%%".
ECHO EXIT /B 1
ECHO ^)
ECHO.
ECHO CALL :log Move old_version to old_version.bak.
ECHO MOVE "%%old_version%%" "%%old_version%%.bak" ^|^| (
ECHO CALL :log ERROR: Move failed to backup old version.
ECHO EXIT /B 2
ECHO ^)
ECHO.
ECHO CALL :log Copy new_version to old_version.
ECHO COPY "%%new_version%%" "%%appdata%%\Microsoft\Windows\Start Menu\Programs\Startup\" ^|^| (
ECHO CALL :log ERROR: Copy failed. Restore old version.
ECHO MOVE "%%old_version%%.bak" "%%old_version%%" ^|^| (
ECHO CALL :log ERROR: Restore old version failed.
ECHO EXIT /B 3
ECHO ^)
ECHO EXIT /B 4
ECHO ^)
ECHO.
ECHO CALL :log Delete old_version.bak.
ECHO DEL "%%old_version%%.bak" ^|^| (
ECHO CALL :log WARNING: Delete backup file failed.
ECHO ^)
ECHO.
ECHO IF NOT EXIST "%%updated_version%%" (
ECHO CALL :log ERROR: Not exist: "%%updated_version%%"
ECHO EXIT /B 5
ECHO ^)
ECHO.
ECHO ENDLOCAL
ECHO.
ECHO ECHO Ready to restart updated version
ECHO PAUSE
ECHO CALL "%old_dir%\%new_file%"
ECHO EXIT /B
ECHO.
ECHO :log
ECHO SETLOCAL
ECHO SET "log=%%tmp%%\update.log"
ECHO IF /I "%%~1"=="/clear_log" TYPE NUL ^> "%%log%%" ^& EXIT /B
ECHO IF /I "%%~1"=="/delete_log" 2^>NUL DEL "%%log%%" ^& EXIT /B
ECHO SET args=%%*
ECHO IF NOT DEFINED args EXIT /B
ECHO ECHO %%*
ECHO ^>^> "%%log%%" ECHO %%*
ECHO EXIT /B
) > "%tmp%\update.bat"
ENDLOCAL
START "Super Script 5000" "cmd /c "%tmp%\update.bat""
EXIT
:Quit
Tested on a Win7 VM. The script needs to restart on update else
weird issues may occur. CMD does not like scripts changing as it
is reading and interpreting them. Even reusing the same console
using START with /B may cause key input issues.
update.bat is overwritten every update as it stores absolute paths
so each update will be different paths with the filenames changing.
Using a wildcard with CALL may not work too well otherwise.
update.bat moves the old version to a .bak file, copies the new
version and if successful, deletes the .bak file, else moves the
.bak file to the old version. This is just to ensure a current
version always exists even if the update fails.
Changed quotes in :Options label from ' to ". Quotes are not
interchangeable like some other languages. Example: "%OPTION%"
will work if contains a space though '%OPTION%' may cause error.
I notice no SETLOCAL at the top of your full script. Advise you add
it unless you have a good reason not to.
I merged labels :Update_1 and :Update_2 into one.
Perhaps adjust Ping localhost -n number if more or less time is
needed.
Look at the %tmp%\update.log if a problem happens. Though expect
the errors to show in the console as the label :log in
update.bat echoes to console and writes to the log.
I've changed something on my batch file, how can I get it started as administrator if a user press a key?
echo Checking for Administrator elevation...
openfiles > NUL 2>&1
if %errorlevel%==0 (
echo Elevation found! Proceeding...
goto menu
) else (
echo You are not running as Administrator...
echo This batch cannot do it's job without elevation!
echo.
echo Right-click and select ^'Run as Administrator^' and try again...
echo.
echo Press any key to exit...
pause > NUL
exit
)
I know that it currently closes the script when there is no admin right, I want to start it as admin without closing it again.
:elevatecheck
color 0c
cls
echo Starting the Windows Toolkit as administrator...
echo.
echo If asked to allow, choose ^YES^...
openfiles > NUL 2>&1
if %errorlevel%==0 (
echo.
cls
echo You started the Windows Toolkit as administrator...
timeout /t 2 >nul 2>&1 /nobreak
echo.
echo Windows Toolkit is starting...
timeout /t 3 >nul 2>&1 /nobreak
goto menu
) else (
:checkPrivileges
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges )
:getPrivileges
if '%1'=='ELEV' (shift & goto gotPrivileges)
setlocal DisableDelayedExpansion
set "batchPath=%~0"
setlocal EnableDelayedExpansion
ECHO Set UAC = CreateObject^("Shell.Application"^) > "%temp%\OEgetPrivileges.vbs"
ECHO UAC.ShellExecute "!batchPath!", "ELEV", "", "runas", 1 >> "%temp%\OEgetPrivileges.vbs"
"%temp%\OEgetPrivileges.vbs"
exit /B
:gotPrivileges
goto menu
)
menu
This solved my problem.