Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
Ed Dyreen
- Expert
- Posts: 1569
- Joined: 16 May 2011 08:21
- Location: Flanders(Belgium)
-
Contact:
#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:
#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:
#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:
#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=1Code: 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:
#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
-
orange_batch
- Expert
- Posts: 442
- Joined: 01 Aug 2010 17:13
- Location: Canadian Pacific
-
Contact:
#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:
#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:
#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:
#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:
#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...