Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have created a bat file to run a simple backup program over night.

This are my lines:


@echo off
echo.
echo Please select the followings:
echo.
echo *************************************
echo.
echo.
echo.
echo Select "Y" if you want to start backup.
echo.
echo ****************************************************
echo.
echo.
echo.
echo Select "N" to shut down without backup.
echo.
echo ****************************************************
echo.
echo.
echo.
echo Select "R" to restart without backup.
echo.
echo ****************************************************
echo.
echo.
echo.
set /p choice=Select Y or N or R and Enter:
set choice=%choice:~0,1%
if "%choice%"=="n" shutdown -s -t 0
if "%choice%"=="N" shutdown -s -t 0
if "%choice%"=="y" c:\backup.exe
if "%choice%"=="Y" c:\backup.exe
if "%choice%"=="r" shutdown -r -t 0
if "%choice%"=="R" shutdown -r -t 0

Currently after I press y and enter, the backup.exe runs but the dos screen just stop there. I would like to improve it.

I would like to add in some text like: "Well done! Backup started, please remember to off the monitor. Good night."

Is this possible?

share|improve this question

4 Answers 4

Try changing the line "if "%choice%"=="y" c:\backup.exe" to this:


if "%choice%"=="y" echo Well done! Backup started, please remember to off the monitor. Good night. & c:\backup.exe

You can put multiple commands on one line separated by the "&" sign.

I also recommend that you add the switch -f to your shutdown commands (shutdown -r -f -t 00). have you ever had to hit "End task" before your computer shut down? Well, the -f switch does that for you if you are not there to do it.

share|improve this answer

A variation, and (I think simpler and clearer) solution based on that of @emb1995. I have shortened for brevity. The solution assumes Windows 7 and command extensions enabled:

@echo off

echo.
echo Please select one of the following:
echo.
echo Select "Y" if you want to start backup.
echo Select "N" to shut down without backup.
echo Select "R" to restart without backup.
echo.
:select
set /p choice=Select Y or N or R and press Enter:
set choice=%choice:~0,1%
if /I "%choice%" EQU "n" (
    shutdown -s -t 0
) else if /I "%choice%" EQU "y" (
    echo Well done! Backup started, please remember to turn off the monitor. Good night.
    c:\backup.exe
) else if /I "%choice%" EQU "r" (
    shutdown -r -t 0
) else (
    echo.
    echo Please enter one of the listed options...
    echo.
    goto :select
)
share|improve this answer
    
+1 I would also start the backup program, as the question also mentions the prompt hanging while the backup executes. –  paradroid Jul 12 '11 at 23:30

Yes you can! Labels, e.g. :start, are a great way to use batch files. You can jump to them, goto start, or just use them as section comments. Take a look at this revised code. I added some labels, and answered your question. Instead of just running the command c:\backup.exe, the batch file jumps to the section labeled :backup. Which displays your message, and then executes the program.

@echo off
:start
echo.
echo Please select one of the following:
echo.
echo *************************************
echo.
echo.
echo.
echo Select "Y" if you want to start backup.
echo.
echo ****************************************************
echo.
echo.
echo.
echo Select "N" to shut down without backup.
echo.
echo ****************************************************
echo.
echo.
echo.
echo Select "R" to restart without backup.
echo.
echo ****************************************************
echo.
echo.
echo.
set /p choice=Select Y or N or R and press Enter:
set choice=%choice:~0,1%
if "%choice%"=="n" shutdown -s -t 0
if "%choice%"=="N" shutdown -s -t 0
if "%choice%"=="y" goto backup
if "%choice%"=="Y" goto backup
if "%choice%"=="r" shutdown -r -t 0
if "%choice%"=="R" shutdown -r -t 0
:end
:backup
echo Well done! Backup started, please remember to turn off the monitor. Good night.
c:\backup.exe

EDIT: You also dont need the case-checking ifs. Just use the /I flag.

if /I "%choice%"=="N" shutdown -s -t 0
if /I "%choice%"=="Y" goto backup
if /I "%choice%"=="R" shutdown -r -t 0
share|improve this answer
    
The /I and goto works well. thanks. –  Mamister Jul 18 '11 at 0:54
    
If this is your chosen solution please click the green checkmark next to my answer. It helps the community see if a final answer was posted. Or, if you like BillP3rd's solution better (it's a lot cleaner/compressed), click on his checkmark. –  evan.bovie Jul 20 '11 at 0:40

I probably would have written it like this:

@echo off

:menu
    echo.
    echo   Please choose one of the following:
    echo.
    echo     (S)hutdown
    echo     (R)estart
    echo     (B)ackup
    echo.
    choice /c:srb "  What would you like to do "
    echo.

    if errorlevel 3 goto backup
    if errorlevel 2 goto restart
    if errorlevel 1 goto shutdown
    if errorlevel 0 goto menu

:shutdown
    echo   Good night.
    start  shutdown -s -t 0 -c "Shutting down from menu."
    goto   :eof

:restart
    echo   Rebooting; just a moment...
    start  shutdown -r -t 0 -c "Restarting from menu."
    goto   :eof

:backup
    echo   Running backup... Please remember to off the monitor.
    start  c:\backup.exe
    goto   :eof

There’s a few things to note. Instead of SET, I used the CHOICE command which is meant for this purpose. While it makes things slightly harder because it uses errorlevel, it provides better customization and control over the prompt, including default selection time-out and defaults to case-insensitive (which makes things much easier most of the time). Next, I used proper formatting and indentation to make the script more readable and maintainable. I did the same with the output, which makes the text and prompts easier to see in the console window. I also threw in a bit of error-detection where it will loop the menu and notice the EOFs; they are necessary because the shutdown command initiates shutdown and returns, even with the start, so you need to avoid falling through to the next “function” (you can actually call labels like functions, but that would unnecessarily complicate a simple menu like this).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.