Batch: Get parameters from command line or user input

When you write a batch file where you either want the caller to provide a parameter or ask the user to type it in if not provided, you can use the following piece of code:

set MyParameter=%1
if “%MyParameter%”==”” (
:input
set INPUT=
set /P INPUT=Enter a value for the parameter: %=%
)
if “%MyParameter%”==”” (
if “%INPUT%”==”” goto input
)
if “%MyParameter%”==”” (
set MyParameter=%INPUT%
)

It first gets the first command line argument. If it’s not empty you can just skip every (so either do it the way I did it or just use a goto.

As long as the user hasn’t entered anything, the script will keep asking. Instead of just checking for an empty string, you could also have a more elaborate validation of the input.

The multiple if blocks are required because if you put it all in one block the %INPUT% variable will only be evaluated when the script is started and not again after user input.

Batch: convert to uppercase

The following batch file can be used to convert a string to uppercase:

uppercase.bat:

@ECHO OFF
SET STRING=%1
IF [%STRING%]==[] GOTO:EOF
SET STRING=%STRING:a=A%
SET STRING=%STRING:b=B%
SET STRING=%STRING:c=C%
SET STRING=%STRING:d=D%
SET STRING=%STRING:e=E%
SET STRING=%STRING:f=F%
SET STRING=%STRING:g=G%
SET STRING=%STRING:h=H%
SET STRING=%STRING:i=I%
SET STRING=%STRING:j=J%
SET STRING=%STRING:k=K%
SET STRING=%STRING:l=L%
SET STRING=%STRING:m=M%
SET STRING=%STRING:n=N%
SET STRING=%STRING:o=O%
SET STRING=%STRING:p=P%
SET STRING=%STRING:q=Q%
SET STRING=%STRING:r=R%
SET STRING=%STRING:s=S%
SET STRING=%STRING:t=T%
SET STRING=%STRING:u=U%
SET STRING=%STRING:v=V%
SET STRING=%STRING:w=W%
SET STRING=%STRING:x=X%
SET STRING=%STRING:y=Y%
SET STRING=%STRING:z=Z%
ECHO %STRING%

This can be used this way:

> uppercase.bat “I want to be an uppercase string”
“I WANT TO BE AN UPPERCASE STRING”