How to Write a batch (.bat) File: Step by Step Guide to Create Batch File

Discussion in 'Windows Know Base' started by securityhope, Jan 16, 2017.

Tags:
  1. securityhope

    securityhope Administrator Staff Member

    Joined:
    Aug 3, 2016
    Messages:
    1,241
    Likes Received:
    0
    Trophy Points:
    36
    Batch files are the new shortcut to the success. I mean, batch files can automate your everyday task resulting in lesser time consumption for doing the task, which would have consumed nothing less than few minutes.

    For example, with the help of a batch file you can easily switch between OS’s if you are on a dual booth in few seconds, which would have otherwise taken few minutes of time. Now, if you want to learn how to create batch files and write your own batch file, here is how to do it.

    Batch files are made so that anyone can do a complex task without going through all the commands. You create a batch file, run the batch file and you are done.

    Batch files are nothing new to the computer world, however, the number of people who actually use them are countable. Many people are not aware of creating or working of the batch file because they are found of automation programs, which do the task for them. You can know more about Batch files here.

    How to Write a batch (.bat) File

    In today’s round, we will try to learn to write a batch file, which can do a basic task of using IPconfig command to display your computer’s IP address, subnet mask etc details in Windows 8 along with some basics to understand the working of a batch file (.bat) while creating their own.

    The IPconfig command is useful if you are on Windows 8. By creating a batch file to run IPconfig, you can avoid the hassle of going through the command prompt window and entering the command each time. Simply run the batch file and your PC will show the IP config details. You can also create a batch file to restart your PC the same way.

    Learn the Basic Commands for Creating a Bat file.

    Knowing commands and how to run them in the command prompt window is an essential skill to create a batch files. There are 5 simple commands that as a beginner we should know as mentioned below.
    • TITLE – Title will be the Window name for your Batch file.
    • ECHO – If you are aware of the print statement in programming, ECHO does that work here. ECHO is used to display any text or comment in command prompt.
    • ECHO OFF – This command would hide the actual commands you are running using the Batch file. This is optional.
    • PAUSE – The PAUSE command would wait the user to press any key before exiting the Windows or executing / running the next command. You might have seen “Press Any Key to Continue” command earlier as well. This is a recommended command, as it will allow the user to see that the commands are running. Else, the file would just execute itself and disappear without even letting anyone know.
    • IPconfig – This is the command we are going to use for creating the bat file, which will show IP details of your PC.
    1. Start with Creating a BAT file (.bat)

    Note that we are not writing our Batch (.bat) file yet but are simply creating one empty batch file.

    You can create a batch file using your text editor such as notepad. Begin with creating a new text document. Open notepad, go to file and click on Save as. Next, enter a name for your file with the extension as .bat (like Ipconfig.bat). Before you click on Save, change the Save as type to “All Files” which should be .txt by default. Once done, click on Save button to create the Batch (.bat) file.

    [​IMG]

    2. Write your Batch (.bat) File

    Now that you have created an empty batch file, it is time to write some code. Before you start writing your code / command always plan what you’re going to do and how you want it to work. Once the blueprint is ready, you can work on your actual project.
    Right, click on the Batch file you just created and select Edit. Now you are ready to write your code in the batch file. Here is how to do it. Hit enter after each line of command.

    TITLE IPconfig

    ECHO OFF

    ECHO find your Ip config details below.

    PAUSE

    IPconfig

    PAUSE

    Once you have typed all the commands, click on File and select Save. Once saved, double-click on the ipconfig.bat file to run it and your PC will show the IP details as soon as you press any key.

    There you go! You have created your own batch file to show IP details on your Windows 8 PC. You can find several already created batch files on the internet and analyse them to learn the commands and codes. That’s it for today’s tutorial, you can let us know about your first batch file in the comments below.
     
    Last edited: Jan 16, 2017

Share This Page

Share