I've had a look at various tips (ss64.com Command Redirection) and tricks but couldn't find an answer to my question:

Question

Is it possible to pipe the command I am going to execute into the same redirected output I will be creating?

Example with netstat

Input command

C:\Users\memyselfandi> netstat -obna >C:\temp\netstat_with_programs.txt

The actual command would be: netstat -obna >C:\temp\netstat_with_programs.txt

Output (The text file netstat_with_programs.txt)

This is the actual content of the netstat_with_programs.txt file. (The command is basically documenting itself in the output file.)

netstat -obna >C:\temp\netstat_with_programs.txt
Active connections

  Proto  Local Address          Remoteaddress          State             PID
  TCP    0.0.0.0:135            0.0.0.0:0              LISTENING         888
  RpcSs
 [svchost.exe]
  TCP    0.0.0.0:2382           0.0.0.0:0              LISTENING         1396
 [sqlbrowser.exe]
  TCP    0.0.0.0:3389           0.0.0.0:0              LISTENING         376
  TermService
 [svchost.exe]
....

Example with arp

Input command

C:\users\memyselfandi> arp -a >C:\temp\arp_output.txt

The actual command is: arp -a >C:\temp\arp_output.txt

Output (The contents of arp_output.txt)

This is the actual content of the arp_output.txt file. (The command is basically documenting itself in the output file.)

arp -a >C:\temp\arp_output.txt
Interface: 10.57.209.191 --- 0x5
  Internet Address      Physical Address      Type
  10.57.209.2           80-e0-1d-58-8a-50     dynamic 
  10.57.209.3           80-e0-1d-58-8b-88     dynamic 
  10.57.209.9           00-50-56-8d-91-fe     dynamic 
  10.57.209.10          00-50-56-8d-91-fe     dynamic 
  10.57.209.175         00-50-56-b5-44-16     dynamic 
  10.57.209.255         ff-ff-ff-ff-ff-ff     static  
  224.0.0.22            01-00-5e-00-00-16     static  
  224.0.0.252           01-00-5e-00-00-fc     static  
  230.0.0.1             01-00-5e-00-00-01     static  
  239.255.255.250       01-00-5e-7f-ff-fa     static  

So basically I would be documenting the command I am executing in the output I am creating.


Based on the possible solutions provided by @barlop in the comments, I went ahead and executed both commands:

With ECHO

echo netstat -obna >C:\temp\netstat_with_programs.txt && netstat -obna >>C:\temp\netstat_with_programs.txt

...this produced the following first line in the output file, which doesn't fully satisfy the requirements:

netstat -obna  
....

With %aaa% variable

set aaa=netstat -obna
echo (%aaa%>C:\temp\netstat_with_programs.txt) && (echo %aaa%|cmd)>>C:\temp\netstat_with_programs.txt

...this produces the same output, which doesn't fully meet the requirements:

netstat -obna  
...
  • Your English is completely ambiguous and thus not clear what you are asking. You should provide examples showing exactly what you mean. Show the contents of C:\temp\netstat_with_programs.txt before and after. Demonstrating exactly what you mean rather than trying to use your highly ambigious words. – barlop Dec 4 at 15:13
  • Also your title gives no indication of exactly what you are talking about – barlop Dec 4 at 15:16
  • I'd be interested to know which part of my question is ambiguous? The input is clearly stated in my question. netstat -obna >C:\temp\netstat_with_programs.txt. The output of my command is a text file which contains the contents outlined in the Output section of my question, which includes the command I executed. I don't think that my question is either ambiguous, nor syntactically, orthographically or typographically incorrect. I would appreciate if you could exactly explain which part of the question you are unable to grasp. – hot2use Dec 4 at 15:22
  • Because it is a > the file is previously non-existent. I am not adding (>>) to an existing file. – hot2use Dec 4 at 15:26
  • You have now corrected the unclear part of your question – barlop Dec 4 at 15:36

This is impossible because the shell, which takes the command and executes it, has no idea of what the arguments do that it is passing to the command.

For example in our theoretical rot13write C:\foobar.txt, is P:\sbbone.gkg

rot13write -qevir "P" -svyr "sbbone" rkg ".gkg"

That may tell rot13write to write to the drive P, the file foobar, and the extension txt. Or it may all be a joke and that path could have already been hard coded into the executable. You don't know, and neither does a shell.

So the shell can not echo to the file that a program is mysteriously writing to, because the shell doesn't know of that sufficiently; and, the program which does know how it is invoked is under no obligation to do anything with that data (like print the invocation command to the file it's outputting to). What you can do is

  1. Have the shell echo all its commands. Most shells support this.
  2. Have the program that you execute write to standard output (as it normally does) which the program will inherit from the shell that spawned it (this is how programs you call and the shell write to the same place -- the psuedo terminal)
  3. Redirect the shells standard output to destination file.

This will do everything you want except show the flag and output location in the command.

This looks like this in command prompt (I think)

cmd /c "netstat" > myOutput.txt | type myOutput.txt

And it looks like this in PowerShell,

powershell -command "Set-PSDebug -Trace 1; netstat" | tee myOutput.txt

If the problem is to display (or redirect to a file) the redirection symbol (>) with the ECHO command, then you just need to escape it. The escape symbol in this case would be ^:

ECHO netstat -obna ^>C:\temp\netstat_with_programs.txt >C:\temp\netstat_with_programs.txt
netstat -obna >>C:\temp\netstat_with_programs.txt

The second command in the above snippet is using >> instead of > because you want to add the output to the same file rather than to overwrite it.

  • I was just starting to wonder why it didn't work, until I realised that your solution is to execute two commands. Good solution, but it breaks the flow somewhat. – hot2use Dec 5 at 10:10

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.