- Jul
- 81
- 0
Is there a simple way to populate an array with the contents of the environment *without* using a file or the Windows clipboard for temporary storage?
--
Peter
--
Peter
By registering with us, you'll be able to discuss, share and private message with other members of our community.
SignUp Now!v:\> setarray env[50]
v:\> echo %@execarray[env,set] > NUL
v:\> do i=0 to 3 ( echo %env[%i] )
ALLUSERSPROFILE=e:\Users\All Users
APPDATA=e:\Users\vefatica\Application Data
CommonProgramFiles=C:\Program Files\Common Files
COMPUTERNAME=ZZ
|Is there a simple way to populate an array with the contents of the environment *without* using a file or the Windows clipboard for temporary storage?
Code:v:\> setarray env[50] v:\> echo %@execarray[env,set] > NUL
An _ENVSIZE variable would be useful.
It's not elegant, but it is quick enough to be used on my machine where I have 40 environment variables defined:Thanks Vince. You're right, of course, but without _ENVSIZE, I have to use a file or CLIP: to determine the number of lines in my environment. I could over-estimate its length, but that's a workaround, not a solution.
===
Peter
@Echo Off
SetLocal
SetArray env[50]
Set Dummy=%@ExecArray[env,Set]
Set I=0
Do While "%env[%I]" != ""
Set I=%@Inc[%I]
EndDo
Set I=%@Dec[%I]
@Echo %I
UnSetArray *
EndLocal
Quit 0
v:\> set /a envsize=1 + %@execstr[set /x | echo %@lines[con]]
42
It's not elegant, but it is quick enough to be used on my machine where I have 40 environment variables defined:I do think this is better than the alternatives you suggested.Code:SetArray env[50] Set Dummy=%@ExecArray[env,Set] Set I=0 Do While "%env[%I]" != "" Set I=%@Inc[%I] EndDo Set I=%@Dec[%I] @Echo %I
Here's another workaround.
Code:v:\> set /a envsize=1 + %@execstr[set /x | echo %@lines[con]] 42
Even an internal/plugin _ENVSIZE would have to plow through the environment and
count the strings.
Right again. This is more along the lines of what I was looking for, using memory for the grunt work. The downside is that the plowing lasts over 2 seconds when run from the command line, and in my case returns 41 the first time and 42 every subsequent instance in the same TCC session. Matthew's "not elegant" workaround returns 41 every time, and in a blistering 0.05 seconds, from a btm file.
The array is a whole lot faster, as the code snippet below demonstrates. The first loop is an adaptation of Matthew's approach, the second loop is yours.Couldn't you just use the index option of @execstr and accomplish the desired task? Why do you need a separate array?
timer
setarray env[50]
echo %@execarray[env,set /x] >& nul
do i = 45 to 0 by -1
echo %@format[2,%i] %@left[140,%env[%i]]
if %i lt 45 .and. "%env[%i]" ne "" .and. "%env[%@inc[%i]]" eq "" echo ===========%=r%=n THE ENVIRONMENT CONTAINS %@inc[%i] VARIABLES %=r%=n============
enddo
unsetarray *
timer off
echo.
timer
do i = 45 to 0 by -1
echo %@format[2,%i] %@left[140,%@execstr[%i,set /x]]
if %i lt 45 .and. "%@execstr[%i,set /x]" ne "" .and. "%@execstr[%@inc[%i],set /x]" eq "" echo ===========%=r%=n THE ENVIRONMENT CONTAINS %@inc[%i] VARIABLES %=r%=n============
enddo
timer off
and accomplish> Quote:
>
> Originally Posted by samintz [image removed]
> Couldn't you just use the index option of @execstr
"%env[%@inc[%i]]" eq ""> the desired task? Why do you need a separate array?
>
> The array is a whole lot faster, as the code snippet below
> demonstrates. The first loop is an adaptation of Matthew's approach,
> the second loop is yours.
> Code:
> timer
> setarray env[50]
> echo %@execarray[env,set /x] >& nul
> do i = 45 to 0 by -1
> echo %@format[2,%i] %@left[140,%env[%i]]
> if %i lt 45 .and. "%env[%i]" ne "" .and.
> echo ===========%=r%=n THE ENVIRONMENT CONTAINS %@inc[%i] VARIABLES
"" .and. "%@execstr[%> %=r%=n===========> enddo
> unsetarray *
> timer off
>
> echo.
>
> timer
> do i = 45 to 0 by -1
> echo %@format[2,%i] %@left[140,%@execstr[%i,set /x]]
> if %i lt 45 .and. "%@execstr[%i,set /x]" ne
ENVIRONMENT> @inc[%i],set /x]" eq "" echo ===========%=r%=n THE
> CONTAINS %@inc[%i] VARIABLES %=r%=n===========> enddo
> timer off
> --
> Peter
Guilty, I didn't clean it up before posting it.Your examples are kind of special case.
Hand-in-glove, an array can't be declared without specifying the number of elements.The original comment was how to get the environment into an array
not how to count the number of entries.
You recall correctly. I prefer to use memory over disk read/write on the assumption that it's faster. I could use the clipboard, but I have had clipboard contents changed by another app running simultaneously, so now I use the clipboard sparingly.IIRC, you did not want to use a file or the clipboard.
I don't see that, the set command is run before _envcnt is filled, so its value is accurate. Although the memory reserved for the array is overstated, your example works.This example gives you both the count and the environment loaded into an array.
The downside to the above is that _envcnt itself gets counted.Code:setarray env[1000] set _envcnt=%@execstr[set |! echo %@filearray[env,con]]
On Mon, 06 Jun 2011 12:17:23 -0400, Steve Fabian <> wrote:
|Of course, the new _ENVCOUNT internal variable in Vince's new 4utils.dll plugin makes the process unnecessary.
A user reported to me privately that in TCC/x64, "ECHO %_ENVCOUNT" causes a
crash. Anyone else?