Page 1 of 1

If string exists as variable

Posted: 17 Jun 2011 13:06
by Ed Dyreen
:?: Expecting an empty string:

Code: Select all

echo.is=!Z:\Test\Test.TST!_
pause
exit

result:
is=\Test\Test.TST_

Can't use if defined because of possible spaces.
Can't disable delayed.


Do I really need all this code to see if string exists as variable ?

Code: Select all

set "$?=^!$tokenexp:*:=^!" %$n1c%

if /i ["^!$?^!"] equ ["^!$tokenexp^!"] ( %$n1c%

   !@forTS! ( "^!$tokenexp^!" ) do set "$tokenexp=^!%%~^!^!" %$n1c%

) %$n1c%
:| Works

Re: If string exists as variable

Posted: 17 Jun 2011 16:42
by orange_batch
:?:

Code: Select all

if defined variable (echo:It's defined.) else echo:It's not defined.
set variable=1
if defined variable (echo:It's defined.) else echo:It's not defined.

Re: If string exists as variable

Posted: 17 Jun 2011 16:49
by Ed Dyreen
:?:

Code: Select all

if defined Z:\This\Dont Work.TMP (echo:It's defined.) else echo:It's not defined.
set variable=1
if defined Z:\This\Dont Work.TMP (echo:It's defined.) else echo:It's not defined.

Re: If string exists as variable

Posted: 17 Jun 2011 17:19
by orange_batch
Well, first remove the space from the variable, but you didn't change set variable=1

Code: Select all

@echo off
if defined Z:\This\Dont_Work.TMP (echo:It's defined.) else echo:It's not defined.
set Z:\This\Dont_Work.TMP=1
if defined Z:\This\Dont_Work.TMP (echo:It's defined.) else echo:It's not defined.
pause

Re: If string exists as variable

Posted: 17 Jun 2011 17:39
by Ed Dyreen

Code: Select all

set "$?=^!$tokenexp: =_^!" %$n1c%

if defined ^^^!$?^^^! ( %$n1c%

   !@forTS! ( "^!$tokenexp^!" ) do set "$tokenexp=^!%%~^!^!" %$n1c%

) %$n1c%

Although it failes on:
set "$var =something"
but that's ok because we shouldn't need to do that I guess.

Shorter indeed, Thanx orange :mrgreen:

Re: If string exists as variable

Posted: 17 Jun 2011 17:45
by orange_batch
if defined is a great way to toggle certain things like one-time error messages, doesn't even require delayed expansion.

For example, the wrong way...

Code: Select all

setlocal enabledelayedexpansion

for /l %%x in (1,1,10) do set /a var+=9999999999999999999||if "!failed!" NEQ "1" echo:Failed^^!&set failed=1

The right way...

Code: Select all

for /l %%x in (1,1,10) do set /a var+=9999999999999999999||if not defined failed echo:Failed!&set failed=1

Re: If string exists as variable

Posted: 18 Jun 2011 02:02
by Ed Dyreen
if defined is a great way

I assume you are implying it's faster .. ( and shorter )

Re: If string exists as variable

Posted: 18 Jun 2011 02:53
by orange_batch
No, I don't know about either, but it's the proper way.

Re: If string exists as variable

Posted: 18 Jun 2011 16:20
by Ed Dyreen

I only care about speed, size, robustness &good error handling.

What is proper, why is it proper ? Please explain. :?

Re: If string exists as variable

Posted: 18 Jun 2011 16:48
by orange_batch
You can do lots of things with the same result, the wrong way lol...

improper...

Code: Select all

for /f "delims=" %%a in ('echo:Hello World') do set var=%%a

proper...

Code: Select all

set var=Hello World