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?
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.
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.
When used in a command line, script, or batch file, %1 is used to represent a variable or matched string.
nul 2>nul. means ignore output of command and ignore error messages.
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)
The following should do it:
if [%PASSWORD%] NEQ [] GOTO MyLabel
For more info, see ss64.com.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With