Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add optional command line parameter in bat file ( windows )

Tags:

batch-file

I have a batch file in with the following parameter hard coded

SET upgrade=false

I want to give user an option to explicitly define the upload parameter. By default it should be false and if user explicitly define upgrade=true I should treat it as true.

I also wants to check the validation for boolean value in upload parameter.

I am new to batch file processing. I have tried with the default value processing.

if "%2"=="" goto false

:false
SET upgrade=false
like image 571
Abhishek Goel Avatar asked Oct 22 '13 06:10

Abhishek Goel


People also ask

How do I pass a command line argument to a batch file?

In the batch script, you can get the value of any argument using a % followed by its numerical position on the command line. The first item passed is always %1 the second item is always %2 and so on. If you require all arguments, then you can simply use %* in a batch script.

How do I pass a command line argument in CMD?

For example, entering C:\abc.exe /W /F on a command line would run a program called abc.exe and pass two command line arguments to it: /W and /F.

What is %% in a BAT file?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.

What is %~ n0 in batch file?

%0 is the name of the batch file. %~n0 Expands %0 to a file Name without file extension.


1 Answers

Easy, try this. To set upgrade to true use /U as a parameter:

@echo off
set upgrade=FALSE
:parse
if /i "%1" EQU "/u" set upgrade=TRUE
if /i "%1" EQU "/?" Echo HELP MSG & goto :eof

if "%1" NEQ "" (shift /1 & goto :parse)

And then you can add on the rest of your code.

like image 97
Monacraft Avatar answered Sep 20 '22 18:09

Monacraft