Computer Hope

Microsoft => Microsoft DOS => Topic started by: robotmonkey on August 16, 2010, 07:23:56 PM

Title: Batch file using CHOICE command
Post by: robotmonkey on August 16, 2010, 07:23:56 PM
I am trying to make a batch operation that backs up files but I don't always want to back them all up so I've split it into choices.  I tried searching and found some examples which I followed, but still mine is not working.  The problem is it automatically selects option 9 and begins to shutdown the computer... fixes?

Code: [Select]
@echo off
ECHO MyBackup Batch Beta
ECHO ==================================================================
ECHO 1. Copy DOCUMENTS to local backup only
ECHO 2. Create archive of DOCUMENTS only
ECHO 3. Create archive of HTDOCS only
ECHO 4. Create archives of both Documents and HTDOCS
ECHO 5. Move archives to ROBOSAM and delete from local backup
ECHO 6. Copy documents to local, archive, and move archives to RS
ECHO 7. Clean up all files in Local Backup (deletes permanently!)
ECHO 8. Quit
ECHO 9. Shutdown the computer
ECHO ==================================================================
ECHO Please make a choice.
CHOICE /C:123456789 Pick a number.
IF ERRORLEVEL ==9 GOTO EXIT_WIN
IF ERRORLEVEL ==8 GOTO EXIT_B
IF ERRORLEVEL ==7 GOTO CLEAN_LB
IF ERRORLEVEL ==6 GOTO DO_ALL
IF ERRORLEVEL ==5 GOTO MOVE_ZIP
IF ERRORLEVEL ==4 GOTO ZIP_ALL
IF ERRORLEVEL ==3 GOTO ZIP_HT
IF ERRORLEVEL ==2 GOTO ZIP_D
IF ERRORLEVEL ==1 GOTO QUICK_B
:QUICK_B
CALL qb.bat
GOTO :EOF
:ZIP_D
CALL zipdocs.bat
GOTO :EOF
:ZIP_HT
CALL ziphtdocs.bat
GOTO :EOF
:ZIP_ALL
CALL zipall.bat
GOTO :EOF
:MOVE_ZIP
CALL movezips.bat
GOTO :EOF
:DO_ALL
CALL doall.bat
GOTO :EOF
:CLEAN_LB
CALL cleanlb.bat
GOTO :EOF
:EXIT_B
EXIT
GOTO :EOF
:EXIT_WIN
CALL shutdown.bat
GOTO :EOF

I added the GOTO :EOFs because I read the commands might be "falling through" but still it just displays the list and runs choice 9 right away.  I should note, I've tried adding
Code: [Select]
/T:1,5 after CHOICE /C:123456789, and it still runs the shutdown.

If you want to try it but don't want your computer shutdown, you can type shutdown.exe /a and it shouldn't, but come again shouldn't run the shutdown anyways since the shutdown.bat I made would have to be there.

