Fall Sale! Code FALL2024 takes 25% OFF our Pro Plugins & Books »
Web Dev + WordPress + Security

Fun with the DOS Command Prompt

Note: This article assumes a basic familiarity with the DOS command prompt, and is somewhat of a continuation of our previous article on DOS, Basic DOS Commands. Here, we are exploring a few of the more interesting commands available via the DOS command prompt. For a more complete reference please consult the Windows Help file (Windows XP) by entering the following command at the command-line prompt (or via the Start > Run... dialogue box):

hh.exe ms-its:C:\WINDOWS\Help\ntcmds.chm::/ntcmds.htm

Also, for a brief overview of essential DOS commands, characters, devices, and variables, check out DOS Fundamentals.

Display file contents in DOS window

There are two ways to display the contents of a file through DOS. Both of these methods employ the con (i.e., console, which consists of the keyboard and monitor) device. Here we are displaying the contents of the file atari.exe, which is located within the c:\files\docs\ directory:

(using the copy command)

c:\> copy files\docs\atari.exe con

(using the type command)

c:\> type files\docs\atari.exe con

Create text files in DOS

While working in DOS, it is possible to create text documents easily, on the fly, and without any external text editor. Here, we command DOS to record all keystrokes into a file called note.txt, which we will create in the c:\files\docs\ directory:

(to create the text file and begin recording keystrokes)

c:\> copy con files\docs\note.txt

(to create a new line within the file)

press [ENTER]

(to stop recording keystrokes)

press [F6] or [CTRL+Z and then ENTER]

Add single lines of text to a file

To quickly append lines of text to any file, use the output redirection command as follows:

c:\> echo sometext >> somefile.txt

This will append the text “sometext” to the file somefile.txt. If the file exists, the line of text will be added to the end of the file. If the file does not exist, it will be created. To overwrite the contents of the file, use this instead:

c:\> echo sometext > somefile.txt

Fun with the nul device

The nul device is like a black hole into which dumped data will simply disappear. You can copy or pipe files or keystrokes into it and they will vanish forever. Thus there are a few fun things that may be done with the nul device.

Here is a way to prevent inexperienced users from using your DOS machine while you are away on break. The following method will create a situation in which a DOS prompt exists, but fails to execute anything. The user will be able to type commands, press enter, and scratch their heads while nothing happens and everything they type is sent directly to the nul hole:

(first type this to initiate the process)

c:\> copy con nul [ENTER]

(then type this to emulate your DOS prompt)

C:\> [DO NOT PRESS ENTER]

(then, to exit the process)

[CTRL+C] or [CTRL+BACK] or [CTRL+Z and then ENTER]

Using the nul device, you can also copy files into nowhere:

c:\> copy somefile.txt nul

..or even pipe nothing into nowhere (yes!):

c:\> copy somefile.txt nul > nul

Print a file through DOS

Although I have as of yet been able to get this method to work, it is theoretically possible to accomplish. This command should send the contents of some file to the printer device (prn):

c:\> copy somefile.txt prn

Convert the output of one process into the input of another process

Piping (via the pipe | symbol) is the process in DOS whereby output data becomes input data. Here, sending the contents of script.js to the system debug.exe file:

c:\> type script.js | c:\programs\debug.exe

This same command could also be written as:

c:\> programs\debug.exe < script.js

Send directory listing to a printer or file

This is a very useful method of printing a list of directory contents:

(send directory listing to the printer)

c:\> dir > prn

(send directory listing to somefile.txt)

c:\> dir > somefile.txt

This is a handy way of printing a list of, say, all .mp3 files in a specific directory. Here, we use the wildcard * character to select all .mp3 files in the music directory. The list is then printed to the file musiclist.txt, which is located in the c:\files\docs\ directory:

c:\music\> dir *.mp3 > c:\files\docs\musiclist.txt

Roll your own boot log

To create a log file that records system activity status at every reboot, append the following to your system’s autoexec.bat:

echo Another Reboot! >> c:\reboot.log
mode.com >> c:\reboot.log
systeminfo.exe >> c:\reboot.log

Now, upon every reboot (or every time the autoexec.bat file is executed), system status data will be appended to c:\reboot.log, which will be created upon initial execution. Here, the appended data is provided by two functions, mode.com, which displays the status of various system devices (e.g., LPT1, COM1, COM2, CON, etc.), and systeminfo.exe, which displays various system information. Of course, these functions are merely random, demonstrative examples. You would, of course, want to customize your own set of logged data according to your system specifics and diagnostic needs.

Customize the DOS command prompt

The default command prompt specifies the current directory, followed by a “greater-than” (>) symbol and a blinking underscore. There are many options for changing the default prompt, including system variables, normal characters, and preset codes. To begin, check out the list of parameters by entering prompt /? at the command prompt. Note that each variable must be preceded by a dollar symbol ($). Here are a few examples for changing the prompt:

