[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

43
Reloader45ACPOP•

Here is where I first learned about the shift command.

http://technet.microsoft.com/en-us/library/bb491002.aspx

2
Danooodle•

The shift command works with parameters/arguments to a batch file/label, so it won't be applicable in this case.
The following code is an example that uses the shift command to cycle through it's arguments one by one without using an explicit for-loop:

@echo off
call :Create_File name.ext name2.ext C:\path\name3.ext "C:\path 2\name4.ex2"
echo Complete.
pause
exit /b

:Create_File
if "%~1" == "" goto :eof
echo Creating file at "%~f1"
if exist "%~f1" (
	echo File already exists.
) else (
	if not exist "%~dp1" md "%~dp1"
	copy nul "%~f1" >nul && echo File created successfully
)
shift /1
goto :eof

As you've seen the choice command only lets the user type one character.
Also, the choice command sets the errorlevel to the POSITION of the character in the possible-values string. I.E. choice /C "1230" would set the errorlevel to 1, 2, 3 or 4 respectively.

Possible techniques:

  1. Use multiple choice commands to get multiple digits from the user. (Like a vending machine)
  2. Use the set /p command to get a string of input from the user. (I've seen this used the most)
  3. Allow the user to type letters as well as digits. (Easiest to implement for up to ~35 values)

Here's some sample code for each. This replaces everything from :MENU to IF %M%==99 goto OTHER.

I haven't tested any of this, so let me know if anything doesn't work.


Example 1 : Multiple Choice commands.

:MENU
ECHO. 
ECHO .......................................................
ECHO Please select a location or type 99 to EXIT
ECHO .......................................................
ECHO.
ECHO (01) - New York Server
ECHO (02) - Los Angles Server
ECHO (03) - Chicago Server
ECHO (04) - Houston Server
ECHO (05) - Philadelphia Server
ECHO (06) - Phoenix Server
ECHO (07) - San Antonio Server
ECHO (08) - San Diego Server
ECHO (09) - Dallas Server
ECHO (10) - San Jose Server
ECHO (11) - Austin Server
ECHO (12) - OTHER (Push to Workstation)
ECHO (99) - EXIT
ECHO.
CHOICE /C:1234567890
SET /A "M=10*%ERRORLEVEL:~-1%"
CHOICE /C:1234567890
SET /A "M+=%ERRORLEVEL:~-1%"
CHOICE /M "Selected option %M%, are you sure"
IF ERRORLEVEL 2 GOTO :MENU

IF %M%==1 GOTO NewYork
IF %M%==2 GOTO LosAngles
:: Deleted repeated text to save space
IF %M%==11 GOTO Austin
IF %M%==12 GOTO OTHER
IF %M%==99 EXIT /B
ECHO Invalid selection (%M%)
GOTO :MENU

Example 2 : Using the set /p command.

: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.
SET /P "M=Input>"

IF "%M%"=="1" GOTO NewYork
IF "%M%"=="2" GOTO LosAngles
:: Deleted repeated text to save space
IF "%M%"=="11" GOTO Austin
IF "%M%"=="12" GOTO OTHER
IF "%M%"=="99" EXIT /B
ECHO Invalid selection ("%M%")
GOTO :MENU

Example 3 : Using other characters for input.

:MENU
ECHO. 
ECHO .......................................................
ECHO Please select a location or type X 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 (A) - San Jose Server
ECHO (B) - Austin Server
ECHO (C) - OTHER (Push to Workstation)
ECHO (X) - EXIT
ECHO.
CHOICE /C:123456789ABCX
set /a "M=%ERRORLEVEL%"

IF %M%==1 GOTO NewYork
IF %M%==2 GOTO LosAngles
:: Deleted repeated text to save space
IF %M%==11 GOTO Austin
IF %M%==12 GOTO OTHER
:: IF %M% GTR 12 (i.e. >'C')...
EXIT /B

2
Reloader45ACPOP•

You just blew my mind. :)

2
[deleted]•

[deleted]

1