Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Done Redirect to a variable

Oct
364
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.
 
Something like this:
Code:
set ttt=
do  xx in /p dir (set ttt=%ttt%^n%xx%)
 
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.)
 
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?
 
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.
 
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.
You can put it in the clipboard at first.
Code:
command > clip:
 
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.
 
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.
 
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
 
@EXECARRAY[array,command] : Execute the specified command and store the resulting lines in the specified array variable. The array must be one-dimensional.

Now all I have to find some examples on how to use arrays. All I found is how to declare, unset arrays and get infos on arrays. But not nothing in the help on how to actually make use of arrays. :-( Even something simple as accessing the first element is not described.
 
Now all I have to find some examples on how to use arrays. All I found is how to declare, unset arrays and get infos on arrays. But not nothing in the help on how to actually make use of arrays. :-( Even something simple as accessing the first element is not described.
You do have to look pretty hard to find an example of an array element being accessed! This is the only one I could find. It's in the help's What's new in version 10.

Code:
echo Name is %array1[0,0] and job is %array1[0,1]
 

Similar threads

Back
Top