-2

I define some parameter when calling a batch file:

:: Usertype:I,C
set Usertype=%~1
set Deltaval=%~2
If Usertype=="C" set Gender=NA
set Gender=%~3
If Gender==NA 
(
goto
END
)

However, I got issue at line If Usertype=="C" set Gender = NA with following error: The syntax of the command is incorrect.

Are there any solution to it?

  • Actually, no, you have more issues than just that one. you forgot the `%` which defines that the words are variables. `if /I "%userType%"=="C"` Also you need to explain what you are trying to do as `%usertype%` currently has a value `:~1` and `%dataval%` has a value of `~2` so not sure where you planned on using it. I suggest you read some help files. Open `cmd` and type `set /?` and `if /?` and lastly, the `goto` and `END` are on separate lines and I do not see `:END` label defined. – Gerhard Nov 04 '19 at 17:39
  • you can also not have a space when setting variables like `set Gender = NA` as that will create a variable with a space `%Gender %` and a value with a leading whitespace. – Gerhard Nov 04 '19 at 17:56
  • @GerhardBarnard Thanks. There are some errors while copying. I've updated code... – Nguyễn Văn Hưng Nov 04 '19 at 18:43
  • Ok. Did you look at my comments on how to use variables? – Gerhard Nov 04 '19 at 19:49
  • Yes. But What im trying to do is Usertype is first parameter Deltaval is second parameter if Usertype is "C" then set Gender==NA. Otherwise, we must put Gender as third parameter. – Nguyễn Văn Hưng Nov 05 '19 at 02:30

1 Answers1

0

So this is what I am trying to tell you, you need to use % in order to define the words you use as variables and not see them as plain text.

:: Usertype:I,C
set "Usertype=%~1"
set "Deltaval=%~2"
If /i "%Usertype%"=="C" If /i "%Usertype%"=="I" (
    set "Gender=NA"
    ) else (
    set "Gender=%~3"
   )
If "%Gender%"=="NA" goto :eof

Or you can do

:: Usertype:I,C
set "Usertype=%~1"
set "Deltaval=%~2"
If /i not "%Usertype%"=="C" If /i not "%Usertype%"=="I" do something

Gerhard
  • 22,678
  • 7
  • 27
  • 43