Nowadays, people don't think that much about batch files when it comes to programming. In my opinion, they can be very helpful, and they are extremely easy to make! There are a few basic rules to them though, and this is a guide for people who are taking their first steps into batch file learning.
I will be going over the basic commands in this tutorial. Next to the command will be - text
and that text will describe what the command does, so it is not part of the command:
echo - Anything that you put after this command will be repeated by the command line. EX: echo Hello world!
pause - This command will sort of pause the actions and wait for you to press a key to continue
help - lists all of the simple commands and what they do
del - deletes the specified file, but you must be in the same directory. EX: del test.txt
cd - Changes the directory. EX: cd "C:\Test batch files\"
cd.. - goes back one directory
cd\ - goes to the root directory, in most cases C:\
echo off - Turns off the option to show the current directory next to where you type...
echo on - Turns on the option to show the current directory next to where you type...
color - Lists all of the color codes. For example, if you wanted a black background with bright green text, you would type this: color a
cls - Stands for clear screen. It wipes the command line so all previous commands that have been executed can't be seen.
dir - Lists all of the files and subdirectories in the current directory
Those are all of the simple commands that I can think of at the moment. Now, lets go on to making your first batch file now that you know a few commands!
First open NotePad. It is a very good text editor when it comes to making batch files. Copy and paste this into notepad:
echo Hello World!
echo This is my first batch file!
Can you guess what that would do? If you can, then that's great! That means that you are very good at programming already if you can visualize the program before running it! If you don't know, that's ok, because this may be your first program and no one expects you to know it all!
Lets see what it does for ourselves! In NotePad, go to File > Save As...
and in the Name
input box, type this: "anyname
and save it. It MUST
have the quotes, or it will not save correctly. You can change the anyname
to anything that you want. Now, go to where you saved it and open it! You will see a black box and you can read the on screen text and do what it says! Great! Now, try to remember some of simple commands and you will be able to make easy programs like this.
You're probably wondering; "Well, what can I do with this? It seems useless..."
To answer your question, many things! You can use this as an instructional program for people. However, there is a LOT more to batch programming than just this... but we will go over more advanced stuff later on.
Now that you've got all of that down and remembered, lets go over some variables.
Variables are preset commands that hold information based on their names. These can come in useful when specifying directories or other things. Variables are always wrapped with %. Here is an example of a variable: %username%
Now, when testing commands, you want to open a command line, not make batch files. To open a command line, go to Start > Run > cmd.exe
and click Ok. Now, to test that %username%
variable and see what it does, type:
You should see that it printed your username on the screen. If not, check your spelling. That variable can come in useful when specifying their desktop directory. Lets say that you wanted to make a batch file show someones desktop directory. We would type this:
echo C:\Documents and Settings\%usermame%
Well, before you dive into using variables, go over the simple commands. Make flash cards and practice
They are very useful when it comes to making helpful and useful batch files. In the 2nd edition of this tutorial, I will go over more variables and you will start making useful batch files!