Cyderize.org

Cyderize.org is my personal website, started in 2011 which I use to write about computers, the Internet, electronics, music, and whatever else I feel like. I'm a computer nerd and musician, residing in Melbourne, Australia.

Read the About page to find out more.

Using the Command Prompt (Windows)

Posted August 17th, 2011 by

The command prompt in Windows is actually a very important part of using Windows. Sure, it mightn’t look flashy, but it’s very powerful, and it’s easy to use once you get used to it.

The command prompt itself in Windows probably goes back to the early releases of the OS (I’m talking before Windows 95) where the OS was actually MS-DOS with an application called Windows that brought up the graphical interface. Once you were in Windows, you could do a lot, but there were still things that you couldn’t do unless you used the old DOS command line. But instead of exiting Windows, doing what you had to do, and going back into Windows, you could just open a command prompt and do what you need there. Now, in the days where almost every includes a fancy graphical user interface (GUI), people are ignoring the command prompt altogether, but there are still some things that it’s useful for.

Open the command prompt: Press [Windows Key] + [R], type “cmd” and press [OK]. Now you’ll be presented with a black and white window that says something like:
Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.

C:UsersJason>

Pretty much the only thing you can do through the command line is actually run programs. A ‘command’ is usually just an instruction to run a program with some sort of parameters that get fed into the program. The single most important and widely used command is ‘cd’. It’s so important that it’s used in the majority of operating systems: Windows, UNIX, Linux and Mac OS (X). The command ‘cd’ means Change Directory, which makes sense because it’s used to change the Current Working Directory. The Current Working Directory is what’s displayed before the flashing cursor, in this case ‘C:UsersJason’. Any commands that aren’t found in the system’s directories (the PATH) are assumed to reside here. But why is this useful? Well it’s really for ease of use, so when you run a command, you don’t have to specify the full path, instead only specifying the ‘relative’ directory. For example, we have a program located here: ‘C:MyApp2ThisAppapp.exe’. If we ran a command at our current state, we would have to write ‘C:MyApp2ThisAppapp.exe’. But, if we changed the current working directory to ‘C:MyApp2ThisApp’, then we would only have to write ‘app.exe’. Oh, and how do we change directory? We use the command ‘cd directory’ where directory is replaced by the path itself.

Example
C:UsersJason>app.exe
'app.exe' is not recognized as an internal or external command,
operable program or batch file.

C:UsersJason>C:MyApp2ThisAppapp.exe
Hello, World!

C:UsersJason>cd C:MyApp2ThisApp

C:MyApp2ThisApp>app.exe
Hello, World!

C:MyApp2ThisApp>

All (almost, some have slight variations) commands use the following syntax
command parameter1 parameter2 parameter3...

So when you want to run the command ‘copy’, then you would write ‘copy’ followed by the parameters that you want to pass to it. EG:
C:MyApp2ThisApp>copy app.exe C:UsersJasonapp.exe

In this case, the command is ‘copy’ and there are two parameters: app.exe and C:UsersJasonapp.exe. The number of parameters a program accepts depends on its function, and you can find out about these parameters by running the command ‘command –help’, which will (usually) display the instructions on how to use a program.

The real advantage of the command line is the ability to create ‘batch files’, which allow for quick automation of commands in a sequence. For example, I could have a batch file that automatically copies two files, says “Foo!” and makes the computer beep. And although it is a silly batch file, it is easily achieve by simply typing in each command necessary (separated on to different lines) into a text file saved with the extension ‘.bat’. I could do it like this:
@echo off
copy C:UsersJasonfile1.txt C:file3.txt
copy C:file2.txt C:Usersfile6.txt
echo Foo!


Go ahead and try it! Open Notepad and paste in the text. Then, go to File > Save and where it says Save As Type [Text Documents (*.txt)], change the type to [All Files (*.*)]. Then, save it as ‘foo.bat’ (Must have ‘.bat’ at the end of filename). Now, find the file in Windows Explorer and drag it into a command prompt window and press enter. Since you won’t have the files listed for copying, it will output something like this:
The system cannot find the file specified.
The system cannot find the file specified.
Foo!
'' is not recognized as an internal or external command,
operable program or batch file.

And will make a beeping noise. This is a very simple (and stupid) batch file. Let’s break it down:
First Line: @echo off – Stops the prompt from rewriting C:UsersJason> and the command for each line in the file.
Second Line: Attempt to copy C:UsersJasonfile1.txt to C:file3.txt – fails since you don’t have such a file
Third Line: Attempt to copy C:file2.txt into C:Usersfile6.txt – also fails since you don’t have this file either
Fourth Line: ‘Echoes’ Foo! – In other words, makes the command prompt write Foo! On to the screen.
Fifth Line: A  character that causes the prompt to beep. It’s really a black circle that’s printed as ^G on the command prompt. In fact, if you run the command ‘Control + G’, you’ll get the same result.

And that’s a simple batch file. You can find out about the various commands usable in batch files by doing a quick Google search. I’ll put up some more stuff on this soon, but until then, that’s all. 🙂

Comments: none yet | Filed under: tutorials | Tagged: , , , , ,

Leave a Reply

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