How To Write A Simple Batch (.bat) File

Ads by Google

how to write a batch fileBatch files are the computer handyman’s way of getting things done. They can automate everyday tasks, shorten the required time to do something, and translate a complex process into something anyone could operate.

Since automation programs like AutoHotKey exist, many people have never written or taken the time to understand bat files, and many don’t even know what they do.

In this article, I’m going to show you how to write a simple batch file and present some basics that a user will need to understand when writing one. I’ll also provide you with a few resources for learning to write batch (.bat) files in case you’d like to go further.


Let’s say that you frequently have network issues; you’re constantly getting on the command prompt and typing in things like “ipconfig” and pinging Google to see what the problem is. After a while you realize that it would be a bit more efficient if you just wrote a simple BAT file, stuck it on your USB stick, and used it on the machines you troubleshoot.

Step 1: Create A BAT File

Create a new text document on your desktop. Double click the file – it should be blank inside. Now, go to file>save as, and in the “Save As” window, input a name for your BAT file and then add a “.bat” on the end (without the quotes). My file was named testBAT.bat, for instance.

Before hitting save we need make sure that Windows doesn’t stick the standard “.txt” ending on the end of our new file. To do this, change the filetype from “.txt” to “all files” as shown in the screenshot below. That’s it – hit save and close the file.

how to write a batch file

Ads by Google

Step 2: Learn Some Quick Code

If you know how to run commands in the command prompt, you’ll be a wiz at creating BAT files because it’s the same language. All you’re doing is telling the command prompt what you want to put in through a file, rather than typing it every time you run the command prompt. This saves you time and effort; but it also allows you to put in some logic (like simple loops, conditional statements, etc. that procedural programming is capable of conceptually).

There are SEVEN simple commands I want to familiarize you with for this program. Commands are NOT case sensitive, so don’t worry about that.

TITLE - The Window name for the BAT file.

ECHO - the “print” statement for BAT files. Anything following the word ECHO will be displayed in the command prompt as text, on its own line.

ECHO OFF – BAT writers typically put this at the beginning of their files. It means that the program won’t show the command that you told it to run while it’s running – it’ll just run the command. I’d recommend that after you run this test program, you try removing this line from your code to see what happens.

PAUSE - This outputs the “press any key to continue…” message that you’ve seen all too many times. It’s helpful because it pauses the BAT file execution until the user tells it to go again. If you don’t put this in your program, everything will speed by and end before you can see it. People typically put this in BAT files to give the user a chance to review the material on the screen before continuing.

CLS - Clears the DOS window (helpful if things get too cluttered!).

IPCONFIG – Outputs a lot of network information into your DOS box (network admins have dreams solely based off this command).

PING - Pings an IP, letting you know if your computer was able to contact it. This command also returns the latency (ping time) and by default pings three times.

Step 3: Do Some Quick Logic

We need to plan our program out. Any good programmer will think about the general framework of their program before they dash into things – it prevents them from making logic mistakes that are hard to back out of.

For this program, we want to check the computer’s network/internet settings with an “ipconfig /all” command, and then review that information by giving the user time to read everything. Afterwards, we want to ping google.com to figure out if we really truly have access to the internet. We’ll pause the program after this as well, because we want to know for sure that they saw it.  OK. Very simple program, very simple logic. Let’s write some code.

Step 4: Write Your BAT File

Right click your BAT file and click “edit” to bring up Notepad. The whole document should be blank – ready for some epic programmer input.

Rather than walking you line by line through the code (it’s extremely short) I’m going to use a code comment (example–   CODE  ::Comment) to let you know what we just did.I’m putting the actual code in bold to make things a bit easier to process.

———–Start Code———–

ECHO OFF
::CMD will no longer show us what command it’s executing(cleaner)
ECHO As a network admin, I’m getting tired of having to type these commands in! Hopefully, this saves me some time in the long run.
:: Print some text
IPCONFIG /ALL
:: Outputs tons of network information into the command prompt
PAUSE
:: Lets the user read the important network information
PING www.google.com
:: Ping google to figure out if we’ve got internet!
ECHO All done pinging Google.
::Print some text
PAUSE
:: Give the user some time to see the results. Because this is our last line, the program will exit and the command window will close once this line finishes.

