Done Redirect to a variable

Oct 18, 2009
351
17
It may already be possible to do this, but if not--redirect command output to a user-defined variable instead of to a file, including any CR/LF's:

MyVar would be a variable, not the name of a file.

DIR > MyVar

Echo %MyVar

[C:\Program Files\JPSoft\TCMD18_x64\Styles]dir

Volume in drive C is MyPC_C Serial number is 1234:abcd
Directory of C:\Program Files\JPSoft\TCMD18_x64\Styles\*

11/20/2016 23:07 <DIR> .
11/20/2016 23:07 <DIR> ..
5/06/2014 17:20 1,436,672 Office2007.dll
5/06/2014 17:19 1,445,376 Office2010.dll
3,444,736 bytes in 2 files and 2 dirs 3,452,928 bytes allocated
14,538,584,064 bytes free


Or possibly instead, redirect output to an array variable, with each line in a separate array node.

... Maybe both.
 
Oct 18, 2009
351
17
I don't think I understand, but it looks like that will be populating an array variable. That does require using SETARRAY.

(I don't use array variables because I mainly write for work and until recently I was only using TCC/LE and pretty much everything I work with involves csv-style data.)
 
May 20, 2008
10,634
81
Syracuse, NY, USA
I don't think I understand, but it looks like that will be populating an array variable.
Each time through the DO loop (each line of output if DIR) it sets ttt to

the current value of ttt PLUS a newline PLUS the next line of DIR's output.

It seems peculiar to put multiple lines in an environment variable. What do you then do with such a variable?
 
Oct 18, 2009
351
17
Each time through the DO loop (each line of output if DIR) it sets ttt to

the current value of ttt PLUS a newline PLUS the next line of DIR's output.

It seems peculiar to put multiple lines in an environment variable. What do you then do with such a variable?
Most likely, save it to the clipboard for pasting into some other program, rather than having to output to a file, have the other program find the file, load it, then delete the file.

I'm not really thinking so much specifically about DIR, that was just an easy command to use as an example.
 
Apr 2, 2011
1,281
3
53
North Carolina, USA
Code:
Navigation:  TCC > Variables & Functions > Functions >
@EXECARRAY   
    

@EXECARRAY[array,command] : Execute the specified command and store the resulting lines in the specified array variable. The array must be one-dimensional.

 

You must define the array before running @EXECARRAY. For example:

 

setarray aresult[10]

echo %@execarray[aresult,dir /u] >& nul

 

@EXECARRAY will read the number of lines specified in the array size definition, or the number of lines in the command output (whichever is less). @EXECARRAY returns the return value of the command.

 

The number of lines stored in the array is saved in the _EXECARRAY internal variable.
 
Oct 18, 2009
351
17
Code:
Navigation:  TCC > Variables & Functions > Functions >
@EXECARRAY  
   

@EXECARRAY[array,command] : Execute the specified command and store the resulting lines in the specified array variable. The array must be one-dimensional.

-- snip --
Thanks--I thought there probably was something like that because it would be so useful.

Regarding do xx in /p dir .... I haven't been able to get it to work completely but the problem seems to be I don't have the coding for "new line" exactly correct and I haven't tried variations yet.

And now that I think about it, it probably would be better to Set a "newline" variable and use the variable rather than hard-coding the sequence.
 
May 20, 2008
10,634
81
Syracuse, NY, USA
Using ^n is OK, but getting the newlines where they belong is a bit tricky. This seemed to work.

Code:
v:\> unset output & do l in /p uptime ( set output=%[output]%@if[%_do_loop==1,,^n]%l )

v:\> echo %output

Uptime  2 days 17 hours 57 minutes 18 seconds
Boot    2018-09-17 21:22:24
Logon   2018-09-19 12:36:12