The term ‘Kata’ is a funky name for an exercise designed to help improve your skills by introducing and explaining new concepts, then getting you to solve problems to solidify your understanding. This is the first of such Katas in the book.
In this Kata you’ll learn about how to use Windows’ built in command line interface (CLI), known as the Command Prompt.
Before we start creating computer software, we need to learn about the Command Prompt. Knowing how to use a command line interface is an essential skill for software engineers.
For this Kata you’ll need access to a computer running Windows 10.
Open the Start menu and search for
cmd. You should see an entry called
Command Prompt and you should see a black window appear. Inside the window should be some text, something like:
Microsoft Windows [Version 10.0.17134.228] (c) 2018 Microsoft Corporation. All rights reserved. C:\Users\Mikiel>
This is the Command Prompt window, or console. We’re going to be typing instructions here to tell the computer what to do. The instructions are usually very short, perhaps only two or three words at most. These instructions are known as ‘commands’, so this is considered to be a ‘command line interface’ (CLI).
The important part is the line that looks like
C:\Users\Mikiel>. This represents a location (such as a folder) on your computer’s hard drive. The Command Prompt is always ‘looking’ at a location on your hard drive. It’s very important to pay attention to this, because it determines what happens when you run commands.
Let’s write our first command. Type
dir into the Command Prompt and press enter to run the command. You should see something like the following:
C:\Users\Mikiel>dir Volume in drive C has no label. Volume Serial Number is 4407-DFED Directory of C:\Users\Mikiel 02/09/2018 14:20 <DIR> . 02/09/2018 14:20 <DIR> .. 15/01/2017 17:12 <DIR> .android 14/10/2016 18:39 <DIR> .AndroidStudio2.2 12/08/2017 13:15 48,257 .babel.json 13/01/2017 14:26 <DIR> .config
dir command lists all the files and folders in Command Prompt’s current location. Because the Command Prompt is looking in
dir command lists all the files there.
To help you visualize what’s happening, in the Command Prompt run the following command:
This will open the Windows File Explorer at the location your Command Prompt is looking at. You can refer to the File Explorer window as you continue with the Kata to see more clearly what each command is doing.
You can use the Command Prompt to create folders too. Let’s create a folder that we can mess around in, then delete when we’re finished.
mkdir command to create a folder. like so:
Notice that this time you have to supply the name of the folder you want to create. In this case we’re creating a folder called
To check if it worked, use the
dir command again and look for
MyFolder. Also check your File Explorer window. You should see
MyFolder has now appeared.
Let’s also create another folder. Run the following command:
Check to see if
MySecondFolder has appeared.
Let’s create some text files using the Command Prompt.
We want to write the following text into a file:
To do this, the first command we want to use is
echo. Run the following:
echo Hello World
And you should see something similar to:
C:\Users\Mikiel>echo Hello World! Hello World!
echo simply outputs whatever text you give it, which isn’t very useful as it is! However, we can use
echo to output text into other commands.
We can do this with the redirect operator, that looks like this:
Operators are special instructions that enable commands to be chained together in some meaningful way. There are many operators available in the Command Prompt, but we only care about redirects for now.
Run the following:
echo Hello World > MyFile.txt
On the left of the
> operator is the
echo command, which we saw earlier. To the right of the
> operator, we supply the name of the file we would like to create - in this case
MyFile.txt. The redirect command will take the output of
echo, and put it inside
MyFile.txt for us.
To see if it worked, we can use the
more command. Write:
more command reads a file, so you should get the following output:
You can also use the redirect operator to write a file into a folder. Run:
echo Hello World > MyFolder/AnotherFile.txt
This will write
The file system on a computer can be visualized as a special sort of graph known as a tree.
Based on the files we’ve created in this Kata so far, we can visualize the file system to look something like this:
C: └─Users └─Mikiel ├─MyFile.txt ├─MyFolder | └─AnotherFile.txt └─MySecondFolder
The ‘root’ of the tree is
C:/, which is the name of the hard drive. The ‘branches’ (folders), are
MyFolder. The ‘leaves’ are
Remember how the Command Prompt is always ‘looking’ at location on your hard drive? Currently, it should be looking at somewhere like:
(Although it’s going to be different on your machine, unless your name also happens to be
This means we can ‘see’
MySecondFolder. We can’t see any leaves or branches above or below - only those on the current level of the tree.
You can only run commands on files and folders that the Command Prompt can ‘see’. So if I want to read
MyFile.txt, I need to write
more MyFile.txt. But what if I want to read
AnotherFile.txt? That file is is inside
MyFolder, so the we can’t see it. Try running the following:
The Command Prompt probably told you it can’t find the file! In order to read
MyFolder, we need to say where to find it. We can do that with the ‘absolute path’ to the file.
An absolute path explains where a file or folder is from the root. The absolute path to
AnotherFile.txt is going to be something like:
Though this is going to be slightly different on your machine! To find the absolute path to
AnotherFile.txt, first run the following:
This will tell you where your Command Prompt is looking. We need to copy the output of the command, and then add
For example, when I ran
cd I got:
So I add
/MyFolder/AnotherFile.txt to get
C:/Users/Mikiel/MyFolder/AnotherFile.txt. You can then run your command that should look something like:
You should then see the output -
Absolute paths tend to be long, and annoying to write. Fortunately there’s a simpler way!
You can also run commands with paths relative to the files and folders we can currently see. These are called ‘relative paths’. Try running this command:
This is saying that
AnotherFile.txt lives inside a folder called
MyFolder. We can see
MyFolder, so we can look in there, and then we can find
AnotherFile.txt - much quicker than using the absolute path!
It’s annoying to have to provide relative paths or absolute for commands all the time. Fortunately, there’s a way to change where the Command Prompt is looking.
Let’s change where the Command Prompt is ‘looking’ to
MyFolder. This is done with the
cd command. Run:
And you should notice the location has changed to something like:
Now we can access the files in
MyFolder more easily.
rename MyFile.txt MyAwesomeFile.txt
dir again and you should see the effect of the rename. You can also check the File Explorer window and see that the file has been renamed to
Now we’ve finished messing around with
MyFile.txt, let’s clean up the mess we’ve made. First we want to change where the Command Prompt is looking back to where we were before.
File paths are structured in such a way that we can consider them to have ‘levels’. For instance, given the file path:
We would say that
Mikiel is one ‘level’ up from
In order to move up a level, back to
Next we can delete our folder using the
rmdir command. Run:
However you should get a warning message saying:
The directory is not empty.
In order to get around this we want to supply a ‘flag’ to the command. Flags are special instructions that tell a command how to behave. In this case, we want a flag to force
rmdir to delete
MyFolder despite it not being empty. The flag we want is
rmdir /s MyFolder
You may get a prompt like:
MyFolder, Are you sure (Y/N)?
Y to confirm the deletion.
Check the File Explorer to see that
MyFolder is now gone.
And that’s it! You’ve just had a whirlwind introduction to the Windows Command Prompt. There’s lots to learn here and we’ve only just scratched the surface of what Command Prompt can do. However this is enough to get started!
At the end of many of the Code Katas in this book is a section called ‘Try on your own’. These sections have exercises for you to try, well, on your own, without following any direct instructions.
These sections are designed to be challenging, so don’t worry if you can’t complete them all! Once you’ve given them your best shot, you can find answers to these sections on the page after each Kata.
In the File Explorer, find a random folder on your computer. Note it’s absolute path (found at the top of the File Explorer). Open up a Command Prompt window. Try and navigate to that file using the
cd command in the Command Prompt.
You can use the up and down arrow keys on your keyboard to recall previously executed commands. Try running a few
echo commands, then recalling them with the up and down arrow keys.
You can use the tab key on your keyboard to automatically complete some commands. Try the following:
cd, but do not press enter
You can also try:
cd SuperF, but do not press enter
There are many commands available in the Command Prompt. You can access help for a command by writing
help and the name of the command. Try to figure out how to use the
color command this way. Start with: