Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Top Posters

Who's Online (1)

Powered by Vanilla. Made with Bootstrap.
[Batch] Batch Files - Introduction
  • Batch Files
    Batch files allow Microsoft Windows users to create a lists of commands( MS-DOS commands) to run in sequence once the batch file has been executed. For example, a batch file could be used to run frequently run commands, deleting a series of files, moving files, etc.

    A good example of a batch file for someone who is more familiar with Windows to think of a batch file as a shortcut in Windows. Much like a shortcut, batch files could be used to run one or more commands and/or programs through the command line(cmd).

    Creating a Batch File:
    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.
    1)Open Notepad
    2)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

    3)Click File and click Save; browse to where you want to save the file. For the file name, type test.bat , and in "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. Otherwise type “test.bat” including the double quotes.
    4)Now, to run the batch file, simply double-click or run the file like any other program. Once the batch file has completed running it will close the window automatically.

    [color]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.

    :: One of two ways of adding remarks into the batch file without displaying or executing that line when the batch file is run.

    :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. Mostly used with 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 Allows for batch files to prompt the user to select one item from a set of single-character choices.
    CLSJust like the DOS command would clear your screen.

    ECHO It is used to print a message to the user. Messages are displayed to the user by preceding a line of text with ECHO.

    EXIT Exits out of the window

    GOTO LABEL Used to go to a certain label, such as LABEL.

    IF Used to check for a certain condition if the condition exists. If that condition exists it will perform that function.

    PAUSE Prompt the user to press any key to continue.

    REM Another of adding remarks into the batch file without displaying or executing that line when the batch file is run.
    Changes the position of replaceable parameters in a batch program.

    START Used to start a windows application; such as START %windir%\system32\calc.exe would run the Windows Calculator.

    Batch file examples

    Two modes of batch file Batch files use two echo 'modes'. The default echo mode is ECHO ON. When ECHO is set to ON, every command in the batch file is displayed to the screen before it is run. Sometimes this information is not required, and can even be downright annoying for larger batch files. The command ECHO OFF sets the batch echo mode to OFF. In this mode, the commands are not printed to the screen prior to their execution
    @echo off
    echo \"echo mode off\"
    pause
    Above example wont print the codes in the window but the following will print code in the window.
    @echo off
    echo \"echo mode off\"
    pause


    Using Pause 'Pause' will pause the batch file,if you press a key the batch will continue
    You just try the following simple code

    @echo off
    echo Pause demo
    pause
    echo I hope you had understand
    pause


    Using cls

    @echo off
    echo Demo for cls
    pause
    cls
    pause


    Displaying text in message box:

    @echo off
    echo Message box test
    msg * The End
    pause
  • Great tutorial dude :)
  • Thanks Bursihido. I hope it will be useful for newbies like me.
  • undead
    Posts: 822
    Nice tut siva ;) Good work.