CON - CMD


Overview

The “CON” command in Windows CMD is used to represent the system console, manipulated primarily for inputting and displaying text data directly via the Command Prompt. It’s not a command to be executed but rather a keyword used to refer to the console device. Most effective in scripting and redirecting input to and output from Command Prompt.

Syntax

The use of CON in commands typically follows general redirection or pipe syntax in Windows CMD. Here are some common syntax patterns:

  • Redirecting output to console:
    command > CON
    
  • Getting input from the console:
    command < CON
    
  • Copying data to the console:
    COPY filename CON
    

Options/Flags

Since CON is a special file name used to access the console in Windows, it does not have its own distinct flags or options. When used, it inherits options from the command with which it is used (like COPY, TYPE, etc.).

Examples

  1. Display a File on the Console
    To display the contents of a text file directly onto the screen:

    TYPE myfile.txt > CON
    
  2. Copy Console Input to a File
    To copy text entered in the console into a file until Ctrl-Z (EOF) is sent:

    COPY CON myoutputfile.txt
    
  3. Using with Batch Scripts
    Echo a message directly to the console:

    ECHO Hello World! > CON
    

Common Issues

  • Infinite Loops: Using COPY CON without specifying an end-of-file (EOF) can lead to an infinite loop where the command waits for input indefinitely. Always ensure to terminate input with Ctrl-Z.
  • Misuse in Modern Contexts: CON, being an archaic concept from DOS days, can sometimes be misunderstood in the context of modern Windows. It’s important to understand it doesn’t ‘do’ anything by itself but facilitates I/O operations through the console.

Integration

The CON keyword can be used in scripts to interact with the user or to display outputs conditionally based on some logic:

@ECHO OFF
SET /P userInput="Enter your choice (Y/N): "
IF "%userInput%"=="Y" (ECHO "You chose yes" > CON) ELSE (ECHO "You chose no" > CON)

This script makes conditional use of CON to display messages based on user input directly.

  • ECHO: Used to display lines of text or variables to standard output or another file, similar to how CON can be targeted.
  • COPY: As seen in examples, used with CON to direct text files to the console or copy console input to files.
  • TYPE: Useful for displaying the contents of files, can be redirected to output to CON.

For more in-depth exploration of command line tools and effective use of the Windows Command Prompt, Microsoft’s official documentation and resources like Windows Command Line Documentation can be particularly useful.