[Q] Batch Files - How to Use more than 10 parameters
Hello Batch File Jedi Masters,
I need your help. A while back I wrote a really basic batch script that had eight choices you could choose from. Each choice represented a server. If you typed in 1, it would run an XCOPY command and send a file on my PC to my server in New York. If I typed 2, it would run another command XCOPY and send a file on my PC to my Los Angles Server. It was super simple and my co-workers really liked it.
I decided to add a few more servers to the list, twelve total, and this is when I ran into my problem.
** WORKING CODE **
ECHO OFF
:: AUTHOR: John Doe
:: DATE: 10-10-2014
:: VERSION: 0.1
CLS
:MENU
ECHO.
ECHO .......................................................
ECHO Please pick a location or type 3 to EXIT
ECHO .......................................................
ECHO.
ECHO (1) - New York Server
ECHO (2) - Los Angles Server
ECHO (3) - EXIT
ECHO.
CHOICE /C:123
IF ERRORLEVEL 1 SET M=1
IF ERRORLEVEL 2 SET M=2
IF ERRORLEVEL 3 SET M=3
IF %M%==1 GOTO NewYork
IF %M%==2 GOTO LosAngles
IF %M%==3 GOTO EOF
:NewYork
xcopy "C:\Data\form324.sff" "\\NewYorkserver\Forms\Updates\" /y /z /v /K >> C:\Data\Log.txt
ECHO Press ENTER to return to MENU.
PAUSE >nul
GOTO MENU
:LosAngles
xcopy "C:\Data\form583.sff" "\\NewYorkserver\Forms\Updates\" /y /z /v /K >> C:\Data\Log.txt
ECHO Press ENTER to return to MENU.
PAUSE >nul
GOTO MENU
** NON-WORKING CODE **
ECHO OFF
:: AUTHOR: John Doe
:: DATE: 10-10-2014
:: VERSION: 0.1
CLS
:MENU
ECHO.
ECHO .......................................................
ECHO Please select a location or type 99 to EXIT
ECHO .......................................................
ECHO.
ECHO (1) - New York Server
ECHO (2) - Los Angles Server
ECHO (3) - Chicago Server
ECHO (4) - Houston Server
ECHO (5) - Philadelphia Server
ECHO (6) - Phoenix Server
ECHO (7) - San Antonio Server
ECHO (8) - San Diego Server
ECHO (9) - Dallas Server
ECHO (10) - San Jose Server
ECHO (11) - Austin Server
ECHO (12) - OTHER (Push to Workstation)
ECHO (99) - EXIT
ECHO.
CHOICE /C:12345678910111299
IF ERRORLEVEL 1 SET M=1
IF ERRORLEVEL 2 SET M=2
IF ERRORLEVEL 3 SET M=3
IF ERRORLEVEL 4 SET M=4
IF ERRORLEVEL 5 SET M=5
IF ERRORLEVEL 6 SET M=6
IF ERRORLEVEL 7 SET M=7
IF ERRORLEVEL 8 SET M=8
IF ERRORLEVEL 9 SET M=9
IF ERRORLEVEL 10 SET M=10
IF ERRORLEVEL 11 SET M=11
IF ERRORLEVEL 12 SET M=12
IF ERRORLEVEL 99 SET M=99
IF %M%==1 GOTO NewYork
IF %M%==2 GOTO LosAngles
:: Deleted repeated text to save space
IF %M%==10 GOTO city10
IF %M%==11GOTO city11
IF %M%==12 GOTO OTHER
:NewYork
xcopy "C:\Data\form324.sff" "\\NewYorkserver\Forms\Updates\" /y /z /v /K >> C:\Data\Log.txt
ECHO Press ENTER to return to MENU.
PAUSE >nul
GOTO MENU
:LosAngles
xcopy "C:\Data\form583.sff" "\\NewYorkserver\Forms\Updates\" /y /z /v /K >> C:\Data\Log.txt
ECHO Press ENTER to return to MENU.
PAUSE >nul
GOTO MENU
:: Removed extra cities to save space.
:OTHER
set /p "computer=PC Name"
xcopy "C:\Data\update344.sf" "\\%computer%\c$\Program Files\AppName\Updates\" /k /y /z /v /i
ECHO Press ENTER to return to MENU.
PAUSE >nul
GOTO MENU
If you notice, my original thinking was to write CHOICE /C:123456789101112, but I quickly learned it doesn't work that way. After doing a bit of testing and some Goggle reading, I learned that the CHOICE command only works up to 9 (e.g. CHOICE /C:123456789).
I've heard of people getting around this limitation using the SHIFT command. However, I wasn't able to successfully implement it, which is probably due to my lack of understanding how it works. Or I might just be going about this all wrong.
Can someone with a better understanding of writing batch scripts look over my code and help me. Thanks!
Posted Oct 10, 2014, 10:44 PM UTC · edited
Here is where I first learned about the shift command.
http://technet.microsoft.com/en-us/library/bb491002.aspx