The command line interface (or command line shell) is a mainstay for system administrators, power users and hackers as it allows them to interact rapidly with a computer without having to click through numerous graphic menus. Moreover groups of instructions to the command line interface can be assembled together into what are known as batch (or script) files. These batch files allow the automation of complex tasks, and are analogous to primitive programs. Should you ever find yourself repeating a series of basic instructions to a computer (moving and renaming files for example) you might be able to save yourself a lot of time and effort by creating a batch file program to automate that task.
Today we will explore some aspects of the windows command line interface and make a simple game using a batch file. For more information on Windows command line programming, feel free to check out these online resources:
The command prompt is run from its own window by invoking the command interpreter that is provided by the file cmd.exe located in the Windows\System32 folder. There are two basic ways to start the command line interface:
The command prompt window can be opened by entering "cmd" (without quotes) into Start-Run box.
The command prompt window can be opened going to Start then All Programs then Accessoriesand then selecting Command Prompt.
A black and white window (the colors can be changed) containing the command prompt will open.
Basic Commands
The command prompt is an interface to the operating system; the first thing you will need to learn is how to get around.
Finding the current directory
The cd command can be used is to determine your location in the file systems. If you type cd (lowercase) into the command prompt window and hit return you should see something like the following:
C:\Users\meyer>cd
C:\Users\meyer
C:\Users\meyer>
The first line is what is called the command prompt. It ends with a > and by default shows the users location in the file structure. The result of typing in the cd command and hitting enter is displayed on the second line, and the third line is the command prompt again. It may seem silly that the cd command gave us the same information that is already in the prompt, but that is only because we have not changed the prompt.
Changing the command prompt
You can change the command prompt line (hereafter called the 'prompt') to say anything you wish. Let’s do that now. Type in the following and hit return:
prompt awaiting instructions:
Your command prompt line should now say "awaiting instructions: " You can change it to say anything you want. It is sometimes useful to have the prompt display the time, which can be accomplished by typing the following:
prompt $T
To see even more options for setting the prompt type in the following and hit return:
help prompt
You can play with those options if you wish, but for now let's return the prompt to its original state by typing in the following and hitting return:
prompt $P$F
Looking Around using dir
Type in the following and hit return
dir
The directory command (dir) will give you a listing of all the files in the current folder. You can find out more about the dir command by typing in:
help dir
I like to use the "wide list" format with the directory command which looks like this:
dir /w
Moving around using cd
When you are done exploring the dir command we need to work our way to the desktop folder (which is the folder that contains the icons on the 'desktop' that you see when working in the windows Graphic User Interface).
We have already used the cd command to identify the current directory; you can also use the cd command to change directories. Type the following and hit enter:
cd Desktop
You should now be in the Desktop folder and you should be able to confirm this by looking at the prompt (if you are not using the standard prompt then type cd by itself as you did in section 2.1 to confirm). Let's go back to the directory we just left, which you can do by typing the following:
cd ..
The two dots ( .. ) tell the command to go back up one folder, and thus you should be back in the directory you started on. Let's go back to the Desktop folder but this time let's do it without typing in the full name Desktop. Instead type in the following:
cd De
and then hit the TAB key. What happened? Hit enter to proceed to change to the Desktop directory. For more information about the cd command use the help command we used in sections 2.3 and 2.4.
Creating a new directory
You should be in the desktop folder (you can confirm this by typing cd) where we would like to create a new folder to hold the remaining files we will create in this lab. You can create a new folder (name lab1) by typing the following and hitting enter:
mkdir lab1
If you look at your desktop you should now see a new folder called lab1. Use the cd command to move to the lab1 folder.
Other commands
There are many other useful commands that you can use to create, move, modify and delete files and folders. In fact anything you can do using the windows GUI you can also do using the command line interface.
Instructions for using the other command line interface (CLI) commands can be found online at: http://pcsupport.about.com/od/commandlinereference/tp/command-prompt-commands-p1.htm A complete list of the command available to you in the CLI can also be obtained by typing in the following command (and hitting return):
help
Before we go, try one last command, and make a note of what it does: cls
Starting programs:
Windows keeps track of the names of the programs that are installed on it, and can search for and try and load programs by name. In the next section we are going to want to create a batchfile using the program called Notepad (which comes built-in to Windows). We can start the Notepad program from the CLI simply by typing the name of the program (Notepad) and then hitting enter. However, it is worth noting that many programs can also be given "input parameters" which are additional instructions to use as the program starts up. For example we can start the Notepad program AND get it to create and name a file called mygame.bat for us by typing in the following command and hitting enter:
notepad mygame.bat
You should now see something like the following (note: you may get an error message saying that the file mygame.bat cannot be found, and you should tell windows to create that file):