CON

CON (short for Console) is an input/output DEVICE, not a command.

Copy from the console to a new file:

COPY CON Newfile.txt

Type from the console to a new file:

TYPE CON > Newfile.txt

Type from the console to append to a file, if the file does not already exist it will be created:

TYPE CON >> file.txt

Copy a file to the console:

COPY file.txt CON

The console accepts this input and acts like TYPE file.txt

Pass an Echo command as input to the console:

Echo some text >CON

Without context this may not look useful, but when used within a redirected block,
it allows one line to break out of the redirection and display on the screen:

  (
   Echo Start
   Echo Something happened >CON
   Echo end of demo
  ) >logfile.txt

After entering COPY CON or TYPE CON, type in the text required, note there is no prompt for this, then end the file by typing CTRL-Z, this will insert an EOF marker: ASCII 0x1a (SUB).

When using COPY the CTRL-Z can be placed anywhere, but when using TYPE it is important that the CTRL-Z is only entered at the beginning of a new line, otherwise it will just be saved into the file and you will have two EOF markers in the same file.

Most basic text editors will stop reading after the first EOF marker.

Some commands e.g. CLS, do not work as expected when redirected to CON and will output extra control characters.

To read text from the console in PowerShell use the following function:

function copycon {
[system.console]::in.readtoend()
}

con is a reserved filename.

A more advanced use of the console device is to redirect the console CON into FIND and then redirect the output from FIND into a file, this will make FIND discard any lines which do not contain the desired text. This can be useful for condensing large log files to return only the lines of interest.

Examples

Type some lines of text and redirect only the lines containing 'Jones' to a text file:

C:\> FIND /i "Jones" <con >logfile.txt
asdf
asdf
zz Jones 2
asdf
^Z

C:\> TYPE logfile.txt
zz Jones 2

“One types the correct incantation on a keyboard, and a display screen comes to life, showing things that never were nor could be” ~ Fred Brooks (The Mythical Man-Month, 1975)

Related commands

COPY - Copy one or more files to another location.
CHOICE - Accept keyboard input to a batch file.
Redirection - Spooling output to a file, piping input.
Equivalent bash command (Linux): read - Read a line from standard input.


 
Copyright © 1999-2024 SS64.com
Some rights reserved