10

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
Abhishek Goel
  • 18,785
  • 11
  • 87
  • 65

4 Answers4

9

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.

Monacraft
  • 6,510
  • 2
  • 17
  • 29
9

Check for presence of variable. If not present, asume false. Any value diferent from "true" asumed to be false

if not defined upgrade set upgrade=false
if not "%upgrade%"=="true" set upgrade=false

When upgrade variable needs to be true, define it before calling your batch file, either from command line o use a second batch file to call the first with the value set.

So, main batch (ej. doWork.cmd) file will look

@echo off
    .... work work work ...
    if not defined upgrade set upgrade=false
    if not "%upgrade%"=="true" set upgrade=false
    .... work work work ...

When you need to start with another configuration (do the upgrade in this sample), you need to configure variable from command line

set upgrade=true 
doWork.cmd

Or you can create a second cmd to do the same thing (ej. doWorkWithUpload.cmd)

@echo off
    set upgrade=true
    call doWork.cmd
MC ND
  • 69,615
  • 8
  • 84
  • 126
  • Thankyou. I have tried it by giving parameter as upload=true but its still taking false – Abhishek Goel Oct 22 '13 at 08:31
  • if not defined upload set upload=false if not "%upload%"=="true" set upload=false then i am doing echo %upgrade% I am getting the value as false everytime – Abhishek Goel Oct 22 '13 at 08:33
  • 1
    Sorry. When i readed your text is has "upload" as the name of the variable, not upgrade. Changing "upload" with "upgrade" should solve it – MC ND Oct 22 '13 at 08:52
  • My earliar doubt got resolved. My new doubt is instead of just defining true in parameters I want to define as upgrade=true. How do I do that – Abhishek Goel Oct 22 '13 at 09:07
  • case 1 : > somecommand.bat true ------------ case 2: > somecommand.bat upgrade=true --------------------- I want to implement second one but using your code I am able to implement the first one – Abhishek Goel Oct 22 '13 at 09:08
  • 1
    Answer updated. I usually have to make processes depend on machine variables, with default values unles changed from environment. Works for me, but seeing what you are trying, not sure if this adapts to what you want. – MC ND Oct 22 '13 at 09:29
2

Whenever I want a boolean value in a batch script, I use the absence of a variable to mean FALSE, and the presence of a variable (any value) to mean TRUE.

To set the value to TRUE, simply define the variable with any value. I like to use a value of 1:

set var=1

To set the value to FALSE, undefine the variable:

"set var="

Whenever I need to test the value, I simply use

if defined var (rem true conditional statements) else (rem false conditional statements)

Now, to allow for an optional parameter that sets upgrade to logical TRUE - Initialize the value to false, and then check your parameter list to see if an option exists, and if it does, set the value to TRUE. I recommend an option like /U to mean upgrade. You don't say whether your script already has parameters. I'll assume you have 2 required parameters.

You should decide if your options come before or after your required parameters. The easiest is after. So if you have 2 required parameters, then the option, if present, would be in %3

@echo off
setlocal
:: Initialize default value to FALSE
set "upgrade="
:: Look for option
if /i %3 equ /U set upgrade=1

You can put your options in the front, but then your required argument values must be quoted if the value begins with /. After processing an option, use SHIFT /1 to guarantee that your required parameters start with %1

@echo off
setlocal
:: Initialize default value to FALSE
set "upgrade="
:: Look for option
if /i %1 equ /U (
  set upgrade=1
  shift /1
)
:: Required parameters now start with %1, regardless whether the option was present.

You can extend the above methods to support multiple options by adding a :parseOptions label, followed by multiple IF statements, one per option. Each time an option is discovered, simply SHIFT the parameters and GOTO :parseOptions to look for the next option. If options are at the end, then use SHIFT /3. If options are at the beginning, then SHIFT /1. But the coding becomes tedious and error prone if there are a lot of options.

Take a look at Windows Bat file optional argument parsing for a convenient and powerful way to define many optional parameters. It provides a mechanism to specify the default value for each option. It may be a bit of overkill for a single option, but it is very helpful if you have a lot of options.

For an example of a script with many options, see getTimestamp.bat for time and date processing

Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390
1

just put the command line parameter in double quotes:

script.bat parameterA "upload=true"

The equal sign = is one of the 'default delimiters', like space, Tab, , and ;.

Endoro
  • 37,015
  • 8
  • 50
  • 63