The Command Line Interface
Hard drive structure and how to navigate through it.
During my time as a systems administrator in the 1990s I became very proficient in the use of command prompts. It was a great time to be a systems administrator. I had to administer a combination of DOS/Windows 3.x, Windows 9x, Novell Netware 2.x and 3.x servers, a Windows NT server, and even an old Zenith CP/M machine. The command prompts on those machines varied wildly in their use but they all had one thing in common, they were very, very powerful tools at my disposal. I couldn't imagine being a computer programmer without at least knowing the basics of using a command prompt.
Therefore, I feel every computer programmer needs a basic understanding of file and directory structure and the best way to learn is to open a command prompt and start typing. The command prompt is what old-timers (like me) refer to as the DOS prompt. However, the correct terminology in today's world is the CLI or Command Line Interface.
Note: This lesson will be focused on how a Windows drive's files and directories are structured and how to navigate around that structure using commands. This knowledge will be very helpful with your programming skills when dealing with files on the hard drive and knowing where and how to access them. Understanding a drive at the command prompt level and knowing how to use command prompt commands will give you a much greater understanding of the statements offered in QB64 that perform these same functions.
Open a CLI now by holding down your Windows key on the keyboard and then press the R key. In the RUN text box type in CMD and then press ENTER. You should see a window appear similar to Figure 1 below.
Figure 1: The Windows 7 CLI
The CLI Meets the GUI
From 1981 to 1995 the command prompt was the way we talked to our IBM and compatible computers. Starting around 1992 Windows was just starting to get popular with the arrival of version 3.1. However, Windows 3.x (and 95, 98, and ME) were nothing more than fancy DOS programs that still needed the command prompt (DOS) to execute. Windows has what is known as a Graphical User Interface or GUI (pronounced goo-ee) that uses icons to represent computer assets such as files, folders, and programs, along with a mouse pointer to navigate the GUI landscape. Windows 3.x included a GUI as well but Windows 3.x needed to be started from a command prompt by typing win. It wasn't until Windows 95 came out that the GUI became front and center.
Windows 95 skipped the command prompt during boot (it was actually hidden behind the loading screen) and went directly into the GUI. The command prompt was still in control but Microsoft wanted you to think that Windows 95 (also 98 and ME) was a real operating system when in reality it was still a big fancy DOS program. Ever since then Windows users have learned to use the GUI and completely forget that the CLI ever existed and how to use it. The command prompt never disappeared but instead evolved along with each version of Windows that is a true operating system (Windows NT, 2000, XP, Vista, 7, 8.x, 10, and 11).
It wasn't until true 32 bit versions of Windows (Windows NT and above) appeared that the DOS command prompt, along with DOS, was truly a thing of the past. It was replaced by Windows NT's CLI which still allowed for the familiar tried and true DOS commands along with a host of new ones. Windows was now a true operating system.
Linux users have never had this problem of forgetting the CLI exists. The CLI in Linux (called the Terminal or Console) is used more often than a GUI by Linux professionals. In fact many Linux installations, servers for instance, don't even bother booting into a GUI at all but instead rely on command input through the Terminal for program installation and configuration. Furthermore Linux has always been a true operating system based on another operating system named Unix, so abandoning the command prompt to make the operating system look "true" was never a thing. The command prompt is just a happy fact of life for Linux users. True Linux users love the command prompt and are very, very proficient with it.
Create a Sandbox to Play In
Before we get started with typing in commands let's create a safe place for you to play. In the CLI window you previously opened type in the following commands one after another in lower-case and make sure to type in the spaces where you see them. Press the ENTER key after each command after you have typed it in.
At this point your command line should look as it appears in Figure 2 below.
Figure 2: The sandbox has been created
What we've done here is create an area on the hard drive called sandbox that we can play around with commands in. We'll get to what each of these commands did later in the lesson.
The command prompt is a very powerful tool at your disposal and if a command is typed in incorrectly nasty things can happen to your operating system. The sandbox we created to play in will ensure any mistyped commands will only affect this play area we created. Don't worry, when the lesson is finished you'll know how to delete this sandbox from your drive freeing up the space and removing the mess we are about to make.
The Hierarchical TREE Structure
DOS was designed to have a structure very similar to a tree. The very beginning of any drive (hard drive, floppy drive, thumb drive, etc..) is known as the root of the drive. The familiar C:\ actually means "at the root of the C: drive". The single backslash character ( \ ) following C: reads as "root". Figure 3 below describes what a typical hard drive's hierarchical tree structure looked like back in the DOS days. Figure 4 below shows how the structure actually looks in a real DOS window.
Figure 3: The hierarchical structure of drives
Figure 4: The hierarchical structure in a real DOS window
This structure is still used to this day and Windows has been presenting it to you all along. Every time you use Windows Explorer or use software to save or open files the hierarchical structure is used as seen in Figure 5 below.
Figure 5: The Windows hierarchical drive structure
An analogy used back in the DOS days to describe the layout of a drive's structure was the metal filing cabinet. Computer's have the capability of having more than one drive. Think of each drawer in the filing cabinet as an individual drive in a computer that can be accessed. Back in the DOS days computers had a floppy drive named A so label the top drawer as A:. Computers back then, just as today, also had a hard drive typically named C so label the second drawer as C:. Those early computers usually included a CD-ROM drive as well named D so label the third drawer as D:.
When you access a drive by opening a file cabinet drawer you are said to be entering the root directory, or beginning, of the drive. The drive letter now gets a backslash ( \ ) character added to it denoting that the root directory is being accessed, such as C:\. Inside the file cabinet drawer you might find loose papers, known as files and programs, or folders, known as directories. The folders, or directories, can be opened to expose more files, programs, or even more folders, known as subdirectories. The secondary folders, or subdirectories, can be opened to expose even more files, programs, or subdirectories.
Imagine you open the C: file cabinet drawer. The C: drive designation now changes to:
with the added backslash signifying that you have opened the C: drive's drawer, or root directory. Inside the drawer you find a folder, or directory, labeled GAMES so out of curiosity you open the folder. The C:\ drive designation now changes to:
meaning that you have opened the GAMES folder, or directory. Inside this folder you find another folder, or subdirectory, labeled DOOM, so once again you open this folder changing the C:\GAMES drive designation to:
which signifies that you have opened the DOOM folder, or subdirectory, contained within the GAMES folder, or directory.
The drive's designation changing like this according to your actions is known as the current path you have taken. Your path took you from the C drive ( C: ), to the root directory ( C:\ ), then to the GAMES directory ( C:\GAMES ), and finally to the DOOM subdirectory ( C:\GAMES\DOOM ). As you navigate through the drive using the command prompt the command prompt will change showing you the current path you have taken to get where you are currently located within the drive. Let's go ahead and start navigating your hard drive to see this in action.
Navigating the Hard Drive Using the CLI
Click on the CLI window you previously opened and type in CD\ and press the ENTER key. This command will take you to the C: drive's root directory.
Note: All commands typed in at the command prompt must be activated by pressing the ENTER key once. From now on it can be assumed that the ENTER key will need to be pressed whenever you are asked to type in a command at the command prompt.
Note: Commands typed in at the command prompt are case insensitive, meaning that they can be entered in lower-case, UPPER-CASE, or a COmbInATion of both. Windows does not care. Commands in this lesson will be presented to you in UPPER-CASE for clarity but you can type them in lower-case to save wear and tear on your SHIFT key.
At the command prompt type in CLS (as the note above stated the ENTER key will need to be pressed after entering a command at the command prompt. This will be the final reminder.) The CLS command clears the CLI screen returning the command prompt to the home position (top left corner). We now have a nice clean slate to start with as shown in Figure 6.
Figure 6: Let's begin
The DIR Command
The DIR command used by itself displays a directory listing of all files, directories, or subdirectories accessible from your current location which is also known as the current working directory.
The DIR command is one of the most powerful commands available at the command prompt. The full syntax for the DIR command is:
DIR [Drive:][Path][FileName] [/A[[:]Attributes]] [/B] [/C] [/D] [/L] [/N] [/O[[:]SortOrder]] [/P] [/Q] [/R] [/S] [/T[[:]TimeField]] [/W] [/X] [/4]
Note: Brackets [ ] mean optional.
Holy parameters batman! No, we are not going to cover all the parameters offered by the DIR command and many of the other upcoming command prompt commands. At the end of this lesson is a listing of commands learned. You can click on each command and be taken to a web site that details each parameter and how it is used if you are curious.
At the command prompt type DIR to get a directory listing of your drive's root directory.
Figure 7: A directory listing of the drive's root directory
Note: Directories and subdirectories are represented as folders in Windows. When you open a folder in Windows using the GUI you are actually opening a directory or subdirectory located somewhere on your hard drive. Even the Windows desktop is nothing more than a subdirectory located on the hard drive.
Figure 7 above shows the root directory listing of the author's hard drive. Yours will most definitely be different but let's cover the common features. Each entry starts with a date and time stamp. The date and time indicate the last time something has been changed with a file or the contents of a directory. Each <DIR> seen indicates that the entry is a directory that can be entered. Finally, the last column to the right is the name of the file or directory.
Within your listing you should see the "sandbox" directory we created earlier.
The CD and CHDIR Commands
The CD and CHDIR commands allow the user to change, or enter, directories. This would be the same as opening a folder in the Windows GUI. The CD and CHDIR commands are actually the same command. Microsoft decided to include CHDIR because many popular operating systems at the time, like CP/M, used the CHDIR version of the command. This was thought to give DOS some familiarity for people moving from other operating systems. The syntax for the CD (and CHDIR) command is:
CD [drive:][path] move into a drive or directory
CD.. move back one level to the parent directory
CD\ move back to the root directory of the drive
Type in CD at the command prompt without any parameters. As you can see the CD command by itself will return the current working directory you are in. Notice that the returned value is the same as the command prompt. The command prompt will always show you the current working directory by displaying the path to the directory. Let's see this in action.
At the command prompt type in CD\sandbox.
Did you notice what happened to the command prompt? It should appear as seen in Figure 8 below.
Figure 8: The command prompt changed
The command prompt will update as you navigate through directories and subdirectories displaying the path taken to your current location, or current working directory. Type in CD at the command prompt once again and you'll see the results are the same. Type in DIR at the command prompt to see what is available in this directory.
Figure 9: In the sandbox directory
As Figure 9 above shows we have three subdirectories available because they are labeled with <DIR>. When you leave the root directory and enter into a directory or subdirectory away from the root you will always see the dot ( . ) and dot dot ( .. ) subdirectories available. These two special subdirectories are for quick navigation. The dot ( . ) subdirectory is used to denote the current working directory and the dot dot ( .. ) subdirectory is used to denote the parent directory. The parent directory is the directory that contains the current working directory you are in, or simply put, the previous directory.
If you want to move one directory back to the parent directory you would use the dot dot ( .. ) subdirectory to do this.
Type in CD.. at the command prompt and notice what happens.
Figure 10: Back at the root directory, the parent directory to the sandbox directory
We are back at the root directory since it was the parent, or previous directory. Type in DIR at the command prompt again and you won't see the dot ( . ) and dot dot ( .. ) subdirectories. This means we are at the beginning of the drive or the root directory. Let's get back in to the sandbox directory.
Type in CD sandbox at the command prompt.
As you can see you have reentered the sandbox directory. But hold on, last time you entered the sandbox directory you used the command CD\sandbox and this time you were instructed to use the CD sandbox command. What gives?
Figure 11: cd\sandbox and cd sandbox did the same thing?
By using a backslash ( \ ) after the CD command you are stating "start from the root directory" and by using a space after the CD command you are stating "start from the current working directory".
CD\sandbox (go into the sandbox directory starting from the root directory)
CD sandbox (go into the sandbox directory from the working directory where I currently reside)
Since the sandbox directory is directly off the root directory both variants of the CD command worked as expected. Now that we are in the sandbox directory type in the following command at the command prompt:
and then type in the following command at the command prompt:
What did we do wrong? Why can't the system find the path specified?
Figure 12: Did we break something?
When you issued the CD\sandbox command you told the command prompt to:
"change directories, starting from the root of the drive, into the sandbox directory"
So, the CD command went back to the root directory of the drive and looked for a directory called sandbox. When it found the directory the command prompt entered it. You basically did a loop back to root directory and ended up right where you started.
When you issued the CD sandbox command you told the command prompt to:
"change directories, starting from where I currently reside, into the sandbox directory."
So, the CD command looks in the current working directory for a directory called sandbox. Well there's the problem. Looking back at Figure 10 you can see there are only three subdirectories available, dot ( . ), dot dot ( .. ), and this. Since a directory named sandbox does not exist the CD command could not continue walking on the path you specified, hence, "The system cannot find the path specified." We'll come back to the CD command in a minute. We need to discuss another command quickly first.
The TREE Command
Yes, there is actually a command called TREE used for viewing a drive's hierarchical tree like structure. The syntax for the TREE command is:
TREE [Drive:][Path] [/F] [/A]
At the command prompt type in TREE to see the result.
Figure 13: This TREE has branches
The TREE command used by itself shows the directories and subdirectories that extend from the current working directory. It creates a treelike branch structure using extended ASCII characters to give a visual representation of what paths lie ahead.
The optional /F switch can be added to also display the files within each branch of the path.
Figure 13 above shows that we have two complete paths that can be taken from our sandbox:
Did you notice that .\ was added to the above two paths? Remember the dot ( . ) subdirectory we saw when the DIR command was issued while in the sandbox directory? The single dot ( . ) again represents the current working directory. So the path:
is stating, "from the current working directory ( . ), to ( \ ) the "this" subdirectory, to ( \ ) the "is" subdirectory, to ( \ ) the "a" subdirectory, and finally to ( \ ) the "path" subdirectory."
The optional /A switch can be added to change the extended ASCII characters that create the nice branch graphics to standard characters. This does not look as nice but was a needed option back when some printers, and even computers, did not supported extended ASCII characters. The option is still available for legacy purposes and you'll probably never need to use it.
Note: You may have noticed that the final subdirectory in the paths above is named path. Unlike QB64 and other programming languages where using reserved words is not allowed for naming items it's completely acceptable to do this with the command prompt command names.
The CD and CHDIR Commands Continued
Now that we know how to use the TREE command to get a picture of the paths ahead let's explore more ways to use the CD command.
Have you noticed that two of the optional parameters for each command introduced so far are:
With most command prompt commands you can build a path that tells them where to carry out their orders. For example, one of the paths from the sandbox directory is .\this\is\a\path and to get there we could do this:
and our command prompt would show:
indicating that we have arrived at our destination. Wouldn't it be nice however if we could just teleport directly to that location instead? Well you can, and here is one of the ways the CLI is much more efficient than using a GUI. All that needs to be done is to supply a path for the CD command follow. Type the following command in at the command prompt:
Figure 14: Teleporting straight to our destination
Just like Star Trek we teleported directly to our location. The CD command will use the path provided to navigate its way to the destination.
"From the root ( \ ) go into the "sandbox" directory, then to the "this" directory ( \this ), then to the "is" directory ( \is ), then to the "a" directory ( \a ), and finally to the "path" directory ( \path )."
Since we started from the sandbox directory these commands would have also worked:
The first example above states to "Start from the current working directory ( .\ ). The second example states the exact same thing. The use of dot ( . ) followed by a backslash ( \ ) and the use of a single space mean the same thing.
Looking at Figure 13 again we see there is another path from the sandbox directory, .\this\is\another\path. We could simply issue the following command:
to teleport directly there. But there is another trick you can do with paths. You can use the parent directory dot dot ( .. ) to aid in navigation as well. Type the following command in at the command prompt:
Figure 15: Teleporting from one path to another
Whoa, what just happened there? Let's break down the supplied path to see. Remember, we are starting from the \sandbox\this\is\a\path path.
"Go back to the parent directory ( ..\ ) which takes us back to \sandbox\this\is\a, go back to the parent directory ( ..\ ) which takes us back to \sandbox\this\is, then to the another directory ( \another ), and finally to the path directory ( \path )."
We have arrived at our destination:
By knowing the structure of your drive with the aid of the TREE and DIR commands you can use the CD command to teleport anywhere within your drive's structure.
You can also use the CD command to teleport to another drive. If your computer has a D: drive and contains the path \Games\3D you can issue the command:
The D: designates the new drive to change to. Once there the path \Games\3D is followed to the final destination:
The MD and MKDIR Commands
The MD and MKDIR commands allow the user to create directories and subdirectories. Both of these commands do the same thing for the same reason as explained with the CD and CHDIR commands. The full syntax for the MD command is:
Notice that the Path parameter is not optional. Let's create a new subdirectory in our sandbox directory. First we'll need to get to the sandbox directory by typing in the command:
Next, let's create a subdirectory called MyStuff by typing in the command:
Let's see what the new tree structure looks like by typing in the command:
And finally let's view the entry in the directory by typing in the command:
Figure 16: A place to put my stuff
As you can see in Figure 16 above you've created a new subdirectory named MyStuff and a new path to it, \sandbox\MyStuff.
There's no limit on how many subdirectories deep you would like to go, for example:
However, there is a limit on the combined number of characters allowed which is 260. Combined characters means the length of the characters in the path plus the length of the filename being accessed.
You don't need to be in the parent directory of the new directory you wish to create either. Type this command in and see what happens:
Let's see how the tree changed by typing the command:
Figure 17: A directory created elsewhere from the current working directory
We told the MD command explicitly where to create the new directory by supplying a path:
The final \newdirectory attached to the end of the path is the name of the new directory to create. Once again a great time saver over using a GUI to manipulate files and folders.
The RD and RMDIR Commands
The RD and RMDIR commands allow the user to remove directories and subdirectories. Both of these commands do the same thing for the same reason as explained with the CD and CHDIR commands. The full syntax for the RD command is:
RD [/S] [/Q] [drive:]path
Notice that the Path parameter is not optional. Let's remove the MyStuff subdirectory we created earlier. Type in the following command to change to the sandbox directory:
and then type in the following command to remove the MyStuff subdirectory:
And finally let's examine the tree to see if the branch has been pruned:
Figure 18: Someone took MyStuff
That was fun, let's do it again. Type in the following command to remove the this subdirectory:
Figure 19: There's something in there
And we get an error as seen in Figure 19 above. Directories and subdirectories must be empty before they can be removed. This includes files, programs, and other subdirectories. There is a workaround to this however, but it can be disastrous if used incorrectly.
The optional /S command line switch.
Adding /S to the RD command removes all directories and files in the specified directory in addition to the directory itself. In other words, everything inside the directory including all files and subsequent subdirectories is deleted. This will prune an entire path from the tree. Let's go ahead and try it. First, we need to make absolutely sure we're in the sandbox directory. Type the following command in:
This is why we created the sandbox directory. If anything goes wrong the damage is limited to this play area.
Now, type in the following command:
RD /S this
Figure 20: Are you sure?
You are presented with a chance to back out of the action since all files, directories, and subdirectories will be pruned from the tree if you proceed. Press Y and then press ENTER to confirm you wish to proceed.
Type in the following command to see what happened to the tree:
Figure 21: All paths have been removed
As you can see both paths that existed in the this subdirectory are now gone. If there would have been any files or programs in there they would have been deleted as well.
IMPORTANT: When you delete files, directories, and subbdirectories using the CLI command prompt they will not be moved to the Windows recycle bin ... they are gone forever.
Read that important statement above a few times so it sinks in. The command prompt is very powerful and can result in unexpected outcomes if not used properly. When the command prompt asks you, "Are you sure (Y/N)?", make sure that you really do want to proceed.
It used to be that computers had a minimum of three drives, a floppy drive, a hard drive, and a CD-ROM/DVD drive. Then, as time went on, the floppy drive became a thing of the past leaving computers with two drives. Today the CD-ROM/DVD drive has mostly become a thing of the past leaving only one drive remaining, the hard drive, which is usually named C:. If you happen to have more than one drive it's very simple to change between them.
For instance, if you have a second hard drive named D: then simply type that at the command prompt:
and you will be taken to the D: drive. To switch back to the C: drive type this at the command prompt:
and you'll return to the C: drive. The command prompt will also remember the path each drive was residing at between drive changes. For example, if you were sitting at this path on the C: drive:
and typed D: at the command prompt to switch to the D: drive:
then later on type in C: at the command prompt to return to the C: drive you would see:
because the command prompt remembered the path you were previously at. This is important to remember when using commands such as COPY (explained below) and simply send a file to the C: drive without specifying a path. If no path is specified the remembered path will be used resulting in an unexpected location where the file has been copied to.
Every command we've learned has sent the results to the screen for us to view. It's possible to redirect the output of commands somewhere else, such as to a file or a printer port. Before we move onto more commands we'll need some files to play with. To create these files we'll redirect the output of the DIR command. First, let's make sure you are in the sandbox:
Next, we'll create a file called File1.txt using redirection:
DIR > File1.txt
Now, let's create a second file called File2.txt:
DIR > File2.txt
Let's see what our sandbox directory looks like now:
Figure 22: Files created using redirection
There are three redirection methods available. The first, which we just used, is the greater than sign ( > ) that sends the output to an alternate destination, typically a file. This method will overwrite an existing file or create a new file if it does not already exist.
The second method is to use two greater than signs ( >> ) that sends the output to an alternate destination, again typically a file. However with this method an existing file is not overwritten but instead the output gets appended (added) to the end of the file. This method will also create a new file it it does not already exist.
The third method is to use the pipe character ( | ) that sends the output to another command or program to be processed by that receiving command or program.
Let's create another file using the two greater than signs method. Type in the following command:
DIR >> File3.txt
Let's do the same command three more times. However, you don't have to retype the command in three more times. The command prompt remembers every command you previously typed in.
Press the UP ARROW key once on your keyboard and the command will reappear. Now press ENTER.
Do this two more times, press the UP ARROW key once and then press ENTER.
Let's see the results by issuing a DIR command.
Figure 23: File3.txt is much larger than the others
The first time the command was executed the file File3.txt was created since it did not already exist. The next three times the command was executed the output of the DIR command was appended to File3.txt since the file already exists. That's why in Figure 23 above the file File3.txt is larger than File1.txt and File2.txt. Let's look inside File3.txt to see what happened.
Type in the following command at the command prompt:
The TYPE command is used to display the contents of a file to the screen for viewing but the contents scrolled by way too fast. Let's pipe the output to another program for easier viewing. Type in the following command:
TYPE file3.txt | MORE
Figure 24: More please
The MORE command is used to display information one page at a time. By setting up a conduit (a pipe) between the TYPE command and the MORE command the output of the TYPE command was sent to the MORE command.
Pressing the ENTER key will scroll through the text one line at a time. Pressing the SPACE BAR will scroll through the text one page at a time. When the end of the file is reached the command prompt will return.
Note: If you ever need to leave a command before it is finished you can hold your CTRL key down and press C. CTRL-C is a break instruction that commands will see and immediately exit sending you back to the command prompt.
The COPY Command
The COPY command allows a user to copy one or more files to an alternate location. Before we get started copying files around we need to rebuild a few directories in the sandbox since we deleted them earlier. Type the following commands in:
Figure 25: A few directories to copy files to
As you can see in Figure 25 above we now have a few directories to copy files to. The DIR command's listing is in alphabetical order which causes the Documents directory to be listed before the files and then finally the remaining directories. This cluttered look can get annoying quick. Here's a little trick you can do to make things easier to read. Type the following commands in:
Figure 26: That's better, nice and neat
Directories are now listed first in alphabetical order followed by individual files listed in alphabetical order. Much better and easier to read. The directive:
is a command line directive that tells the DIR command to order listings ( /o: ) with directories first in alphabetical order ( g ) followed by files in alphabetical order ( n ). This directive is usually one of the first things I type into the command prompt if I plan on spending more than a minute or two with it.
Again, this is just a little trick you can use to make your time at the command prompt more enjoyable. Ok, back to learning the COPY command.
The full syntax for the COPY command is:
COPY [/D] [/V] [/N] [/Y | /-Y] [/Z] [/L] [/A | /B ] source [/A | /B] [+ source [/A | /B] [+ ...]] [destination [/A | /B]]
Wowsers! A very powerful command with many optional parameters available. Don't worry, we'll just cover the basics of the COPY command to get a feel for it.
The simplest way to use the COPY command is to supply it with a file name to copy and where (the path) to send it to. Let's put a copy of File1.txt into the Documents subdirectory. Type the following commands in:
COPY file1.txt .\documents
Figure 27: The file has been copied
Well that was easy. A copy of File1.txt now sits in the Documents subdirectory as the DIR command revealed. But hold on, how did we get a directory listing of the Documents subdirectory without first changing into it?
Remember that earlier it was pointed out that most command prompt commands have the optional [Drive:][Path] parameters. Use those parameters to your advantage. By specifying a path to the DIR command you specifically told it where you want to see the listing generated from:
"Show me a directory listing from the current working directory ( .\ ) to the Documents subdirectory."
This command would have performed the same function:
Remember, a space and .\ mean the same thing, "from the current working directory".
Again, with most commands, you don't actually need to be at a location to take actions at that location. Very powerful stuff this Command Line Interface is!
The COPY command can also be used to rename a file as it is copied. Type in the following commands:
COPY file1.txt .\documents\Renamed.txt
Figure 28: Renaming a file while copying
The Documents subdirectory now contains two identical files with one of them being renamed while it was copied. If you wish to copy a file within the same directory the only way to do this is to rename the file as it is being copied. Two files with the exact same name can't exist in a directory. For example:
COPY file1.txt renamed.txt
Since no destination path was given the file will be copied within the current working directory.
If you try to copy a file and it already exists in the destination path the COPY command will ask you how to handle the situation. Type the following command in:
COPY file1.txt .\documents
Figure 29: The file already exists
If the file already exists the COPY command will ask you if you wish to overwrite the existing file. Answer with a Y if you wish to overwrite, an N if you do not, and we'll get to what the All option means in a bit. Go ahead and answer N and press ENTER to abort the COPY procedure.
Here another handy trick for you that can be done with the COPY command. If you need to make a quick file, for instance a simple text file, you can do this at the command prompt with the COPY command and your keyboard. Type the following command in:
COPY CON SpainRain.txt
Now, type the following line of text in:
The rain in Spain falls mainly on the plain
Next, hold your CTRL key down and press the Z key. You'll see a ^Z appear on the screen.
Now, press the ENTER key.
Figure 30: A file created by using the keyboard as input
You just created a text file using your keyboard as the source file. The command:
COPY CON SpainRain.txt
tells the COPY command to copy a file from the keyboard. CON is short for console which in the old days of computing meant keyboard.
"COPY from the keyboard to SpainRain.txt"
The CTRL-Z ( ^Z ) sequence of characters means end of file, or EOF, to a computer. This tells the COPY command that the end of the keyboard file has been reached and the command can exit. By the way typing a carat ( ^ ) and then a capital Z will not work. The EOF must be performed by pressing CTRL-Z.
Go ahead and use the TYPE command to view the new file you just created:
I used this trick a lot back in the day to create quick text and batch files at the command prompt when a text editor was not readily available.
The DEL and ERASE Commands
The DEL and ERASE commands are used to delete files from a computer. Both of these commands do the same thing for the same reason as explained with the CD and CHDIR commands. The syntax for the DEL command is:
DEL [/P] [/F] [/S] [/Q] [/A[[:]attributes]] names
First, the important message you read in the RD command section:
IMPORTANT: When you delete files, directories, and subbdirectories using the CLI command prompt they will not be moved to the Windows recycle bin ... they are gone forever.
DOS used to include a command called UNDELETE that is no longer supported. This needs to be pointed out in case you went searching for command prompt commands and came across this old command thinking it could help you retrieve a file(s) you deleted by mistake.
The DEL command used by itself is very simple. Type in the DIR command to get a directory listing of the files we created.
Figure 31: The files located in the sandbox directory
We don't need SpainRain.txt any longer so let's get rid of it. Type the following commands in:
Figure 32: SpainRain.txt has been deleted
That's all there is to it. Like other commands you can use the optional [Drive:][Path] parameters to supply a path to where the file you want to delete is located.
Be careful with this command. If you delete a file that is required by Windows to operate you could end up crashing your entire computer system.
The REN and RENAME Commands
The REN and RENAME commands are used to rename files and directories. Both of these commands do the same thing for the same reason as explained with the CD and CHDIR commands. The syntax for the REN command is:
REN [drive:][path][directory name1 | file name1] [directory name2 | file name2]
Type in the following commands to rename a file:
REN file3.txt Renamed.txt
Figure 33: File3.txt has a new name
Type in the following commands to rename a directory:
REN games MyGames
Figure 34: The Games directory has a new name
No special actions are needed to inform the REN command if the name you provided is a file or a directory. The REN command will automatically make that determination and rename the file or directory you have chosen.
REN will not allow renaming a file or directory to a file name or directory name that already exists. Type the following command in:
REN file1.txt file2.txt
Figure 35: Duplicate names are not allowed
You may be thinking, "file2.txt is not the same as File2.txt". Remember, Windows does not care about the case of commands, directories, or files. Upper and lower case file and directory names are the same to Windows. While Windows will preserve the case for you when you create a file or directory in actuality behind the scenes Windows treats everything in lower case. This is not the case however in Linux. File2.txt and file2.txt would in fact be two unique files.
Dealing With SPACES
In every example that has been shown up to this point none of the file or directory names have included spaces within them. Windows allows spaces and to accommodate this file and directory names need to be handled differently. Let's try to create a subdirectory that contains a space in it. Type in the following commands:
MD Steamed Hams
Figure 36: No Steamed Hams for you
The MD command ignored the space and created two subdirectories instead, one named Steamed and the other Hams. (Which I found rather odd ... I had no idea both subdirectories would be created. After a little research I found this is normal behavior. I learn something new every day.)
In order to tell a command to include a space, the name of the file, directory, or subdirectory needs to be encapsulated in quotes ( "" ). Let's try that again, this time with the quotes. Type in the following commands:
MD "Steamed Hams"
Figure 37: That's better
By encapsulating "Steamed Hams" in quotes the command now saw this as a single directory name instead of two. Encapsulating names with quotes needs to be performed for any command that works with names that include a space. Type the following command in:
CD .\"steamed hams"
Figure 38: Inside the "Steamed Hams" subdirectory
We are now inside the Steamed Hams subdirectory. If the quotes were not there this would happen. Type in the following commands:
CD steamed hams
Figure 39: That worked?!?
What the?? That worked even though the name of the subdirectory was not encapsulated in quotes. Thank you Microsoft for making things complicated.
For some commands quotes are needed, for others they are not, which is why I ALWAYS use quotes with every command that involves a name that includes a space. Trying to remember which commands work without quotes versus those that require the quotes is an exercise in futility. Just use quotes when a space is involved and save yourself the headaches.
Wild cards allow the user to work with many files at once. Type in the following commands:
Note: the * symbol is called an asterisk and is created by holding your SHIFT key down and pressing the 8 key.
Figure 40: Well that's wild, only the files that have the extension .txt are listed
The DIR command was instructed to show only the files that have an extension name of .txt by replacing the file name with the asterisk ( * ) wild card character.
"Give me a directory listing of files, I don't care what the file name is ( * ) but the extension must be .txt"
Before we delve further into wild cards let's talk about file names first. File and directory names have two parts to them, the file or directory name itself, followed by an optional dot ( . ) and then an extension name identifier.
The extension name of a file is usually used to identify the type of file it is. For example, Beethoven5th.mp3 is more than likely a music file because it has the extension of .MP3 (now you know why MP3 players back in the day were called this). Another common extension is .JPG or .JPEG which indicates that file is more than likely an image file. File names with the extension of .EXE are most likely executable files, otherwise known as programs or "apps" for your younger generation.
Noticed I used "more than likely" in each of those sentences. In reality any file name can be given any extension name regardless of its type. To make things even more confusing it's acceptable to use more than one dot ( . ). Back in the early days of the Internet the Windows "feature" to hide file extensions bit users in the ass. Bad actors were doing this to executable files and sending them to unsuspecting people through email:
Since Windows was hiding the extension the only thing users saw in the email was:
And of course, nerds being nerds, they just had to open that "picture". What really happened is since the file is actually an executable ( .EXE ) a nasty payload such as a virus was unleashed on the user's system. If your Windows system is hiding the extensions (yes, believe it or not this is still a thing) I would highly suggest you turn that "feature" off. Most, if not all, email servers today will block executable files from being send via email mostly eliminating this hack.
Back in the DOS days file names were limited to 1 to 8 character file names and optional 1 to 3 character extension names. This became known as the 8.3 convention. Today filenames and extensions can be any length as long as combined they don't exceed 260 characters. A common file format used today are torrents for downloading files. Torrents are file pointers having an extension of .Torrent breaking the old 8.3 convention rule. Ok, back to wild cards.
There are two wild card characters to choose from, the Asterisk ( * ) and the question mark ( ? ). Here are a few examples of wild cards in use. Each will be explained in detail afterward.
COPY *.txt .\mygames
COPY f*.* .\documents
The asterisk ( * ) wild card is used to render the character at which it resides in a file name or extension name and all characters to the right wild, or "any characters".
The question mark ( ? ) wild card is used to make a single character at that position within a file name or extension name wild, or "any character".
Example 1 ( DEL *.* ) is instructing the DEL command to delete files with any file name ( * ) and any extension name ( * ). Basically to delete all files in the current working directory! Care is needed when using wild cards.
Example 2 ( DEL file*.txt ) is instructing the DEL command delete all files that start with "file" while the remaining characters in the file name can be anything ( * ). However, the extension name must be .txt.
Example 3 ( DIR file?.txt ) is instructing the DIR command to list only files that begin with "file" while the fifth character can be anything ( ? ). The extension name must be .txt. The way in which this example differs from example 2 is that file names in example 2 can be any length after the asterisk ( * ) while the single question mark in this example limits the file name to only 5 characters in length ( file? - first four characters must be "file", the 5th character can be anything).
Example 4 ( DIR ???e*.* ) is instructing the DIR command to list files that have a 4th character of "e" ( ???e ) with the remaining file name being anything ( * ) and the extension name can be anything ( * ).
Example 5 ( COPY *.txt .\mygames ) is instructing the COPY command to copy any file ( * ) that has a .txt extension name to the mygames subdirectory located within the current working directory.
Example 6 ( COPY f*.* .\documents ) is instructing the COPY command to copy only file names that start with f ( f* ) and have any extension name ( * ) to the documents subdirectory in the current working directory.
Wild cards make command prompt commands that deal with files and directories much more powerful and flexible. Instead of working with just one file you can work with many at a time.
Let's try a few wild cards with commands by first starting out with a clean slate in the sandbox directory:
Figure 41: The current state of the sandbox directory
First we'll copy all of the files with the extension of .txt to the Documents subdirectory:
COPY *.txt .\documents
Figure 42: So this is why the All option exists
Earlier when the COPY command was introduced the file File1.txt was copied into the Documents subdirectory. The COPY command is now confirming that this file should be overwritten. This is where you can answer using the All option to tell the COPY command to overwrite this file and any subsequent files that may also exist.
Go ahead and type A the press ENTER to confirm all should be overwritten.
Figure 43: All files have been copied
The COPY command used the wild card ( * ) to scan the current working directory for all files that have a .txt extension name and then copied them to the Documents subdirectory. Let's verify the results by getting a directory listing of the Documents subdirectory:
Figure 44: The COPY command was successful
On second thought we decide that copying the .txt files to the Documents subdirectory isn't what we wanted to do. Time to use the DEL command and a little wild card magic to get rid of them:
Figure 45: The .txt files in the Documents subdirectory have been deleted
There was no need to change into the Documents subdirectory to perform the delete when a path could be specified ( .\documents\*.txt ) to where the files are located that we wish to delete.
The system administrator just walked in and stated that none of the files located in the sandbox directory are needed any longer and to go ahead and delete them:
(answer Y to confirm the deletion)
Figure 46: All of the files have been deleted from the sandbox directory
How This Relates to QB64
Most software requires asset files (sound files, image files, database files, etc..) that need to be loaded in as the software executes. For simple games, such as the Pac-Man clone I wrote, there are only a few asset files. The Pac-Man clone has a sprite sheet file (an image file) and a few sound files that need loading when the program is executed. For a small game like this it's perfectly acceptable to have the program executable (the .EXE) and the asset files all within the same current working directory.
However, when projects get large, and asset files start numbering in the 100s or even 1000s you really need to organize those assets into subdirectories. From the current working directory of your program's executable you may need a Sounds subdirectory for the various sound files, an Image subdirectory for the image files, a Texture subdirectory for various texture images used throughout your game, etc. Furthermore, within each of these subdirectories you may wish to break the assets down even further by including, for example, a Level1 subdirectory, Level2 subdirectory, Level3 subdirectory, and so on to separate the assets into the levels in your game where they are used.
As a QB64 programmer you'll need to know how to reference these paths to access your assets:
JumpSound = _SNDOPEN(".\Sounds\Jump.ogg") ' load the jump sound
PlayerShip = _LOADIMAGE(".\Images\Ships\Level1\Pship.png", 32) ' load level 1 player ship
By understanding how paths operate you'll be able to create paths such as the examples above demonstrate. Furthermore, when looking at someone else's code you'll understand how their path construction works. QB64 also offers commands that perform many of the concepts we learned in this lesson and are constructed in much the same way actual command prompt commands are structured.
QB64 Command Prompt Statements
QB64 offers statements that interact with files and subdirectories in the same way that command prompt commands do. Below is a a chart showing some of the QB64 statement and the equivalent command prompt command. Lesson 11 of the tutorial will introduce you to these QB64 statements which will be much easier to understand now that you have gone through this side tutorial.
Statement Command Prompt Equivalent
CHDIR CD or CHDIR - change into a directory or subdirectory
MKDIR MD or MKDIR - make a directory or subdirectory
RMDIR RD or RMDIR - remove a directory or subdirectory
NAME...AS REN or RENAME - rename a file, directory, or subdirectory
KILL DEL or DELETE - delete a file
OPEN TYPE - view the contents of a file and Redirection - write to a file
_FILEEXISTS DIR - look for the existence of a file
_DIREXISTS DIR - look for the existence of a directory or subdirectory
LOF DIR - return the length of a file
WRITE Redirection - write to a file
EOF CTRL-Z - test for the end of a file
_STARTDIR$ the working directory at program startup
_CWD$ the current working directory
Commands Learned in this Lesson
Concepts Learned in this Lesson
Oh, One Last Thing
If you have not done so yet you can remove the sandbox directory we created by typing in the following commands:
RD /S sandbox
( Answer Y to the question )
Figure 47: All gone - thank you for using this lesson