Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check empty value in cmd file?

Tags:

batch-file

cmd

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?

like image 320
ParagJ Avatar asked Apr 16 '12 12:04

ParagJ


People also ask

How do I echo a blank line in CMD?

To create a blank line in a batch file, add an open bracket or period immediately after the echo command with no space, as shown below. Adding @echo off at the beginning of the batch file turns off the echo and does not show each of the commands. @echo off echo There will be a blank line below.

What is Type nul in CMD?

In CMD help: type /? Displays the contents of a text file or files. TYPE [drive:][path]filename. and NUL I think is an empty file symbol. so, type NUL > introduction.js. reads the content of NUL "Which is an empty file", and write it to introduction.

What does %1 do in CMD?

When used in a command line, script, or batch file, %1 is used to represent a variable or matched string.

What does NUL mean in batch file?

nul 2>nul. means ignore output of command and ignore error messages.


2 Answers

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)
like image 116
mivk Avatar answered Oct 26 '22 18:10

mivk


The following should do it:

if [%PASSWORD%] NEQ [] GOTO MyLabel

For more info, see ss64.com.

like image 30
NPE Avatar answered Oct 26 '22 18:10

NPE