Tuesday, January 31, 2012

Batch Files

Creating a batch file

MS-DOS users
Microsoft Windows and other users

MS-DOS users

To create a basic batch file in MS-DOS, follow the below steps that give you an example of how to create a basic batch file.

    Open an MS-DOS command window or get to MS-DOS.
    At the MS-DOS prompt, type: edit test.bat and press enter.
    If typed properly, you should now be in a blue screen. Within the screen, type:

    pause
    dir c:\windows
    dir c:\windows\system

    Once the above three lines have been typed in, click File and choose exit; when prompted to save, click "Yes." Users who do not have a mouse cursor can accomplish this same task by pressing ALT+F to access the file menu, then pressing "X" to exit, and pressing enter to save changes.
    Once you are back at the MS-DOS prompt, type: test and press enter. This will execute the test.bat file and begin running the file. Because the first line is pause, you will first be prompted to press a key. Once you press a key the batch file will run line-by-line; in this case, listing the files in the windows and windows\system directories.

If you wish to add more lines to this batch file you would type "edit test.bat" to edit the file again.

Additional information about the MS-DOS edit command can be found on our edit command page. Some versions of MS-DOS and bootable diskettes may not have the edit command; if this is the case, you would either need to obtain the edit.com file to access this file or use the copy con command.

Microsoft Windows and other users

A Windows user can still use the above MS-DOS steps if they wish to create a batch file. If, however, you're more comfortable using Microsoft Windows or your operating system, you can use any text editor, such as Notepad or Wordpad, to create your batch files, as long as the file extension ends with .bat. In the below example we use the Windows notepad to create a batch file.

    Click Start
    Click Run
    Type: notepad and press enter.
    Once notepad is open, type the below lines in the file or copy and paste the below lines into notepad.

    @echo off
    echo Hello this is a test batch file
    pause
    dir c:\windows

    Click File and click Save; browse to where you want to save the file. For the file name, type "test.bat", and if your version of Windows has a "Save as type" option, choose "All files", otherwise it will save as a text file. Once all of this has been done click the Save button and exit notepad.
    Now, to run the batch file, double-click or run the file like any other program. Once the batch file has completed running it will close the window automatically.

Batch commands

Just like all MS-DOS commands, all batch file commands are not case sensitive. However, in the below listing we have listed all commands in all caps to help you identify what is a command and what is not.
@    Does not echo back the text after the at symbol. This is most commonly used as @ECHO OFF to prevent any of the commands in the batch file from being displayed, just the information needed.
%1    The percent followed by a numeric value, beginning with one, allows users to add variables within a batch file. The below line is an example of what can be used in a batch file.

ECHO Hello %1

When the above one-line batch file is created, add your name after the batch file. For example, typing myname (being the name of the bat file) and then your name:

myname bob

would output:

Hello bob

Note: This can be extended to %2, %3, and so on.
::    One of two ways of adding remarks into the batch file without displaying or executing that line when the batch file is run. Unlike REM this line will not show regardless if ECHO off is in the batch file.
:LABEL    By adding a colon in front of a word, such as LABEL, you create a category, more commonly known as a label. This allows you to skip to certain sections of a batch file such as the end of the batch file. Also see GOTO.
CALL    This is used to run another batch file within a batch file. When the batch file that is called is completed, the remainder of the original batch file is completed. Note if the batch file does not exist it will give an error message.
CHOICE    See running different programs for an example of how to use this command.

    Additional information and the syntax of this command in each version of Windows and MS-DOS can be found on our CHOICE command page.

CLS    Just like the DOS command would clear your screen.

    Additional information and the syntax of this command in each version of Windows and MS-DOS can be found on our CLS command page.

ECHO    Will echo a message in the batch file. Such as ECHO Hello World will print Hello World on the screen when executed. However, without @ECHO OFF at the beginning of the batch file you'll also get "ECHO Hello World" and "Hello World." Finally, if you'd just like to create a blank line, type ECHO. adding the period at the end creates an empty line.
EXIT    Exits out of the DOS window if the batch file is running from Windows.

    Additional information and the syntax of this command in each version of Windows and MS-DOS can be found on our EXIT command page.

GOTO LABEL    Used to go to a certain label, such as LABEL. An example of GOTO would be to GOTO END. For an example of this see running different programs.

    Additional information and the syntax of this command in each version of Windows and MS-DOS can be found on our GOTO command page.

IF    Used to check for a certain condition if the condition exists. If that condition exists it will perform that function. To see an example of this see running different programs.

    Additional information and the syntax of this command in each version of Windows and MS-DOS can be found on our IF command page.

PAUSE    Prompt the user to press any key to continue.
REM    One of two ways of adding remarks into the batch file without displaying or executing that line when the batch file is run.
SHIFT    Changes the position of replaceable parameters in a batch program.

    Shift command help and syntax information.

START    Used for Windows 95, Windows 98, and Windows NT 4.0 and above to start a windows application; such as START C:\WINDOW\CALC would run the Windows Calculator. Users running Windows 3.x need to utilize the WIN command.  For example, WIN C:\Windows\CALC.EXE would run Windows and then Calculator after Windows has finished loading.

    Start command help and syntax information.

No comments:

Post a Comment