ParagJ
ParagJ

Reputation: 1582

How to check empty value in cmd file?

I have the command line file below. I need to check for an empty value of a variable. I am not supplying any command line arguments.

@echo off
@set PASSWORD=
@set PORT=9001
@set command=START
if %PASSWORD% NEQ () GOTO MyLabel

:MyLabel
@set command=%command% -p%PASSWORD%

@set command=%command% -i%PORT%
@echo %command%

I tried several options such as comparing with empty parentheses (()), empty strings (""), but nothing seems to work. It gives me the following output when it runs:

() was unexpected at this time.

I am using Windows 7 x32. Can anyone please help?

Upvotes: 9

Views: 19774

Answers (2)

mivk
mivk

Reputation: 14824

Use IF DEFINED variable without the percent signs around variable.

Tested in XP (32bit) and Win7 x64:

SET PASSWORD=
IF DEFINED PASSWORD (echo PASSWORD = %PASSWORD%) ELSE (echo PASSWORD is empty or undefined)
IF DEFINED USERNAME (echo USERNAME = %USERNAME%) ELSE (echo USERNAME is empty or undefined)

Upvotes: 12

NPE
NPE

Reputation: 500267

The following should do it:

if [%PASSWORD%] NEQ [] GOTO MyLabel

For more info, see ss64.com.

Upvotes: 11

Related Questions