SET question

May 20, 2008
11,400
99
Syracuse, NY, USA
"I have a need to see the envvars whose names are exactly two characters long. Can I do that with SET alone?
 
May 24, 2010
855
0
Northlake, Il
Vince, I certainly could be wrong in that I don't think there's any "command" that will do that, but doing what you want to do doesn't take all that much code, either. Specifically,

Code:
@Echo Off
SetLocal
UnsetArray /Q EnvVars
SetArray EnvVars[64]
Echo >NUL: %@ExecArray[EnvVars,Set]
Set I=0
Do While %I LT %_ExecArray
   If %@Len[%@Left[%@Index[%EnvVars[%I],=],%EnvVars[%I]]] == %@If["%1" == "", 2,%1] ^
      @Echo %EnvVars[%I]
   Set /A I+=1
EndDo
UnsetArray /Q EnvVars
EndLocal
Quit 0

gets the job done, and it isn't all that long or complicated (and can obviously be made a into .btm file that can either be "Call"ed or "@ExedArray"'d, or invoked by an Alias if you want to run it from the command line, and the number of characters long that you want a variable name to be is a parameter that "defaults" to 2 in the above code to boot).

- Dan
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
4,461
88
Albuquerque, NM
prospero.unm.edu
Or, if you don't mind using an external:
Code:
set | grep "^.[^=]="

(Find lines beginning with any character, followed by any character other than the equals sign, followed by an equals sign.)
 
May 24, 2010
855
0
Northlake, Il
Charles,

