[batch] test for empty variable

May 22, 2010 at 20:31:18
Specs: Windows 7
if "%var%"=="" echo empty

nothing

echo %var% >gives> %var%

which is probably why. any ideas people?

.


See More: [batch] test for empty variable

Report •

#1
May 22, 2010 at 20:55:06
my quite non-brilliant analysis is:
1) assuming you have set a value for %var%
echoing that value to that value will result in a file, not a variable.
f/e: set %var%=im_ok
echo %var% > gives > %var%
sends the string, "im_ok" to the line one of the file "im_ok", and not to
a variable %var%.
2) Also, the intermediate pipe doesn't really
do anything ( > gives > ). That file is not created, or not as a
persistant entity.
ultimately, the variable's value is reflected in a file having the
name that is the variable's value. The value does not wind up
back in the variable. It's in the file that has the variable's contents as its name.
(f/e: you can: set aa=TEST
but you can't: echo TEST > %aa% except %aa% is a file not a var. No reason to anyway.)
Maybe post a synopsis of what you're trying to accomplish might get some real help as opposed to this non-help. :-)

ps: sorry, just re-read my post.
It IS saturday night, I HAVE consumed alc.
beverages. apologies! Lol!


Report •

#2
May 22, 2010 at 20:57:59
AFAIK an environment variable cannot be set to nothingness, it must contain at least one character or white space.

If you use:
Set var= Where one space follows the = you can test by using

If "%var%"==" " echo something

If you want to test for the existence of an environment variable use

If defined %var% echo something
or
If Not defined %var% echo something

Hope this helps.


Report •

#3
May 22, 2010 at 21:05:58
The first one should work, if %var% contains anything at all, including a space if will fail, especially if it contains double quotes.

Perhaps:

if not defined var echo empty

Again if it contains anything at all......


I have no idea what you are trying to do with:

echo %var% >gives> %var%

It displays some interesting behaviour but nothing particularly useful.

[edit]
Gee I'm slow, beaten by minutes...

@Wahine
If you want to test a variable with "if [not] defined" you need to leave out the %'s or !'s otherwise it will check if the contents of the variable is itself a defined varable.

To kill a variable you simply put nothing after the "=", "set var="

set var=

[/edit]


Report •

Related Solutions

#4
May 22, 2010 at 21:59:53
@echo off
echo.
if not defined %1 ( set /p drives=Enter drive to format [c, d, e, cd, cde]: ) else ( set drives=%1 )
rem if "%1"=="" ( set /p drives=Enter drive to format [c, d, e, cd, cde]: ) else ( set drives=%1 )
echo.
echo drive %drives%
if "%drives%"=="c" purandc c:\ -fg -s2c
if "%drives%"=="d" purandc d:\ -fg -s2c
if "%drives%"=="e" purandc e:\ -fg -s2c
if "%drives%"=="cd" purandc c:\ d:\ -fg -s2c
if "%drives%"=="cde" purandc c:\ d:\ e:\ -fg -s2c
echo end

the rem'd out part actually works.. but... if someone can test for me, set drives=%1 this doesn't seem to work..

actually it does
both if and else produces the same result in echo drive %drives%

but the else part doesn't get pass on to the next if statement

i dont get it.

oh yea i dont know how to make the if not defined %1 or 1 or whatever work.. haha
.


Report •

#5
May 22, 2010 at 22:21:03
If not defined won't work for parameters or for variables, only environment variables.

I added the "/i" switch to the if statement to make them case insensitive as well as a few other small changes:

@echo off
echo.
:input
if "%~1"=="" (
     set drives=
     set /p drives=Enter drive to format [c, d, e, cd, cde]:
     if not defined drives goto input
) else (
    set drives="%~1"
)
set drives="%drives:"=%"
if %drives%=="" goto input
echo.
echo drive %drives%
if /I %drives%=="c" purandc c:\ -fg -s2c
if /I %drives%=="d" purandc d:\ -fg -s2c
if /I %drives%=="e" purandc e:\ -fg -s2c
if /I %drives%=="cd" purandc c:\ d:\ -fg -s2c
if /I %drives%=="cde" purandc c:\ d:\ e:\ -fg -s2c
echo end


Report •

#6
May 23, 2010 at 03:30:44
thanks for all the help. got it working now

.


Report •

Ask Question