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).

9
  • 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. Commented Mar 16, 2017 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. Commented Mar 16, 2017 at 13:51
  • if "%Preset%"=="" also gives me an error with incorrect syntax :( Commented Mar 16, 2017 at 15:05
  • Please show the entire code line. What is the content of preset? Commented Mar 16, 2017 at 15:10
  • 1
    you may be interested to use choice instead of set /p: no wrong inputs possible. Commented Mar 16, 2017 at 16:53

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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)

4 Comments

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.
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.
Yes it's empty :) But it doesn't go in the else, please see my update code
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 <kbd>Space</kbd> 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...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.