Display current directory followed by a greater-than symbol:

prompt $p$g

Display the time after the default prompt:

prompt $p$g$t

Display the computer name followed by the username:

prompt [%computername%] [%username%] $g

Reset the prompt to the default:

prompt

To modify the color of the DOS prompt and screen color, type color followed by two hexidecimal color values of your choice. The first digit sets the background color while the second sets the text color.

Change prompt color to matrix green and screen color to black:

color 0a

Change colors to red on grey:

color 84

Here are a few other basic color choices:

  • 0 = Black
  • 1 = Blue
  • 2 = Green
  • 3 = Cyan
  • 4 = Red
  • 5 = Magenta
  • 6 = Yellow
  • 7 = White
  • 8 = Grey
  • 9 = Bright Blue
  • A = Bright Green
  • B = Bright Cyan
  • C = Bright Red
  • D = Bright Magenta
  • E = Bright Yellow
  • F = Bright White

Note: users may also change colors and other properties via the prompt shortcut itself. Right-click the shortcut and select properties. Have a look around — lots of choices!

Related resources

About the Author
Jeff Starr = Web Developer. Security Specialist. WordPress Buff.
Blackhole Pro: Trap bad bots in a virtual black hole.

20 responses to “Fun with the DOS Command Prompt”

  1. Noor Isham 2007/03/07 10:26 pm

    no comments just question.. i’m sharing my pc with 2 other persons.. is there any dos command to trace when they on/use my pc?

  2. Perishable 2007/03/11 6:17 pm

    Noor,

    I do not know of any way to do this with DOS/command line.. But there are many excellent solutions available to explore:

    • 007 Spy Software
    • Desktop Authority (404 link removed 2015/02/10)
    • Lazy man’s way (404 link removed 2014/07/20)
    • Windows IT Pro Forum

    Good luck!

  3. james.m.k 2007/07/29 3:06 pm

    Noor Isham,

    If you want to do it via dos promt you can add a batch file with the following text into the “All Users” startup Directory (found at C:\Documents and Settings\All Users\Startup)

    ======

    echo %Time% %Date% %USERNAME% Has Logged On >> c:\logfile.txt
    echo His profile is found at %USERPROFILE% >> c:\logfile.txt

    ======

    It will make a logfile something like this:

    12:04:13.12 06/30/2007 Don has Logged On
    His profile is found at C:\Documents and Settings\All Users\Don

    ======

    Have fun!

  4. Perishable 2007/07/29 4:11 pm

    james.m.k,

    muchas gracias, señor!

    ( very kind of you! )

  5. for the printer i think you want “lpt” ive never heard of prn

  6. Perishable 2007/08/13 7:49 am

    prn and lpt are equivalent in the eyes of DOS:

    Ports of Call: DOS System Device Drivers, Part 2 (404 link removed 2014/12/01)

  7. Hi,how can I see the web pages my roommates visited on my computer?

  8. Perishable 2007/11/18 9:08 am

    Hey curious,
    Which version of Windows are you using?

  9. Dear sir, It is a pleasure writing to you this massege,i’m a student in a professional school in Cameroon.I will like to know more about the fundamental fact about DOS command. thanks in advance

  10. Dear DOS Master, I have a batchfile that sends/gets files via ftp but i would like a log of all information in the dos window. the append command only records certain things and doesnt give me the kb/s information after a file is uploaded or downloaded. can you help?

    I need the information which comes back from the FTP: how can i get this into a log?

    Thanks!

  11. Thanks for the question, luke, but I am nowhere near being a “DOS Master”.. (DOS noob, maybe)..

    Nonetheless, I will keep your question in mind and see if I can come up with a solution the next time I find myself playing around with DOS :)

  12. Hey, I’m just wondering, when I go into directory

    C:\Program Files\ROBOPro>

    and type in

    copy roboproen.hlp con

    The computer beeps, and on the command prompt is a bunch of garbage. Random letters, along with random alt-codes (e.g., Smiley faces, arrows, card suits). Is it something to do with the file being a .hlp?

    I’m just wondering, actually it’s pretty cool.

Comments are closed for this post. Something to add? Let me know.
Welcome
Perishable Press is operated by Jeff Starr, a professional web developer and book author with two decades of experience. Here you will find posts about web development, WordPress, security, and more »
Digging Into WordPress: Take your WordPress skills to the next level.
Thoughts
Went out walking today and soaked up some sunshine. It felt good.
I have an original box/packaging for 2010 iMac if anyone wants it free let me know.
Always ask AI to cite its sources.
All free plugins updated and ready for WP 6.6 dropping next week. Pro plugin updates in the works also complete :)
99% of video thumbnail/previews are pure cringe. Goofy faces = Clickbait.
RIP ICQ
Crazy that we’re almost halfway thru 2024.
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.