I truly am laughing as I type this.:) Your answer is so correct and so obvious. However I will note that (somewhat sadly, I suppose, although I really don't feel that way; maybe because this has been the case for so many years now that it is no longer, and has not been for a very long time, an "emotional" issue for me - it would be kind of like having "feelings" about the fact that the sky is blue) I no longer use "tools" like "grep" and the like (unless I'm essentially "forced" to, which hasn't happened for many years) because I have used too many pattern-matching programs of one kind or another over the years (almost 40 years at this point); and in most cases the program I used was the only program I could use at that particular point in time for whatever reason such as operating system/availability/whatever so that I had to learn the syntax of its "regular expressions"; and with my developing bad memory I found remembering the particular syntax of the "patterns" of this particular program vs. the syntax of the "patterns" of all of its "alternatives" (and the fact that the individual syntax of the patterns for the various programs are typically very similar but not identical actually makes things far worse for me because trying to remember the small syntax differences between this particular program vs. that particular program became simply more than I could handle). For me, the simple "*" and "?" "wildcards" like in TCC and even plain old DOS are sufficient for my needs probably upwards of 95% of the time; and when they are not I could easily "come up with" an alternative, fairly simple like my suggested "solution" above is, way to do it that I can pretty much immediately fully understand. Yes, I can "read" the pattern in your "grep" (barely!), but trying to compose it would simply take too much time and effort for me and looking at a "help document" of some kind whereas, for me (and probably no one else) my solution was simple and straightforward and required almost no thought whatsoever - or, to say it another way, your "solution" is, without any doubt whatsoever, better than mine is in a "general" sense; but I will still continue to do things my way because they are, for me, simple and straightforward and require almost no thought. However I (will at least try to remember to :)) not "answer" any more questions like that on this bulletin board!

- Dan
 
May 20, 2008
3,515
4
Elkridge, MD, USA
It is documented that when SET is used to display currently defined environment variables, the only wildcard available is * - but why? IMHO the operation of the SET command in this mode ought to match how the ALIAS and FUNCTION commands perform the analogous tasks: the ? wildcard is interpreted as a match for a single character. "FUNCTION ??" displays all UDFs with 2-character names; "ALIAS ??" displays all aliases with 2-character names. There may exist a Windows API call to perform the current operation, but obviously it is not available for ALIAS or FUNCTION definition display. Whatever code is used by ALIAS and FUNCTION could be modified to enhance SET's display, and incidentally make it also function with registry variables (/D, /S, /U and /V).

SET with unsupported wildcards causes TCC 13.0.38 to close with neither GPF nor any other notification. Try "SET [AB]*" - TCC will disappear. V12 and V11 behave the same. V10 generates a GPF and triggers the "report to Microsoft" pop-up. V9, V8, V7 and V6 display "TCC: Not in environment "[ab]*"
and proceed to the prompt.
 
May 20, 2008
11,400
99
Syracuse, NY, USA
Well, I was hoping to do it with FFIND. But it appears that FFIND always quits at the first match. (Is there any way to override that?)
Yeah! Why does it do that? Stopping after the first match is supposed to be an option (/F).

Code:
e:\logs\mercury> set | ffind /e"^..=" /k /m
AE=3

(There should be more.)

This (below) works, avoiding externals. But I really wanted to do it with just SET.

Code:
e:\logs\mercury> set | do line in @CON ( if %@regex["^..=",%line] == 1 echo %line )
AE=3
AF=2
AL=3
(snip)
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
4,461
88
Albuquerque, NM
prospero.unm.edu
Hah!

Code:
set | ffind /e"^.[^=]=" /v /k /m

Regexps usually look like someone spilled a box of toothpicks. That one looks more like a Japanese "Hello Kitty" smiley.
 
May 20, 2008
11,400
99
Syracuse, NY, USA
Yeah! Why does FFIND do that? Stopping after the first match is supposed to be an option (/F).

It also stops after the first line when not in a pipe.

Code:
e:\logs\mercury> ffind /e"." /m /k MERCURYS.LOG
T 20100601 033308 4bb58e47 Connection from 74.71.55.217
 
Aug 2, 2011
258
4
Berlin, Germany
Code:
(system)  C:\...\TCCLE >set aa=1234
 
(system)  C:\...\TCCLE >set bb=dfjsdfgjsdfg
 
(system)  C:\...\TCCLE >set cc=****************
 
(system)  C:\...\TCCLE >set | for %x in (@con:) (set i=%@index["%x",=] %+ if %i eq 3 echo %@word["=",0,%x])
aa
bb
cc
OS
 
May 24, 2010
855
0
Northlake, Il
Frank,

Your solution is certainly very good and very clever (I've never had a known reason to use any of the "@Word..: functions in the past, so I know very little about them other than the fact that they exist) and hopefully Vince, who started this thread, can take advantage of it since it is clearly one of the best solutions suggested so far. However, since my "solution" works fine and since I can both remember it and easily understand it there is no incentive for me to change anything. But thank you (at least for Vince! :))!

- Dan
 
Aug 2, 2011
258
4
Berlin, Germany
Hi Dan,

@word is one of my favorite functions.
I often have to deal with logfiles or other machine-generated output.
And with @word you can comfortably test the contents whether it contains the desired keyword (at a certain position) you are looking for and pick it up for further usage.

regards
Frank
 
Similar threads
Thread starter Title Forum Replies Date
S SET /A question; HELP nits Support 8
V Set path permanently from within TCC? Support 3
S Debugger keeps breaking with no break point set. Support 1
Joe Caverly Set a batch variable (%1 - %n) when TCC is executing a batch file Support 5
R How to? Set default application in a special folder Support 4
A How to? Set up SMTP password in TCC-RT Support 1
vefatica WAD More SET /A woes Support 4
vefatica SET /A and multidimensional array. Support 1
M How to set Path variable for non-executable file extensions in 4DOS Support 8
D Need to set Take Command font size when switching to high DPI display Support 0
P "Set" command behavior - a bug? Support 3
M How to? Set the ribbon titles to mixed case Support 1
X How to set %_INIREAD result to variable Support 5
M WAD Set statements in () causes problems Support 8
M How to? How to set a default TITLE for newly opened tab? Support 2
M Fixed Set "VAR=VAL" format causes problems Support 12
vefatica SET /A var=expression=digits Support 3
cxxl Set encoding for batch file Support 4
B TCC 21.01.50 Problem with ALIAS /r and SET /r Support 2
vefatica SET /A var\=...? Support 10
dcantor Documentation SET /T: missing from heading Support 0
fpefpe Documentation Set /v Support 4
vefatica What's SET /Z? Support 1
R Fixed set /r has stopped working in build 35 Support 4
BeechComer Debugger: how to set %1 variable? Support 3
fpefpe How to? Global variable set /v Support 4
fpefpe Documentation Can the title line of tcmd be set? Support 4
J How to set working directory Support 10
Jay Sage Failure of ACTIVATE to Set a Window Position Support 3
rps Fixed Win "set /p" not working in V17 build 53 Support 3
vefatica SET /A "var%=N", TCC vs. CMD Support 0
J Set environment variable from stdout of command Support 3
B How to? Pre-set _tcfolder on startup when Folders is set Auto-Hide Support 6
Dan Glynhampton Fixed @version "force" flag not working, and unable to set separator Support 3
R WAD /= size ranges not set correctly Support 5
S How to? Set the timestamp of ZIP command built archive to match latest member Support 8
Mordachai Help! How to set parameters for script I'm debugging?! Support 6
Roedy SET= Support 2
A How to? Set up a log file for all command output Support 3
M Fixed character set in dir/copy Support 3
Phileosophos Fixed TakeCommand exits upon "set [" command Support 14
R How to set comspec to point to cmd.exe? Support 5
S WAD SET problem - array element initialization Support 11
C Wildcard expansion in FOR set Support 27
T Set 1% then Echo 1% Support 9
noahcoad copy /w to FTP does not set time stamp Support 1
M A request for a new feature for the "set" command... Support 5
cgunhouse Accessing variables set using SET /U or SET /S Support 3
D Force For to treat set of values as text, not files Support 3
vefatica Regular expressions in FOR's set? Support 10

Similar threads