———–End Code———–

Step 5: Run Your BAT File

Save the file you just coded (or copy/paste mine in, it will run as written), and double click it. Your output should be something like the screenshot below.

how to write a batch file

Congratulations! You’ve written a batch file successfully.

If you want to learn more about bat files, I’d recommend you check out the commands available to the language. From there, your best bet is to write your own, or follow more examples online. Feel free to comment here if you have a BAT-related question.

The most useful BAT I’ve made so far is one that allowed me to compile and run Java programs with a single command, saving me countless amounts of command typing in the long run (because I compile/run so often when programming). I’ve also made one that sets file associations up the way I want them when I plug in my flash drive to a PC – this makes it possible for my portable apps to be the default app right from the get-go with a new computer.

Have you written anything cool with BAT files before?

Join live MakeUseOf Groups on Grouvi App Join live Groups on Grouvi
Windows Troubleshooting
Windows Troubleshooting
28 Members
Windows_10
Windows_10
36 Members
Windows Hacks & Customization
Windows Hacks & Customization
47 Members
Best Windows Software
Best Windows Software
30 Members
Ads by Google
Comments (37)
  • John Smith Images

    Well, congrats and thanks. It’s always the most basic information that are the most difficult to get and you’ve done it perfectly.

  • Kaushal

    I really liked the post.

    After reading the post I would like to implement it in my work to make it easier and less time consuming. Can we update a column in the Database for the bulk of data at once?

    If yes, can you please provide the sample code for it. I would be very thankful for the same.

    Thanks in Advance. Kaushal

  • Mallinath

    Can anyone help me to create one batch file for connecting VPN automatically. Because we keep selenium test execution in the night. But due to some technical issues the vpn is getting disconnect automatically.

    For our automation testing we have to connect Junos VPN , login with credentials. and it should click on connect. Then my selenium automation script should continue our test execution,

    So I am looking for a script for batch file or java file which should run in selenium web driver.

    Please feel free to share your ideas to me at mallumulage@gmail.com

  • Khaleel

    Hi Nathan,

    When I gave input as 1 and enter, it is returning error like this.

    “ô1ö is not valid please try again” for all the value it is displaying this error. please advice.

    Regards,
    Khaleel

    • kumbaya

      It’s because (double and single) quotation marks you’ve copied from the text are not accepted in windows command prompt. Copy all script to text file and replace these characters:
      “ with ”
      ” with ”
      ’ with ‘
      ? with ‘

  • Nathan

    Here is a bat file ive been coding in my free time….

    title PC TOOLS (C) Marcus Meyer designed by NATHAN VAN NIEKERK Version 2.0
    @echo off

    color 0c
    cls
    echo (C) Marcus Meyer designed by NATHAN VAN NIEKERK
    echo.
    cls
    goto start

    rem ####################### START ######################
    :start
    echo (C) Marcus Meyer designed by NATHAN VAN NIEKERK
    echo.
    color 0a
    echo %name% WELCOME TO PC TOOLS!!
    echo What option would you like to execute?
    echo.
    echo NETWORKING:
    echo _____________
    echo 1) Ipconfig
    echo 2) Ping an ip address
    echo 3) Trace Root
    echo 4) NetStat
    echo 5) Get Mac address
    echo.
    echo SYSTEM INFO:
    echo ______________
    echo 6) Who am I
    echo 7) PC INFO
    echo 8) Performance Check
    echo 9) Disk Check
    echo 10) Driver Check
    echo 11) Mirror Drive
    echo 12) Recover Drive
    echo.
    echo WINDOWS TOOLS:
    echo __________________
    echo 13) Clear recycle bin and Temporary files
    echo 14) Hard drive Defrag
    echo 15) Start or End a Task
    echo.

    echo 16) Exit

    echo.
    set option=
    echo.
    set /p option=Select One:
    echo.
    if ‘%option%’==’1′ goto ipconfig
    if ‘%option%’==’2′ goto ping
    if ‘%option%’==’3′ goto tracert
    if ‘%option%’==’4′ goto netstat
    if ‘%option%’==’5′ goto getmac
    if ‘%option%’==’6′ goto whoami
    if ‘%option%’==’7′ goto pcinfo
    if ‘%option%’==’8′ goto perfmon
    if ‘%option%’==’9′ goto CHKDSK
    if ‘%option%’==’10’ goto DRIVER
    if ‘%option%’==’11’ goto MIR
    if ‘%option%’==’12’ goto recover
    if ‘%option%’==’13’ goto clearmgr
    if ‘%option%’==’14’ goto defrag
    if ‘%option%’==’15’ goto task
    if ‘%option%’==’16’ goto exit
    echo.
    ECHO “%option%” is not valid please try again
    goto start
    ECHO.

    rem ###################### Options #####################

    rem ###################### ipconfig #######################
    :ipconfig

    color 0c
    echo.
    ipconfig
    pause
    cls
    goto start

    rem ###################### ping #######################
    :ping
    color 0b
    echo If you dont know the ip adress of the target pc, you can find the other target IP Address, by using our program and option 1
    pause
    echo Please enter the IP adress you would like to ping?
    echo.
    Set /p ip=
    ping %ip%
    pause
    cls
    goto start

    rem ###################### tracert #######################
    :tracert
    color 0b
    echo Please enter web page you would like to trace?
    echo.
    Set /p trace=
    echo.
    echo Trace Root initiate
    tracert %trace%
    pause
    cls
    goto start

    rem ###################### netstat #######################
    :netstat
    color 0f
    echo.
    netstat
    pause
    cls
    goto start

    rem ###################### getmac #######################
    :getmac
    @echo off
    color 0e
    echo.
    getmac
    echo.
    echo Have you got all the info needed?
    pause
    cls
    goto start

    rem ###################### whoami #######################
    :whoami
    color 0d
    whoami
    pause
    cls
    goto start

    rem ###################### pcinfo #######################
    :pcinfo
    color 0e
    systeminfo
    pause
    cls
    goto start

    rem ###################### clearmgr#######################
    :clearmgr
    echo Just follow the On Screen instructions and Press OK, Press Delete and OK.
    CleanMgr
    echo DONE!!
    pause
    cls
    goto start

    rem ###################### defrag #######################
    :defrag
    echo You will need to run this program as Administrator
    echo This will Defrag all volumes:
    pause
    defrag /C /H /V
    pause
    cls
    goto start

    rem ###################### perfmon #######################
    :perfmon
    color 0b
    echo.
    echo Please wait program starting…
    PERFMON
    pause
    cls
    goto start

    rem ###################### CHKDSK#######################
    :CHKDSK
    echo Please enter the drive you would like to check?
    echo.
    Set /p DSK=
    echo.
    cls
    echo Check disk initiate
    CHKDSK %DSK%:
    pause
    cls
    goto start
    rem ###################### DRIVER#######################
    :DRIVER
    DRIVERQUERY
    pause
    cls
    goto start

    rem ###################### MIR #######################
    :MIR

    color 0c
    echo Please Enter the Source DRIVE
    echo.
    set /p SOURCE=
    echo Please Enter the Destination DRIVE
    echo.
    set /p DEST=
    ROBOCOPY %SOURCE%: %DEST%: /MIR
    pause
    cls
    goto start

    rem ###################### recover #######################
    :recover
    echo Please Enter the Source file path
    echo.
    set /p SOURCE=
    TYPE SOURCE
    pause
    cls
    goto start

    rem ###################### task #######################
    :task
    cls
    echo TASK KILL OR START TASK MENU
    echo.
    echo What option would you like to execute?
    echo.
    echo 1) Task Kill
    echo 2) start task
    echo 3) back

    echo.
    set option=
    echo.
    set /p option=Select One:
    echo.

    if ‘%option%’==’1′ goto taskkill
    if ‘%option%’==’2′ goto starttask
    if ‘%option%’==’3′ goto back

    ################# taskkill ###################
    :taskkill
    echo What process would you like to end?
    echo.
    set /p task_name= task name without .exe
    taskkill /f /im %task_name%.exe
    pause
    cls
    goto task

    ################# starttask ###################
    :starttask
    echo What process would you like to start?
    echo.
    set /p task_name=
    start %task_name%

    cls
    pause
    goto task..

    ################# back ###################
    :back
    cls
    goto start

    rem ###################### exit #######################
    :exit
    echo Are you sure you want to EXIT?
    pause
    exit

    • João

      I take de liberty to change a litle your batch file.
      i wope you don’t mind .
      you can add those if you want:
      displaydns
      flush dns
      ipconfig/realese
      ipconfig/renew
      routeprint
      and remote assistant as well
      title PC TOOLS (C) Marcus Meyer designed by NATHAN VAN NIEKERK Version 2.0
      mode 48,40
      @echo off
      color 0c
      cls
      echo (C) Marcus Meyer designed by NATHAN VAN NIEKERK
      echo.
      cls
      goto start
      rem ####################### START ######################
      :start
      echo (C) Marcus Meyer designed by NATHAN VAN NIEKERK
      echo.
      mode 48,40
      color 0a
      echo %username% WELCOME TO PC TOOLS!!
      echo What option would you like to execute?
      echo.
      echo NETWORKING:
      echo _____________
      echo 1) Ipconfig
      echo 2) Ping an ip address
      echo 3) Trace Root
      echo 4) NetStat
      echo 5) Get Mac address
      echo.
      echo SYSTEM INFO:
      echo ______________
      echo 6) Who am I
      echo 7) PC INFO
      echo 8) Performance Check
      echo 9) Disk Check
      echo 10) Driver Check
      echo 11) Mirror Drive
      echo 12) Recover Drive
      echo.
      echo WINDOWS TOOLS:
      echo __________________
      echo 13) Clear recycle bin and Temporary files
      echo 14) Hard drive Defrag
      echo 15) Start or End a Task
      echo.
      echo 16) Exit
      echo.
      set option=
      echo.
      set /p option=Select One:
      echo.
      if “%option%”==”1″ goto ipconfig
      if “%option%”==”2″ goto ping
      if “%option%”==”3″ goto tracert
      if “%option%”==”4″ goto netstat
      if “%option%”==”5″ goto getmac
      if “%option%”==”6″ goto whoami
      if “%option%”==”7″ goto pcinfo
      if “%option%”==”8″ goto perfmon
      if “%option%”==”9″ goto CHKDSK
      if “%option%”==”10″ goto DRIVER
      if “%option%”==”11″ goto MIR
      if “%option%”==”12″ goto recover
      if “%option%”==”13″ goto clearmgr
      if “%option%”==”14″ goto defrag
      if “%option%”==”15″ goto task
      if “%option%”==”16″ goto exit
      echo.
      ECHO “%option%” is not valid please try again
      goto start
      ECHO.
      rem ###################### Options #####################
      rem ###################### ipconfig #######################
      :ipconfig
      mode 70,40
      cls
      color 0c
      echo.
      ipconfig
      pause
      cls
      goto start
      rem ###################### ping #######################
      :ping
      cls
      color 0b
      @echo off
      mode 45,2
      color 0F
      set /p a=”Enter IP ADDRESS: ”
      MODE 60,15
      ping %a%
      echo [———————————————————-]
      pause
      cls
      goto start
      rem ###################### tracert #######################
      :tracert
      mode 60,5
      cls
      color 0b
      echo Please enter web page you would like to trace?
      echo [EXAMPLE] http://www.google.com
      echo.
      Set /p trace=
      mode 75,50
      echo.
      echo Trace Root initiate
      tracert %trace%
      pause
      cls
      goto start
      rem ###################### netstat #######################
      :netstat
      @echo off
      color 0F
      :options
      mode 45,10
      cls
      echo MENU OPTIONS:
      echo =====================
      echo a All links
      echo e Ethernet Statistics
      echo f Displays FDQN
      echo n Addresses and ports
      echo =====================
      echo q to quit
      echo =====================
      set /p input=”Enter Options Here: ”

      if “%input%”==”a” goto:a
      if “%input%”==”e” goto:e
      if “%input%”==”f” goto:f
      if “%input%”==”n” goto:n
      if “%input%”==”q” goto:start

      echo Please choose from the Options listed above!
      pause
      goto:options

      :a
      mode 80,60
      netstat.exe -a
      pause
      goto:options

      :e
      mode 80,15
      netstat.exe -e
      pause
      goto:options

      :f
      mode 80,25
      netstat.exe -f
      pause
      goto:options

      :n
      mode 80,20
      netstat.exe -n
      pause
      goto:options
      rem ###################### getmac #######################
      :getmac
      mode 60,25
      @echo off
      cls
      color 0e
      echo.
      arp -a
      echo.
      echo Have you got all the info needed?
      pause
      cls
      goto start
      rem ###################### whoami #######################
      :whoami
      mode 45,5
      color 0d
      whoami
      pause
      cls
      goto start
      rem ###################### pcinfo #######################
      :pcinfo
      mode 80,60
      cls
      color 0e
      systeminfo
      pause
      cls
      goto start
      rem ###################### perfmon #######################
      :perfmon
      mode 45,5
      cls
      color 0b
      echo.
      echo Please wait program starting!
      PERFMON
      pause
      cls
      goto start
      rem ###################### CHKDSK#######################
      :CHKDSK
      mode 48,5
      cls
      echo Please enter the drive you would like to check?
      echo.
      Set /p DSK=
      echo.
      cls
      echo Check disk initiate
      CHKDSK %DSK%:
      pause
      cls
      goto start
      rem ###################### clearmgr#######################
      :clearmgr
      echo Just follow the On Screen instructions and Press OK, Press Delete and OK.
      CleanMgr
      echo DONE!!
      pause
      cls
      goto start
      rem ###################### defrag #######################
      :defrag
      echo You will need to run this program as Administrator
      echo This will Defrag all volumes:
      pause
      defrag /C /H /V
      pause
      cls
      goto start

      rem ###################### DRIVER#######################
      :DRIVER
      mode 48,50
      cls
      DRIVERQUERY
      pause
      cls
      goto start
      rem ###################### MIR #######################
      :MIR
      color 0c
      echo Please Enter the Source DRIVE
      echo.
      set /p SOURCE=
      echo Please Enter the Destination DRIVE
      echo.
      set /p DEST=
      ROBOCOPY %SOURCE%: %DEST%: /MIR
      pause
      cls
      goto start
      rem ###################### recover #######################
      :recover
      echo Please Enter the Source file path
      echo.
      set /p SOURCE=
      TYPE SOURCE
      pause
      cls
      goto start
      rem ###################### task #######################
      :task
      mode 40,10
      cls
      echo TASK KILL OR START TASK MENU
      echo.
      echo What option would you like to execute?
      echo.
      echo T) Tasklist
      echo 1) Task Kill
      echo 2) start task
      echo 3) back
      echo.
      set option=
      echo.
      set /p option=Select One:
      echo.
      if “%option%”==”T” goto tasklist
      if “%option%”==”1″ goto taskkill
      if “%option%”==”2″ goto starttask
      if “%option%”==”3″ goto back
      ################# tasklist ###################
      :tasklist
      mode 48,50
      cls
      echo The fowlling process are actives!
      tasklist
      pause
      cls
      goto task
      ################# taskkill ###################
      :taskkill
      echo What process would you like to end?
      echo.
      set /p task_name= task name without .exe
      taskkill /f /im %task_name%.exe
      pause
      cls
      goto task
      ################# starttask ###################
      :starttask
      echo What process would you like to start?
      echo.
      set /p task_name=
      start %task_name%
      cls
      pause
      goto task..
      ################# back ###################
      :back
      cls
      goto start
      ###################### exit #######################
      :exit
      mode 45,5
      cls
      echo Are you sure you want to EXIT?
      echo Select c to cancel or press ok to exit
      set /p input=”Enter Options Here: ”
      if “%input%”==”ok” goto:ok
      if “%input%”==”c” goto:start

Load 10 more
Affiliate Disclamer

This review may contain affiliate links, which pays us a small compensation if you do decide to make a purchase based on our recommendation. Our judgement is in no way biased, and our recommendations are always based on the merits of the items.

For more details, please read our disclosure.
Affiliate Disclamer

This review may contain affiliate links, which pays us a small compensation if you do decide to make a purchase based on our recommendation. Our judgement is in no way biased, and our recommendations are always based on the merits of the items.

For more details, please read our disclosure.