Menu Share

Basics of the command line (Windows)

by | Published on
category Trendy

The command line is long forgotten feature of the computer. Most people these days rely on the GUI windows and visual representation of data. This blog is also about games so this will be a short post on explaining the basic commands that anyone can use at the terminal so that we can run some of the more basic programs before we move on to drawing on the screen.

The terminal is a useful tool that gives a nice text representation of the data that the computer manages. It is really utilized during development for logging, compiling and automation. Computers always had a terminal and even the most advanced IDEs have it built inside and show it somewhere in a panel – to display build progress and status messages, to display basic program output, to run terminal commands, etc.

Table of Contents

Access the terminal

On windows there are two languages/terminals available that are both supported – Command Prompt and PowerShell. PowerShell can utilize the power of .NET and is also considered to be fully featured as a programming language on its own. To run any of the two you can click on the start menu on the bottom left and start typing “cmd”, “Command Prompt” or “PowerShell”. When you open it you will be greeted by a window resembling this one:

PowerShell Window

You will notice a greeting message from Microsoft then there is a directory path that will show you the current “Working directory”. This is the directory in which you will execute commands. This is just like in windows explorer you can open certain directories.

Basic Commands

Here is a list of the basic commands that you need to know. The code lines will start with a greater than symbol (>)

To change a directory use

> cd "<path_to_directory>"

To go back one level you can use the .. notation like so

> cd ..

You can also chain these like go back 3 directories and then go to scripts from there

> cd ../../../scripts

To note the current directory you can use a single . (this will not do anything as we’re already here)

> cd .

In command prompt to change between drives like C: and D: use the /D directive.

C:> cd /D D:/
D:> cd /D C:/

To execute a program from current directory simply type it with a . in front

> ./program.exe

PATH

The PATH in windows and linux is a set of directories that would always be looked for when running commands from the command line. You already know that the current directory can easily be referenced when using the “./” notation but for example if you have a program like CMake that is installed in “C:\Program Files\CMake\bin” and you want to run it in the current directory you would have to write:

> C:/Program Files/CMake/bin/cmake.exe ./

To make this convenient you can search for “Edit environment variables for your account” in windows.

Search for Edit Environment Variables for Your Account

A window will pop out and in the upper section you should look for “Path” and click edit to edit it or create a new variable named “Path” if there is none in the list.

Environment Variables Window

The edit window will appear where you can add, edit and delete directories that you want included for quick execution later in the command line.

Environment Variables Edit Window

Batch scripts

Moving between directories and relative paths are just the beginning. Being able to execute programs opens up endless possibilities. This is very useful for automation as we can then run all kinds of applications – even write our own that would later on be part of our tooling. But sometimes it becomes tedious to always have to remember the commands, execute them in the right order and in the right directories.

This is where batch scripts come in handy. Just like .exe files the batch scripts (.bat files) can be double clicked and run and can be executed from command line or other batch scripts. This is especially useful when executing Java or Python code.

To have a simple scenario of a useful batch file we can create a python script for hello world. You can install python from here https://www.python.org/downloads/ (make sure to add python to PATH when installing). We can start by creating the python script in a directory of our choice called “hello.py”. The script will contain the following content:

print("Hello, World!")

To execute the python script you would usually type in the command line the following command

> python ./hello.py

This should produce the string “Hello, World!” in the console window. In windows though most users are used to double clicking instead of the command line and having to always type this is a bit tedious so we can also write a batched file called “hello.bat” in the same directory:

python ./hello.py

Now you will be able to double click the bat file which will run the python script and even nicely close the console window after that. This will be useful in later articles on this blog where I talk a bit about automation.

Leave a comment

Your email address will not be published. Required fields are marked *