edit: i've added a
Code: [Select]
:BEGIN section to the file now, made the linked bat files all inline (replacing
Code: [Select]
CALL ... with what was is those bat files and removed all but the last two GOTO :EOFs, replacing them with GOTO :BEGIN. It still runs #9 without giving me a chance to choose, all that was just to simplify it into one file.

New, more complicated code (user name redacted):

You WILL need to type shutdown.exe /a in the command to abort shut down if you try to run this version.

Code: [Select]
@echo off
:BEGIN
ECHO MyBackup Batch Beta v2
ECHO ==================================================================
ECHO 1. Copy documents to local backup only
ECHO 2. Create archive of documents only
ECHO 3. Create archive of htdocs only
ECHO 4. Create archives of both documents and htdocs
ECHO 5. Move archives to ROBOSAM and delete from local
ECHO 6. Copy documents to local, archive, and move archives to RS
ECHO 7. Clean up all files in Local Backup (deletes permanently!)
ECHO 8. Quit
ECHO 9. Shutdown the computer
ECHO ==================================================================
ECHO Please make a choice.
CHOICE /C:123456789 Pick a number.
IF ERRORLEVEL ==9 GOTO EXIT_WIN
IF ERRORLEVEL ==8 GOTO EXIT_B
IF ERRORLEVEL ==7 GOTO CLEAN_LB
IF ERRORLEVEL ==6 GOTO DO_ALL
IF ERRORLEVEL ==5 GOTO MOVE_ZIP
IF ERRORLEVEL ==4 GOTO ZIP_ALL
IF ERRORLEVEL ==3 GOTO ZIP_HT
IF ERRORLEVEL ==2 GOTO ZIP_D
IF ERRORLEVEL ==1 GOTO QUICK_B
:QUICK_B
xcopy /D /E /Q /G /R /K /Y "C:\Users\---\Documents" "C:\Users\---\Local Backup\Folders\Documents"
xcopy /D /E /Q /G /R /K /Y "C:\Users\---\Downloads" "C:\Users\---\Local Backup\Folders\Downloads"
GOTO :BEGIN
:ZIP_D
cd C:\Users\---\Roaming\PortableApps\Toucan
Toucan.exe -j ziplb /w
GOTO :BEGIN
:ZIP_HT
cd C:\Users\---\Roaming\PortableApps\Toucan
Toucan.exe /w -j ziphtdocs
GOTO :BEGIN
:ZIP_ALL
cd C:\Users\---\Roaming\PortableApps\Toucan
Toucan.exe /w -j ziplb
Toucan.exe /w -j ziphtdocs
GOTO :BEGIN
:MOVE_ZIP
cd C:\Windows\system32
xcopy /D /E /Q /G /R /K /Y "C:\Users\---\Local Backup\Zips" "E:\Backups"
del /q /f "C:\Users\---\Local Backup\Zips\*.*"
GOTO :BEGIN
:DO_ALL
xcopy /D /E /Q /G /R /K /Y "C:\Users\---\Documents" "C:\Users\---\Local Backup\Folders\Documents"
xcopy /D /E /Q /G /R /K /Y "C:\Users\---\Downloads" "C:\Users\---\Local Backup\Folders\Downloads"
cd C:\Users\---\Roaming\PortableApps\Toucan
Toucan.exe /w -j ziplb
Toucan.exe /w -j ziphtdocs
cd C:\Windows\system32
xcopy /D /E /Q /G /R /K /Y "C:\Users\---\Local Backup\Zips" "E:\Backups"
del /q /f "C:\Users\---\Local Backup\Zips\*.*"
GOTO :BEGIN
:CLEAN_LB
cd C:\Windows\System32
del /q /f "C:\Users\---\Local Backup\*.*"
GOTO :BEGIN
:EXIT_B
EXIT
GOTO :EOF
:EXIT_WIN
cd C:\Windows\System32
shutdown.exe /s
GOTO :EOF

The Toucan command call jobs I have saved in that program that zip certain folders for me.  Any suggestions on my #9 problem?  If you see any other problems too, let me know.
Title: Re: Batch file using CHOICE command
Post by: robotmonkey on August 16, 2010, 09:30:44 PM
ok folks, I got it working.  I guess you can delete this or leave it for reference.  The solution was in one of the articles on this site I had only skimmed before.  It mentioned for Windows XP using the SET command... well I'm using Windows 7, but I figured I'd give it a shot.  I left things the same under the layers, only adding some more stuff and looping them back to the start rather then GOTO :EOFing it.  I replaced the CHOICE commands with SET using the information here:

http://www.computerhope.com/sethlp.htm#04 (http://www.computerhope.com/sethlp.htm#04)

Haven't tested all rabbit trails because takes a while to run some operations, but here's my finished product, with some extra stuff added:

Code: [Select]
@ECHO off
cls
:start
ECHO.
ECHO ==================================================================
ECHO MyBackup Batch Beta v3 -- Welcome
ECHO ==================================================================
ECHO.
ECHO.
goto menu
:return
ECHO.
ECHO What else would you like to do?
ECHO.
ECHO.
goto menu
:menu
ECHO 1. Copy DOCUMENTS to local backup only
ECHO 2. Create archive of DOCUMENTS only
ECHO 3. Create archive of HTDOCS only
ECHO 4. Create archives of both DOCUMENTS and HTDOCS
ECHO 5. Move archives to ROBOSAM and delete from Local Backup
ECHO 6. Copy documents to local, archive, and move archives to RS
ECHO 7. Clean up all files in Local Backup (deletes permanently!)
ECHO 8. Quit
ECHO 9. Shutdown the computer
ECHO.
set choice=
set /p choice=Please choose 1-9 on the number row.
if not '%choice%'=='' set choice=%choice:~0,1%
if '%choice%'=='1' goto quickb
if '%choice%'=='2' goto zipd
if '%choice%'=='3' goto zipht
if '%choice%'=='4' goto zipall
if '%choice%'=='5' goto movezip
if '%choice%'=='6' goto doall
if '%choice%'=='7' goto cleanlb
if '%choice%'=='8' goto exitb
if '%choice%'=='9' goto exitwin
ECHO "%choice%" is not valid please try again
ECHO.
goto menu
:quickb
xcopy /D /E /Q /G /R /K /Y "C:\Users\---\Documents" "C:\Users\---\Local

Backup\Folders\Documents"
xcopy /D /E /Q /G /R /K /Y "C:\Users\---\Downloads" "C:\Users\---\Local

Backup\Folders\Downloads"
goto return
:zipd
cd "C:\Users\---\Roaming\PortableApps\Toucan"
Toucan.exe -j ziplb
cls
ECHO Files from DOCUMENTS have been compressed to the Local Backup.
ECHO Returning to menu...
pause
goto return
:zipht
cd "C:\Users\---\Roaming\PortableApps\Toucan"
Toucan.exe -j ziphtdocs
cls
ECHO Files from HTDOCS have been compressed to the Local Backup
ECHO Returning to menu...
pause
goto return
:zipall
cd "C:\Users\---\Roaming\PortableApps\Toucan"
Toucan.exe -j ziplb
Toucan.exe -j ziphtdocs
cls
ECHO DOCUMENTS and HTDOCS have been compressed to the Local Backup
ECHO Returning to menu...
pause
goto return
:movezip
cd C:\Windows\system32
xcopy /D /E /Q /G /R /K /Y "C:\Users\---\Local Backup\Zips" "E:\Backups"
del /q /f "C:\Users\---\Local Backup\Zips\*.*"
goto return
:doall
cls
ECHO Running all operations may take 10-20 minutes.
ECHO.
ECHO What would you like me to do after the batch it complete?
ECHO.
ECHO 1. Return to menu
ECHO 2. Quit MyBackup Batch
ECHO 3. Shutdown the computer
ECHO.
ECHO 4. I changed my mind, quit now.
set choice=
set /p choice=Please choose 1-4 on the number row.
if not '%choice%'=='' set choice=%choice:~0,1%
if '%choice%'=='1' goto dart
if '%choice%'=='2' goto daq
if '%choice%'=='3' goto dasd
if '%choice%'=='4' goto exitb
ECHO "%choice%" is not valid please try again
ECHO.
goto doall
:dart
xcopy /D /E /Q /G /R /K /Y "C:\Users\---\Documents" "C:\Users\---\Local

Backup\Folders\Documents"
xcopy /D /E /Q /G /R /K /Y "C:\Users\---\Downloads" "C:\Users\---\Local

Backup\Folders\Downloads"
cd C:\Users\---\Roaming\PortableApps\Toucan
Toucan.exe /w -j ziplb
Toucan.exe /w -j ziphtdocs
cd C:\Windows\system32
xcopy /D /E /Q /G /R /K /Y "C:\Users\---\Local Backup\Zips" "E:\Backups"
del /q /f "C:\Users\---\Local Backup\Zips\*.*"
goto return
:daq
xcopy /D /E /Q /G /R /K /Y "C:\Users\---\Documents" "C:\Users\---\Local

Backup\Folders\Documents"
xcopy /D /E /Q /G /R /K /Y "C:\Users\---\Downloads" "C:\Users\---\Local

Backup\Folders\Downloads"
cd C:\Users\---\Roaming\PortableApps\Toucan
Toucan.exe /w -j ziplb
Toucan.exe /w -j ziphtdocs
cd C:\Windows\system32
xcopy /D /E /Q /G /R /K /Y "C:\Users\---\Local Backup\Zips" "E:\Backups"
del /q /f "C:\Users\---\Local Backup\Zips\*.*"
goto exitb
:dasd
xcopy /D /E /Q /G /R /K /Y "C:\Users\---\Documents" "C:\Users\---\Local

Backup\Folders\Documents"
xcopy /D /E /Q /G /R /K /Y "C:\Users\---\Downloads" "C:\Users\---\Local

Backup\Folders\Downloads"
cd C:\Users\---\Roaming\PortableApps\Toucan
Toucan.exe /w -j ziplb
Toucan.exe /w -j ziphtdocs
cd C:\Windows\system32
xcopy /D /E /Q /G /R /K /Y "C:\Users\---\Local Backup\Zips" "E:\Backups"
del /q /f "C:\Users\---\Local Backup\Zips\*.*"
goto autoshut
:cleanlb
cd C:\Windows\System32
del /q /f "C:\Users\---\Local Backup\*.*"
goto return
:exitb
exit
:autoshut
cd C:\Windows\System32
shutdown.exe /s
:exitwin
cd C:\Windows\System32
shutdown.exe /s
goto end
:end
cls
ECHO Shutting down. Would you like to abort shutdown?
pause
shutdown.exe /a
cls
ECHO.
ECHO Shutdown aborted.
ECHO.
ECHO.
ECHO MyBackup Batch Beta v3 will now exit.
ECHO.
pause

Title: Re: Batch file using CHOICE command
Post by: BC_Programmer on August 16, 2010, 11:09:58 PM
the original issue was because your choice command had a syntax error.


the "pick a number" portion should have been in quotes and preceded by the /M switch, otherwise, you get an invalid argument error and it gives you back 255 for the errorlevel, and since an IF ERRORLEVEL will match an errorlevel equal to or greater then the specified value, it did the shutdown.


Either way, SET is a all around better solution.