Have you considered using Windows PowerShell instead of Cmd.exe? PowerShell is able to operate on objects, either .NET or COM, rather than being limited to plain text as is the case with Cmd.exe. Therefore you don't need to worry about output at all - you just code your classes and then use them directly from PowerShell. So for example, you can do this:
PS C:\WINDOWS\system32> $list = New-Object System.Collections.ArrayList
PS C:\WINDOWS\system32> $list.Add("First item")
0
PS C:\WINDOWS\system32> $list.Add("Second item")
1
PS C:\WINDOWS\system32> $list
First item
Second item
PS C:\WINDOWS\system32> $list.Count
2
PS C:\WINDOWS\system32>
As you can see, it is easy to create objects and call their methods and properties from PowerShell. This is one of the reasons that PowerShell is now Microsoft's primary management interface. The ability to work with objects from a command line interface is really very powerful and PowerShell is well worth a look.
However, if that is not a possibility, then with a little more work you can get what you want using cmd.exe by simply writing to the console.
In a console application there are always three streams available, known as stdin
, stdout
and stderr
('standard input', 'standard output' and 'standard error'). stdout
is normally connected to the display and stdin
is normally connected to the keyboard input, so by default a console application gets input from the keyboard and writes output to the display. However, the streams can be redirected to/from a file, or directly to another program.
The >
character redirects stdout
to a file. Thus,
dir > myfiles.txt
writes output to myfile.txt instead of the display.
Similarly, output can be 'piped' directly to another program, as in:
dir | more
The pipe (|
) character in the middle connects stdout
from the dir
command directly to stdin
of the more
command and thus data flows directly between the two programs. Note that dir is still just writing to the console, it doesn't necessarily know that its output has been redirected.
Using one of the above techniques, in combination with serialization, you could arrange to pass data between command line applications. You would have to arrange for your data to be serialized in a format that both programs can understand, and to make life easier using a format such as XML or JSON would be sensible, as it is easier to deserialize than plain text.