rVidia
08-29-2006, 08:35 PM
*For Windows
Today I felt like writing about batch files; they're both fun and useful! For starters, you can identify a batch file by the .bat file extension. It is basically a text file that contains Command Prompt commands (you can open Command Prompt by going to Start > Run > cmd).
Batch files can be very useful, and to make this post a relatively quick read, I've decided to write about the basics of batch files by breaking down a simple .bat file I have written for this purpose. The .bat file below is a very simple backup program. (You can already get an idea of their usefulness). Before beginning, you might want to copy the below code and print it out (I attached a .txt file with the code) so you can follow along / take any notes you feel are necessary.
::backup.bat
::Backs up the My Documents folder
::Written by Ray
:MENU
@ECHO OFF
CLS
ECHO ===============
ECHO 1-Back up files
ECHO 2-Exit
ECHO ===============
ECHO.
SET /P INPUT=
IF "%INPUT%" == "1" GOTO CHECK
IF "%INPUT%" == "2" EXIT
CLS
ECHO Invalid choice.
PAUSE
GOTO MENU
:CHECK
CLS
IF NOT EXIST D:\ ECHO Please insert a CD into the CD-ROM drive at the front of your computer.
ECHO.
PAUSE
IF NOT EXIST D:\ GOTO CHECK
:START
CLS
ECHO Copying files . . .
ECHO.
IF NOT EXIST D:\Documents mkdir D:\Documents
XCOPY "C:\My Documents" D:\Documents /D /R /S /Y > D:\log.txt
ECHO.
ECHO Backup complete!
PAUSE
:EOF
CLS
ECHO Remove CD now.
IF EXIST D:\ GOTO EOF
ECHO.
ECHO To exit,
PAUSE
EXIT
At first glance, this might look somewhat complicated, but I'll break things down for you.
First of all, you'll need to know how to make a .bat file. Start by opening Notepad (Start > Run > notepad). Then, save the file in this way: go to Save As... > in the File name box, name the file "backup.bat" (make sure you USE the quotes so the computer will recognize the file as a .bat file and not a .txt file).
Now to break down that batch file...
The first part, ::backup.bat, is not necessary. I wrote it to introduce you to a comment. Everything that follows the :: is commented out. You'll notice the other comments I have used as well.
The next part, the section :MENU, will be covered later, so move on down to :CHECK
This is a bit complicated to explain (but don't get discouraged; it's not hard once you understand it). You'll notice the colon : before CHECK, similar to when you're commenting something out, but there's only one colon here. "CHECK" can be replaced with any other word you desire; for instance, you could use :CHK4CD or :S1, etc. You have several options. In simplest terms, what this is is a section. Everything beneath it until the next :SECTION is a part of that section.
Sections are not at all necessary in batch files, but they are useful. You can just have sections for orginazation, or they are useful when using the "GOTO" command as you'll see soon.
The next part, @echo off, is a bit harder to understand. Basically, by adding @echo off to the beginning of the code, you are telling the computer to only display what you want the batch file to display when you run it, i.e., it won't show the batch file's code when you run it.
It might be easier for you to understand if you test it out by yourself, hands-on, by omitting @echo off and then running the batch file and seeing what happens, then running the batch file WITH @echo off and noting the difference.
Sometimes, it is useful to omit @echo off when you first write the batch file so if you come across any errors, you can see what part of the code was causing the problem. Then, when everything is working correctly, you can add @echo off to the beginning of the batch file.
The next command is CLS, which clears the screen. Pretty simple.
And now for the next line,
IF NOT EXIST D:\ ECHO Please insert a CD into the CD-ROM drive at the front of your computer.
I'll break this down into a few parts. The first part is IF NOT EXIST D:\
To make more sense of this, I'll tell you WHAT this batch file does. It is going to copy all of the files in the My Documents folder to a CD (to back these files up). Now, in this case, the CD drive is drive D:\ so all this is saying is if there is no CD in drive D:\ then do ...
In this case, the "..." is ECHO Please insert a CD into the CD-ROM drive at the front of your computer. The only thing you need to understand here is the command ECHO, which displays everything after it on the screen. Why don't you type something into your batch file and put ECHO in front of it, then run the batch file? You'll see what I mean when you run it; it simply displays what's written after it.
And now the ECHO. portion of the code. I'll put it this way: every time you see a "." after ECHO, it means "skip a line" Again, try adding this to your batch file and you'll get a better understanding of what it does.
The next command is PAUSE
What it does is it displays "Press any key to continue . . ." and waits for you to press a key before continuing. It's important to use a command like PAUSE in a batch file for this reason: without something like this, the batch file will open and then immediately close. Try adding PAUSE to the end of your batch file and then omit it.
The next line is IF NOT EXIST D:\ GOTO CHECK
Here you'll see the importance of sections. What this is saying is if there is still no CD in the CD-ROM drive, go to (GOTO) section CHECK
This will force the user to insert a CD into the CD-ROM drive before they can continue.
Now another section :START
Another CLS (clear screen), and then ECHO Copying files . . .
This does NOT actually copy files yet but is just displaying that sentence on the screen to let the user know what's going to happen.
You'll again see ECHO. to skip a line.
The next line has something new: IF NOT EXIST D:\Documents mkdir D:\Documents
First, I will explain what's supposed to happen during the backup process. All of the files being copied from My Documents will be copied to a folder on the CD called Documents, so what this is saying is that if the folder "Documents" does not exist on the CD, just create a folder called "Documents" on the CD. And that's that.
You'll next see the most important command in the code: XCOPY "C:\My Documents" D:\Documents /D /R /S /Y
XCOPY is a command telling the computer to copy.
"C:\My Documents" is the folder that files will be taken/copied from. (Note the quotation marks, which MUST be used when there is a space in the location).
D:\Documents is where these files will be copied to.
/D /R /S /Y are parameters for the XCOPY command. You can find out more about the parameters for the XCOPY command by opening a Command Prompt and entering XCOPY /?
Here is an overview of what the parameters I have chosen to use are:
/D basically does this: when copying the files to the CD, it only copies files that have been changed or that have not been copied to that CD yet. This keeps the program from copying everything in My Documents to CD every time the program is run.
/R overwrites read-only files.
/S copies all directories and subdirectories, except empty ones.
/Y suppresses prompting to copy files.
So, you can look at this line this way: XCOPY "backthisup" "savebackuphere" /parameter
The next command in that line is > D:\log.txt
This command is not necessary but is just used to introduce you to redirectors. The > is a redirector that tells the batch file to store the output of the command before it in a text file. So, this command tells the batch file to create a text file called log.txt on the CD and to send a list of all copied files to that text file. Again, try running the batch file to see what happens.
You'll again see ECHO. to skip a line.
The next line is ECHO Backup complete! which will simply display that sentence on the screen.
You'll recognize the rest of the commands in the code. Just note the beginning of the next section by :EOF (you can replace EOF with another word, but I use it to stand for 'end of file') and note the EXIT command at the end of the batch file, telling the program to exit.
You'll also want to note IF EXIST D:\ GOTO EOF
You've seen this before, but another explanation: What this is saying is that if the CD is still in drive D:\ then go to (GOTO) section EOF (the screen will be cleared a second time and "Remove CD now" will be displayed a second time; the commands in the rest of the section will not be executed until the CD is removed)...
Finally, let's go back to the first section, :MENU
You should already be familiar with most of the commands in the MENU section, although there are some new commands to learn as well.
First, the SET /P INPUT= command is used to create the variable INPUT. This variable (INPUT) can be renamed to any one word you want, although I prefer to use INPUT, standing for user input.
The following line is IF "%INPUT%" == "1" GOTO CHECK
This says to go to section CHECK if the user typed in 1
IF "%INPUT%" == "2" EXIT says to exit the program if the user typed in 2
The rest of the commands in this section; rather, the commands after the above line, give instructions on what to do if the user types in a character other than 1 or 2. For example, if the user typed in 3, the screen would be cleared, Invalid choice. would be displayed on the screen, the user would be prompted to press any key to continue, and the batch file would go to the MENU section.
Congratulations! These are the basics. It's difficult to understand everything at first, but if you read through this a few more times and start testing things out on your own, I promise you'll get a better feel for batch files. Please feel free to ask any questions if any of you would like to attempt to make your own batch file. The key to writing one is to know exactly why you need it, to plan it out, and to then edit/add lines here and there to make it even better ;)
Ray
Today I felt like writing about batch files; they're both fun and useful! For starters, you can identify a batch file by the .bat file extension. It is basically a text file that contains Command Prompt commands (you can open Command Prompt by going to Start > Run > cmd).
Batch files can be very useful, and to make this post a relatively quick read, I've decided to write about the basics of batch files by breaking down a simple .bat file I have written for this purpose. The .bat file below is a very simple backup program. (You can already get an idea of their usefulness). Before beginning, you might want to copy the below code and print it out (I attached a .txt file with the code) so you can follow along / take any notes you feel are necessary.
::backup.bat
::Backs up the My Documents folder
::Written by Ray
:MENU
@ECHO OFF
CLS
ECHO ===============
ECHO 1-Back up files
ECHO 2-Exit
ECHO ===============
ECHO.
SET /P INPUT=
IF "%INPUT%" == "1" GOTO CHECK
IF "%INPUT%" == "2" EXIT
CLS
ECHO Invalid choice.
PAUSE
GOTO MENU
:CHECK
CLS
IF NOT EXIST D:\ ECHO Please insert a CD into the CD-ROM drive at the front of your computer.
ECHO.
PAUSE
IF NOT EXIST D:\ GOTO CHECK
:START
CLS
ECHO Copying files . . .
ECHO.
IF NOT EXIST D:\Documents mkdir D:\Documents
XCOPY "C:\My Documents" D:\Documents /D /R /S /Y > D:\log.txt
ECHO.
ECHO Backup complete!
PAUSE
:EOF
CLS
ECHO Remove CD now.
IF EXIST D:\ GOTO EOF
ECHO.
ECHO To exit,
PAUSE
EXIT
At first glance, this might look somewhat complicated, but I'll break things down for you.
First of all, you'll need to know how to make a .bat file. Start by opening Notepad (Start > Run > notepad). Then, save the file in this way: go to Save As... > in the File name box, name the file "backup.bat" (make sure you USE the quotes so the computer will recognize the file as a .bat file and not a .txt file).
Now to break down that batch file...
The first part, ::backup.bat, is not necessary. I wrote it to introduce you to a comment. Everything that follows the :: is commented out. You'll notice the other comments I have used as well.
The next part, the section :MENU, will be covered later, so move on down to :CHECK
This is a bit complicated to explain (but don't get discouraged; it's not hard once you understand it). You'll notice the colon : before CHECK, similar to when you're commenting something out, but there's only one colon here. "CHECK" can be replaced with any other word you desire; for instance, you could use :CHK4CD or :S1, etc. You have several options. In simplest terms, what this is is a section. Everything beneath it until the next :SECTION is a part of that section.
Sections are not at all necessary in batch files, but they are useful. You can just have sections for orginazation, or they are useful when using the "GOTO" command as you'll see soon.
The next part, @echo off, is a bit harder to understand. Basically, by adding @echo off to the beginning of the code, you are telling the computer to only display what you want the batch file to display when you run it, i.e., it won't show the batch file's code when you run it.
It might be easier for you to understand if you test it out by yourself, hands-on, by omitting @echo off and then running the batch file and seeing what happens, then running the batch file WITH @echo off and noting the difference.
Sometimes, it is useful to omit @echo off when you first write the batch file so if you come across any errors, you can see what part of the code was causing the problem. Then, when everything is working correctly, you can add @echo off to the beginning of the batch file.
The next command is CLS, which clears the screen. Pretty simple.
And now for the next line,
IF NOT EXIST D:\ ECHO Please insert a CD into the CD-ROM drive at the front of your computer.
I'll break this down into a few parts. The first part is IF NOT EXIST D:\
To make more sense of this, I'll tell you WHAT this batch file does. It is going to copy all of the files in the My Documents folder to a CD (to back these files up). Now, in this case, the CD drive is drive D:\ so all this is saying is if there is no CD in drive D:\ then do ...
In this case, the "..." is ECHO Please insert a CD into the CD-ROM drive at the front of your computer. The only thing you need to understand here is the command ECHO, which displays everything after it on the screen. Why don't you type something into your batch file and put ECHO in front of it, then run the batch file? You'll see what I mean when you run it; it simply displays what's written after it.
And now the ECHO. portion of the code. I'll put it this way: every time you see a "." after ECHO, it means "skip a line" Again, try adding this to your batch file and you'll get a better understanding of what it does.
The next command is PAUSE
What it does is it displays "Press any key to continue . . ." and waits for you to press a key before continuing. It's important to use a command like PAUSE in a batch file for this reason: without something like this, the batch file will open and then immediately close. Try adding PAUSE to the end of your batch file and then omit it.
The next line is IF NOT EXIST D:\ GOTO CHECK
Here you'll see the importance of sections. What this is saying is if there is still no CD in the CD-ROM drive, go to (GOTO) section CHECK
This will force the user to insert a CD into the CD-ROM drive before they can continue.
Now another section :START
Another CLS (clear screen), and then ECHO Copying files . . .
This does NOT actually copy files yet but is just displaying that sentence on the screen to let the user know what's going to happen.
You'll again see ECHO. to skip a line.
The next line has something new: IF NOT EXIST D:\Documents mkdir D:\Documents
First, I will explain what's supposed to happen during the backup process. All of the files being copied from My Documents will be copied to a folder on the CD called Documents, so what this is saying is that if the folder "Documents" does not exist on the CD, just create a folder called "Documents" on the CD. And that's that.
You'll next see the most important command in the code: XCOPY "C:\My Documents" D:\Documents /D /R /S /Y
XCOPY is a command telling the computer to copy.
"C:\My Documents" is the folder that files will be taken/copied from. (Note the quotation marks, which MUST be used when there is a space in the location).
D:\Documents is where these files will be copied to.
/D /R /S /Y are parameters for the XCOPY command. You can find out more about the parameters for the XCOPY command by opening a Command Prompt and entering XCOPY /?
Here is an overview of what the parameters I have chosen to use are:
/D basically does this: when copying the files to the CD, it only copies files that have been changed or that have not been copied to that CD yet. This keeps the program from copying everything in My Documents to CD every time the program is run.
/R overwrites read-only files.
/S copies all directories and subdirectories, except empty ones.
/Y suppresses prompting to copy files.
So, you can look at this line this way: XCOPY "backthisup" "savebackuphere" /parameter
The next command in that line is > D:\log.txt
This command is not necessary but is just used to introduce you to redirectors. The > is a redirector that tells the batch file to store the output of the command before it in a text file. So, this command tells the batch file to create a text file called log.txt on the CD and to send a list of all copied files to that text file. Again, try running the batch file to see what happens.
You'll again see ECHO. to skip a line.
The next line is ECHO Backup complete! which will simply display that sentence on the screen.
You'll recognize the rest of the commands in the code. Just note the beginning of the next section by :EOF (you can replace EOF with another word, but I use it to stand for 'end of file') and note the EXIT command at the end of the batch file, telling the program to exit.
You'll also want to note IF EXIST D:\ GOTO EOF
You've seen this before, but another explanation: What this is saying is that if the CD is still in drive D:\ then go to (GOTO) section EOF (the screen will be cleared a second time and "Remove CD now" will be displayed a second time; the commands in the rest of the section will not be executed until the CD is removed)...
Finally, let's go back to the first section, :MENU
You should already be familiar with most of the commands in the MENU section, although there are some new commands to learn as well.
First, the SET /P INPUT= command is used to create the variable INPUT. This variable (INPUT) can be renamed to any one word you want, although I prefer to use INPUT, standing for user input.
The following line is IF "%INPUT%" == "1" GOTO CHECK
This says to go to section CHECK if the user typed in 1
IF "%INPUT%" == "2" EXIT says to exit the program if the user typed in 2
The rest of the commands in this section; rather, the commands after the above line, give instructions on what to do if the user types in a character other than 1 or 2. For example, if the user typed in 3, the screen would be cleared, Invalid choice. would be displayed on the screen, the user would be prompted to press any key to continue, and the batch file would go to the MENU section.
Congratulations! These are the basics. It's difficult to understand everything at first, but if you read through this a few more times and start testing things out on your own, I promise you'll get a better feel for batch files. Please feel free to ask any questions if any of you would like to attempt to make your own batch file. The key to writing one is to know exactly why you need it, to plan it out, and to then edit/add lines here and there to make it even better ;)
Ray