Live Chat & Podcast at 1:00PM Eastern on Sunday!
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
DOS/Other
Tag Cloud
access acer asus bios bsod computer crash desktop driver drivers error ethernet excel freeze gaming graphics hard drive hardware hdmi internet laptop lcd malware memory monitor motherboard network operating system printer problem ram registry router slow software sound trojan ubuntu 11.10 uninstall usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless
Search
Search for:
Tech Support Guy Forums > Operating Systems > DOS/Other >
Solved: Empty environment variable expansion fails

Reply  
Thread Tools
rettug's Avatar
Junior Member with 2 posts.
 
Join Date: Nov 2009
Experience: Advanced
12-Nov-2009, 05:28 AM #1
Solved: Empty environment variable expansion fails
Ok, this one's killing me. I've used expressions like

if x%VALUE% == x

to test for empty (really, undefined) environment variables for years. It's suddenly broken. That is, even from the command line

set VALUE=
if x%VALUE% == x echo Empty

displays nothing. You can see 'why' with an echo:

set VALUE=
echo %VALUE%

displays the string

%VALUE%

Now I know I can use

if not DEFINED VALUE

instead, but that prevents me from using %VALUE% for both actual and null substrings, which is really handy.

Note that batch parameter substitution still works, e.g.

call :sub
goto :eof
:sub
if x%1 == x echo Empty
goto :eof

displays Empty as usual.

Any ideas? I'm running XP Pro SP2. CMD reports the version as Microsoft Windows XP [Version 5.1.2600]
TheOutcaste's Avatar
Computer Specs
Distinguished Member with 9,048 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
13-Nov-2009, 01:02 AM #2
That's normal behavior when typing those commands at a command prompt. Been that way since NT 4.0, if not earlier.

At the Command Line, if a variable is undefined, Echo will echo exactly what you typed, it doesn't substitute a null value for the undefined variable. Same with If.
It only works if the statements are executed in a batch file.

If it's not working when executed from within a batch file, I haven't a clue, as it still works for me. Assuming you've rebooted, make sure you are opening the real cmd.exe by typing c:\Windows\System32\cmd.exe into the run box.
Run SFC /SCANNOW to check for corrupted system files.

This file works as expected for me, just tested on NT4.0, Win2K, XP-SP0, XP-SP2, XP-SP3, Win7:
Code:
set VALUE=
echo %VALUE%
if x%VALUE% == x echo Empty
Code:
E:\Scripts>EVTest.cmd

E:\Scripts>set VALUE=

E:\Scripts>echo
ECHO is on.

E:\Scripts>if x == x echo Empty
Empty

E:\Scripts>
Since value is undefined, the echo statement simply echos the current echo setting.
Also tested with Command Extensions disabled.

If your variable contains spaces you will get an error:
Code:
Set VALUE=TEST DATA
If x%VALUE%==x Echo Empty
Will tell you DATA was unexpected at this time.
You should use this format instead:
Code:
Set VALUE=TEST DATA
If "%VALUE%"=="" Echo Empty
__________________
Microsoft MVP - Windows Expert - Consumer
Of course I know all the answers ; I just don't always match the answers to the right questions

rettug's Avatar
Junior Member with 2 posts.
 
Join Date: Nov 2009
Experience: Advanced
13-Nov-2009, 04:20 AM #3
Right you are. A different bug led me down the path of testing the undefined variable from the command line, and once I saw it echo the string - I never knew it did that! - I assumed it would do the same from within the batch file.

Thanks for your work on this, and your quick & knowledegeable response!
Reply

Tags
environment variable

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.
Thread Tools



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 01:11 AM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.