0

I want to use the first argument of my batch script as a variable. If it contains FULL I will set all my variables to 1 and if it's empty I will ask for each.

But I can't make my code work :

 @echo off
        color 02

    ::-----------------------------------------
    :: Variables
    set Preset=%1 
    set Profile=%2 
    set NeedToPassTheTests=1
    set PublishAPI=0

    REM ---------------------
REM We fill the variables depending on the arguments
echo(%1
if defined Preset (

if "%Preset%"=="FULL" (
echo FULL
set PublishOCPP=1
set NeedToPassTheTests=1 
set PublishAPI=1
)

) else (
    REM No preset so we are going to ask
    set /p PublishAPI="Publish API ? (0 or 1).............. ? "
    set /p NeedToPassTheTests="Test the projects before ? (0 or 1).............. ? "
)


echo NeedToPassTheTests %NeedToPassTheTests% 
echo %PublishAPI%

echo.
pause

I get

The syntax of the command is incorrect

And also my echo at the end are not printing the value (when I remove my ifs) it only displays echo is off.

I'm starting with .bat files, I've tried with only the is Empty test (taken from here : What is the proper way to test if variable is empty in a batch file? ) and it doesn't work either.

Do you know why ?

UPDATE : It works when I pass FULL as argument but my variables are not updated. however it the parameter is empty it doesn't work (ie doesn't ask me to fill the variables).

Community
  • 1
  • 1
user2088807
  • 1,378
  • 2
  • 25
  • 47
  • 1
    Don't use `::comment` within a *code block* (parenthesised sequence of statements) - use `rem ` The `::` is a broken label and labels within code blocks can lead to unexpected results. – Magoo Mar 16 '17 at 13:48
  • Use the syntax `if "%varname%"=="value" (dothis) else (dothat)` to compare strings which may contain separators like spaces or you'll get a syntax error. The quotes cause the contained values to be interpreted as a single string. – Magoo Mar 16 '17 at 13:51
  • if "%Preset%"=="" also gives me an error with incorrect syntax :( – user2088807 Mar 16 '17 at 15:05
  • Please show the entire code line. What is the content of `preset`? – Magoo Mar 16 '17 at 15:10
  • it's in my initial post, Preset contains %1 – user2088807 Mar 16 '17 at 15:11
  • `%1` means the first parameter to the batch file. What parameters were you giving the batch file, ie your invocation line is *thisbatch whatisthisvalue* – Magoo Mar 16 '17 at 15:13
  • Empty in my example, I want to be able to check if it's empty. – user2088807 Mar 16 '17 at 15:15
  • Further problem with your code structure. The `(` for the `if` must be on the same line as the `if` and the string `) else (` must also all be on one line – Magoo Mar 16 '17 at 15:20
  • 1
    you may be interested to use [choice](https://ss64.com/nt/choice.html) instead of `set /p`: no wrong inputs possible. – Stephan Mar 16 '17 at 16:53

2 Answers2

1

I'm not sure which errors are in the code but I can see several bugs in a second.

set NeedToPassTheTests=1 will set the variable called NeedToPassTheTests to the value 1

set NeedToPassTheTests<space>=1 will set NeedToPassTheTests<space> to 1

set NeedToPassTheTests<space>=<space>1 will set NeedToPassTheTests<space> to <space>1.

So first of all, get rid of any unnecessary spaces.

MichaelS
  • 5,941
  • 6
  • 31
  • 46
0
set NeedToPassTheTests = 1

Remove the spaces. Batch is sensitive to spaces on oth sides of the assignment - you are setting a variable named "NeedToPassTheTestsspace" to a value of "space1"

To test for variable var is empty:

if defined var (echo variable is not empty) else (echo variable is empty)

Quirk:

echo(%var%

will show the variable if it's not empty or an empty line if it is (not the echo status)

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • I don't have an error anymore my bad. But when my variable is empty (no arguments passed, echo %1 show "ECHO is off" it's considered not empty. – user2088807 Mar 16 '17 at 15:30
  • 1
    With an empty argument, `echo` shows the current `echo` status (on or off) Use `echo(%1` (or `echo(%potentiallyemptyvariable%`) which will yield an empty line if the argument evaluates to *nothing* Note that the `(` is required for this. – Magoo Mar 16 '17 at 15:35
  • Yes it's empty :) But it doesn't go in the else, please see my update code – user2088807 Mar 16 '17 at 15:40
  • Use the syntax like `"set Preset=%1"` on all string-set instructions. Your code contains trailing spaces on lines, so `preset` is being set to Space which is a little hard to see but is a value, so `preset` **is** defined. (and yes - this can be a real head-scratcher. Using the quotes becomes Pavlovian after you've spent hours trying to figure it out...) – Magoo Mar 17 '17 at 03:26