14.01.33 unset doesn't complain about undefined variables

May 31, 2008
382
2
As the help file says, option /Q keeps UNSET from complaining about undefined variables, and it used to be that - without /Q - unset did complain about such variables. Apparently now it doesn't complain anymore, but it should.
Code:
C:\>type x.btm
set x=
set x
unset x
C:\>x.btm
set x=
set x
TCC: C:\temp\maxleo\x.btm [2] Not in environment "x*"
unset x

C:\>ver

TCC 14.01.33 Windows 7 [Version 6.1.7601]

C:\>
 
As the help file says, option /Q keeps UNSET from complaining about undefined variables, and it used to be that - without /Q - unset did complain about such variables. Apparently now it doesn't complain anymore, but it should.
Code:
C:\>type x.btm
set x=
set x
unset x
C:\>x.btm
set x=
set x
TCC: C:\temp\maxleo\x.btm [2] Not in environment "x*"
unset x

C:\>ver

TCC 14.01.33 Windows 7 [Version 6.1.7601]

C:\>
It works correctly here. Are you sure you don't have an alias for UNSET?

--
Howard
 
May 20, 2008
11,414
99
Syracuse, NY, USA
Here, none of v11, v12, v13, v14 produce an error message on Win7. This may have something to do with Windows versions. Under Win7, a call to SetEnvironmentVariable(L"variable", NULL) succeeds when "variable" doesn't exist, and also in that case GetLastError() returns 0. That's OK with me.
 
May 20, 2008
3,515
4
Elkridge, MD, USA
Here, none of v11, v12, v13, v14 produce an error message on Win7. This may have something to do with Windows versions. Under Win7, a call to SetEnvironmentVariable(L"variable", NULL) succeeds when "variable" doesn't exist, and also in that case GetLastError() returns 0. That's OK with me.
I agree with Vince - the purpose of UNSET is to remove a single variable or a whole set of variables. IMHO it is not an error to remove a variable that did not exist - it means that your goal has already been accomplished. For my own use, I have always aliased UNSET=*UNSET /Q i.e. the new operation.

What would be the purpose of checking the existence of a variable using UNSET anyway? If its existence before UNSET is relevant, it should be checked with IF DEFINED, a test which allows using the value before destroying it (though in some cases all that matters whether or not the variable is defined, not its value).

Rex:
From the performance standpoint, when one uses only the existence or absence of a variable (i.e., using IF DEFINED test), what is the best value to set the variable to? I realize that the difference between different values is minimal, unless it is done millions of times.
--
Steve
 
May 20, 2008
11,414
99
Syracuse, NY, USA
From the performance standpoint, when one uses only the existence or absence of a variable (i.e., using IF DEFINED test), what is the best value to set the variable to? I realize that the difference between different values is minimal, unless it is done millions of times.

No doubt, a one-character value would be best. If the value is being fetched, it must be copied. And even if it's not to be fetched, (GetEnvironmentVariable() with a NULL buffer) it's length must be determined. That said, it probably takes under a microsecond to do either in most cases. And from a practical point of view if you're only testing for existence, "1", "true", "on", "yes", or any of myriad other values might serve to remind the batch programmer what he's doing.

Actually ... SetEnvironmentVariable() allows the value L"" (empty string). A variable with no value shows up if you GetEnvironmentStrings(). If such a variable is set, GetEnvironmentVariable() will return 0 (the length of the data, typically meaning failure) but GetLastError() will be 0 (ERROR_OK). OTOH if it's not set, GetEnvironmentVariable() will return 0, and GetLastError() will be 203 ("The system could not find the environment option that was entered."). I suppose a shell could allow variables with no value, and "IF DEFINED" could be made to work correctly with them. But why ... to save one keystroke and (perhaps) a nanosecond? I wonder what "IF DEFINED" would say if a plugin set a variable with an empty string as value.
 

rconn

Administrator
Staff member
May 14, 2008
12,363
150
As the help file says, option /Q keeps UNSET from complaining about undefined variables, and it used to be that - without /Q - unset did complain about such variables. Apparently now it doesn't complain anymore, but it should.

Neither does CMD.EXE.

Since Microsoft changed the behavior in Windows 7, the only way for TCC to do this would be to try to read the variable first before attempting to delete it. Which would slow everything down measurably, and result in error messages being returned in scripts that do not display errors with CMD.
 
Similar threads
Thread starter Title Forum Replies Date
samintz WAD Cannot unset .py[co] Support 2
dcantor Documentation Documentation error in UNSET Support 0
CWBillow UNSET a Variable Support 7
Stefano Piccardi 13.0.0.21 unset /V foo error Support 1
J Strange error: unset /s Support 14
J Issue with unset? Support 1
vefatica Setting array variable ignores leading spaces (and @execarray doesn't) Support 4
MickeyF Why doesn't my python program get executed without specifying its path? Support 2
gworley Virtual network drive filename completion doesn’t get directories Support 0
vefatica Win10's ANSI doesn't like 2>NUL Support 10
vefatica Win10, alternative screen buffer, cursor doesn't blink Support 11
M Trying to use ffind with a @file.lst doesn't work Support 4
Charles Dye Ctrl-Shift-Up doesn't update directory history Support 1
gschizas Take Command (Environment) doesn't show v2 ANSI colors Support 30
vefatica Fixed JOBS /K doesn't work Support 2
E Fixed SHRALIAS doesn't work / SHRALIAS.EXE is missing in v20.11.35 Support 2
D WAD Reboot doesn't shutdown or restart Support 13
M Documentation Q.Search doesn't support selection? Support 1
R Fixed 7ZIP Compression Method Option Doesn't Work Support 1
T Regex engine doesn't recognize native DOS line endings Support 2
E Fixed DEDUPE doesn't handle filenames with whitespace properly when symlinking Support 1
D Docking doesn't persist Support 5
E WAD DESCRIBE under TCC 19 doesn't work with diacritics Support 14
C VIEW PRINT /Wrap doesn't work Support 0
D CD doesn't always find everything Support 8
M View doesn't recognize Ctrl+End and Ctrl+Pos1 Support 2
M Ctrl-End doesn't work. DelToEnd is not executed Support 13
B Fixed FFIND doesn't find last character in file without CR/LF at EOF Support 1
G .BTM association doesn't work anymore Support 1
jadaml Documentation Is COPY realy doesn't work for any HTTP and FTP? Support 3
D do /s doesn't recurse Support 1
D WAD Alt-Left doesn't work, but Alt-Shift-Left does Support 1
I How to? Bat Works but BTM Doesn't Support 6
B %@lines doesn't count last line without cr/lf at eof Support 1
williamleara WAD ribbon doesn't remember expand/collapse setting Support 3
vefatica Why doesn't "IDE %_ININAME" work? Support 4
M How to? /Elevated doesn't do it for me... Support 8
D Why doesn't ffind find directory? Support 3
Phileosophos TPIPE doesn't work at all with v16 Support 3
D ejectmedia doesn't work in v16 Support 2
Phileosophos Fixed Register for all users doesn't register for all users Support 10
T "Checking for a newer version" doesn't complete Support 18
vefatica Doesn't IDE auto-indent? Support 3
C FFind /w works but FFind /= doesn't? Support 2
noahcoad WAD cmd.exe under tcc doesn't find apps Support 4
vefatica Fixed TPIPE doesn't /SPLIT at characters Support 0
M Site doesn't recognise cookies Support 0
D What's New doesn't mention copydir movedir Support 25
D TIMER OFF doesn't work Support 21
M @Files sure doesn't work the way I would have assumed it did! Support 4

Similar threads