Sunday 17 April 2016

Batch File In A Nutshell

Welcome to my tutorial. I will go over just about everything you can do with a batch file from simple commands to utilizing those in every day use, and very complex batch files to automate any process. This tutorial is geared towards batch files in a Windows XP environment. So lets get started.

Chapter 1 - What is a Batch File?

In DOS, OS/2, and Microsoft Windows, a batch file is a text file containing a series of commands intended to be executed by the command interpreter. When a batch file is run, the shell program (usually COMMAND.COM or cmd.exe, or Wscript.exe or cscript.exe) reads the file and executes its commands, normally line-by-line. Batch files are useful for running a sequence of executables automatically and are often used by system administrators to automate tedious processes. Unix-like operating systems (such as Linux) have a similar type of file called a shell script.

(Source Batch File Wikipedia )

What this means is you create a text document (using notepad or any simple text editor in ANSI format) with an extention of "bat" so it would be myfile.bat. Now why not just delve into the programming?

Chapter 2 - Hello World

Ok as will ANY programming language, we will create your very first batch file. Of course we will do the "Hello World" Application. So what we need to do is create a new text document and open the file in notepad. Now I will go line by line explaining what each of those lines do.

Line One:
Code
@echo off

What that command does is will hide the command echo, so when you want to display text, it wont display the "echo" command or what is happening, some simply when you write the command, the command wont be visable only the output you designate.

Line Two:
Code
title Hello World

This command sets the title of the command prompt to whatever you choose, I chose "Hello World" because that is the name of the file we are making. This command should be pretty straight foreward, your setting a title for the application

Line Three:
Code
echo Hello World

This command is actually what we are displaying in the command prompt screen which is simply displays "Hello World" the echo command is one of the most common you will come across in batch scripting. I will go in further detail in the next chapter.

Line Four:
Code
echo.

This command will create a blank line (like a line brake in HTML <br /> )

Line Five:
Code
pause

This command is another common one that does exactly what it says, it will pause any further commands until a user pastes any key to continue. This is similar to the WAITKEY command in various programming languages.

So the final batch file code will look like this:
Code
@echo off
title Hello World
echo Hello World
echo.
pause

And the output like this:


Ok that's it, your very first Hello World Batch File. Now to the next Chapter.

Chapter 3 - Common Commands

Ok as said before I will now be going in depth of the most commonly used commands in a batch file. Chances are in each of your batch files they will contain these commands. So lets learn the basics.

@

This commands is used in the begginning (usually) in front of the ECHO command as in "@ECHO OFF" and what it does is not echo back the text after the "at" symbol. And the purpose of the "@ECHO OFF" to prevent any of the commands in the batch fie from being displayed, just the information that is needed.

ECHO

This command has 3 functions, "ON", "OFF", and "MESSAGE". For example at the beggining of the batch file if it has "@ECHO ON" then it will disply each line of the batch on screen, however that command is not needed because if you just remove it, it is default so it will do that anyways, a real life application for that is when your debugging a batch file and just change OFF to ON in the first line just for simplicities sake. Now the OFF function will only display the output on the screen. And finally "MESSAGE" is a string of characters to display (remember the Hello World file). Wait, you think I'm done with echo? Hahaha, no way I'm just getting started. You can even use echo to insert text into a file. For example:
Code
ECHO Word word word word>Filename.txt

That will create a text document with "Word word word word" in Filename.txt Now you can also use ECHO to display a variable, for example:
Code
echo %temp%
will display the location of your Temperary files that the %temp% variable points to. You can even have echo make a sound. To do this is the following command:
Code
ECHO �

You use the 'Alt' key, followed by a 7 on the numeric keypad to get the 'ascii 7' character. Lastly I would like to touch on the blank line. As stated in the second chapter you do the following command to produce and empty line:
Code
ECHO.

CLS

This command can be used a lot or very little, myself I use it A LOT, like the echo command its in every single one of my batch files. its use is simple, there are no parameters, its simply "CLS" you add it on a line to "CLear the Screen"

CD

Ok this command is to change the current directory to another. Another commonly used command in batch files (or navigating through a CLI or Command Line Interface, as opposed to GUI or Graphical user Interface). The Syntax is as follows:
Code
CD [/D] [drive:][path]
CD [..]

The /D parameter changes the current DRIVE in addition to changing folders. Lets say your batch file is in the root of the drive, which we will call "C:\" however you want to create a batch file that deletes temperary files, so we need to be in the temp file for the computer, now here you can do one of two things, the first more complicated way is to use the "CD" command and do the following:
Code
CD /D "C:\Documents and Settings\User\LocalData\temp"

Or more simply, and also universal for all computers:
Code
CD %TEMP%

Then there is also the 'CD ..' which will continue to the parent directory, for example
Code
C:\Working\Directory>cd .. [press enter]
C:\Working\>cd .. [press enter]
C:\>

So it goes up a directory. Now another function that I use with CD is the following:
Code
CD\

This command with bring the current directory to the root directory. I include this usually right after '@ECHO OFF' in my batch files.''


Bye!

No comments :

Post a Comment

Super Blog Directory