If string exists as variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

If string exists as variable

#1 Post by Ed Dyreen » 17 Jun 2011 13:06

:?: 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
Last edited by Ed Dyreen on 17 Jun 2011 16:57, edited 2 times in total.

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: If string exists as variable

#2 Post by orange_batch » 17 Jun 2011 16:42

:?:

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.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: If string exists as variable

#3 Post by Ed Dyreen » 17 Jun 2011 16:49

:?:

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.

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: If string exists as variable

#4 Post by orange_batch » 17 Jun 2011 17:19

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

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: If string exists as variable

#5 Post by Ed Dyreen » 17 Jun 2011 17:39

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:

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: If string exists as variable

#6 Post by orange_batch » 17 Jun 2011 17:45

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

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: If string exists as variable

#7 Post by Ed Dyreen » 18 Jun 2011 02:02

if defined is a great way

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

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: If string exists as variable

#8 Post by orange_batch » 18 Jun 2011 02:53

No, I don't know about either, but it's the proper way.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: If string exists as variable

#9 Post by Ed Dyreen » 18 Jun 2011 16:20


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

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

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: If string exists as variable

#10 Post by orange_batch » 18 Jun 2011 16:48

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

Post Reply