How to verify ENVs with brackets within?

Microsoft Windows v.3.11 for workgroups...
August 3, 2010 at 16:22:54
Specs: DOS, 286
HI!

First of all I do not know if this is the right place in the forum to post this request :P

I have a small problem with my batch-file.
in practice, I create an ENV variable, in this variable may be brackets, such as "SET MYENV=mynameis(Turrican). Well, in my batch I need to check if MYENV is empty, then I do:
IF "%MYENV%"=="" echo myenv is empty!
This works correctly under DOS/WINDOWS9X, but does NOT work under Windows NT/2000/XP.
I need this batch works on either the OS.
I think the problem is that Windows NT Cmd.exe interprets the brackets as sub-commands and then returns an error.

Is there a way to force this check character by character, so that it works on WinNT/2k/XP ?

thanks


See More: How to verify ENVs with brackets within?

Report •

#1
August 4, 2010 at 04:04:00
I tested your environment variable under plain DOS and NT based Windows (NT/2K/XP/Vista) and the script worked fine.

Which error do you get from the interpreter?

Anyway if no solution exists, you may detect the NT family by coding

uf exist C:\ntldr set OSType=NT

then

if "%OStype%"=="NT" if not defined MYENV echo. MYENV is EMPTY
if "%OStype%"=="" if "%MYENV%"=="" echo. MYENV is EMPTY


Report •

#2
August 4, 2010 at 04:38:28
Sorry, I missed a particular in my ENV example. In fact my ENV should contains spaces and is surrounded by quotation marks, such as SET MYENV="my name is (Turrican)".
In this way the IF command does not work correctly under WinNT, the error I get is "name not expected."

try this batch file:

@echo off
set MYENV="my name is(Turrican)"
if not "%MYENV%"=="" echo MYENV not empty!
if "%MYENV%"=="" echo MYENV empty!
::set MYENV=

anyway, your solution (if defined) is a bit spartan, but I did not know this parameter of the IF command, well, I will use it in other situations in the future! thanks =)
(PS: By the way, why are you using ECHO. instead of ECHO without the dot point?)

Anyway, never mind, I found how to fix it:
I just used brackets instead of parentheses.
I believe this is the best, you have some other idea about it?

@echo off
set MYENV="my name is(Turrican)"
if not [%MYENV%]==[] echo MYENV not empty!
if [%MYENV%]==[] echo MYENV empty!
::set MYENV=

many thanks for your help,
superturrican2


Report •

#3
August 4, 2010 at 06:57:10
As Umberto Eco wrote in his novel "The Name of the Rose" things are defined by their NAME. You said "parenthesis" while they are "double quotes" so anything has a meaning now.

Your solution is the best I can figure out as the problem arises just from the twice double quotes.

I use ECHO. as it behaves fine even if the string to be displayed vanishes for any reason, e.g. ECHO.%MYENV% and MYENV is undefined.


Report •

Related Solutions

#4
August 4, 2010 at 09:05:06
In W2K OS is set by default.

BTW: if not defined won't fly in DOS.


=====================================
Helping others achieve escape felicity

M2


Report •

#5
August 4, 2010 at 10:56:53
Umberto Eco is right =)

Report •

#6
August 4, 2010 at 13:06:13
@M2

Right, but it is better to not relay upon the OS variable when coding a multi-environment compatible script since the value is sometime unpredictable slightly varying from release to release.

Checking for the presence of NTLDR is a simple way to detect if the environment is DOS or NT based and so select the appropriate commands.


Report •

#7
August 4, 2010 at 16:01:01
EDIT: I just used brackets instead of parentheses. --> I just used brackets instead of double quotes.

sorry for my mistake, IVO, just now I notice the error :P

btw, I think next Windows release should not be NT-based, but a new kernel will be prepared. So the %OS% env may contain a new string.


Report •

#8
August 5, 2010 at 01:04:43
LOL When we get to the year 2929 maybe the winders kernel will be unix.

Hi IVO,

Right, as usual. In my case, I only boot 32bit DOS or NT5 W2K so I know the OS string is either void or OS=Windows_NT.

In my case it wouldn't help to check for NTLDR because it's there all the time.

OT: Did you see the "Name of the Rose" movie?


=====================================
Helping others achieve escape felicity

M2


Report •

#9
August 5, 2010 at 08:07:58
Hi M2,

of course, I'm a movie fan and I like Sean Connery not just playing James Bond but different characters too.

About Eco's novel I prefer the book as the movie misses the inner flavour of the amazing story.

M2, you will receive a mail from a Computing.net friend about two batches. I redirected him to you, please give him your valuable help if you can.

Thanks in advance.


Report •

Ask Question