Windows: a little batch programming towards a friendly greeting

The tutor has wanted to place a greeting at the top of the Windows terminal (aka command prompt screen).  Now you can too.

I heard about batch files – and saw files with the .bat extension – in the late ’90s. They weren’t necessarily new then; that’s just when I first saw them.

A batch file is a plain text file that sends specific commands to other programs in the Windows system. It might tell one to open, for instance. It may also order the backup of a file.

This little batch project consists of two programs: one (called p1.bat) opens the command prompt, then initiates the second one. The second one (p2.bat) prints a friendly greeting, then the date, atop the command prompt screen.

p1.bat
@echo off
cmd /k p2


p2.bat
@echo off
echo Hello. Hope you’re well:)
date /t

In p1.bat, cmd opens the terminal, while /k keeps it open. p2 calls p2.bat to start.

In p2.bat, echo prints the message that follows to the screen. date outputs the date, while /t suppresses the option for the user to change it.

@echo off means that the actual commands are not printed to the screen, but just executed.

As I can tell, batch programming uses the newline as a command separator.

Batch programs need to be written in plain text, so not with a word processing program (unless you know how to switch its output to plain text). In Windows, the plain text editor is Notepad.

To write a batch program that opens the terminal and just prints “Cheers:)” at the top of the screen, you’d open Notepad and type a little file:

@echo off
cmd /k echo Cheers:)

Just to be safe, you probably want to press enter at the end of the line – although it may not matter.

Save the file as cheers.bat. You’ll probably want to give it its own folder.

To execute the file, find it in its folder (using the file manager, rather than the terminal), then double click it. Hopefully, the command prompt screen opens up with the friendly greeting Cheers:) at the top.

I’ve used some very helpful sources for this article:

superuser.com

microsoft.com

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Leave a Reply