More Than Basicbatchfiles


Many of us have heard or perhaps known about the batch file, but very few are aware of its power and dominance in Windows. Almost everything can be done when we know relevant command line instructions. So in this series of tutorials, we will learn about batch file programming and how we can execute command line instructions with a single click through them.

  1. Create A Batch File
  2. Best Batch Files
Batch file Introduction
Batch file Commands
Batch file Variables
Batch file If else
Batch file Loops
Batch file Return Codes
Batch file Functions

This is more like writing an actual small program than a quick and dirty script. That’s one reason why.bat files are sometimes called “batch programs.” If you want to do something more complex, you’ll find plenty of guides to doing specific things with batch programming online. @LogicDaemon There is a switch to stop prompting for more files /M Do not prompt for compare more files. – tiboo Feb 7 '20 at 11:41 @tiboo it only appeared in some recent Windows 10 version, I guess in 2019 at least. Personally I find it a lot more convenient to read the help info in file form. Also, I very much agree with Mike B's proscription against using batch files too much. It's almost always better to use a more robust language than batch for any task that isn't trivial. I began my career in IT using code (Visual Basic, BATch files, and the like) to solve problems for a financial services firm in the days when dial-up was a thing. I have held positions of increasing responsibility at two major regional banks, including managing a team of engineers responsible for 1,000 servers and more than 6,500 desktop. Batch file multiple commands for loop.

Batch File Programming – Introduction

A batch file is an unformatted text file or script file which contains multiple commands to achieve a certain task. It contains series of command that is executed by command line interpreter.

Extensions:.bat or .cmd

The instructions in batch files are for automating repetitive command sequences.

Before the implementation of modern GUI’s ( Graphical User Interface ), in the operating system like MS-DOS, we had to operate every command from command line. Even though we are facilitated with GUI’s, many major core operations can only be achieved through command line instructions.

So whenever we write instructions or codes in batch files, we are executing command line operations through our instructions and when we know how to write commands, we can do many powerful things in the Windows.

For example: We can create a .bat file with instructions of shutting down and whenever clicked in that file, Windows will automatically shut down.

Sounds fun, right?

How to create a batch file?

Well as simple as it sounds, you don’t need any extra software installed to create a batch file.

Just open up a built-in text editor for windows.

After writing commands, all you need to do is save it as a .bat or .cmd file.

Voila, you have created your first ever batch file. But you haven’t put any instructions. We will cover about programming and scripting in next articles.

Now that you know how to create a batch file, you must be wondering how to run it?

How to run a batch file?

It may sound funny, but all you have to do is click that file to run it and Windows will automatically run the commands written in a batch file.

A batch file can also be run via command prompt. In order to execute the batch file from command prompt, we must set the path to the directory where the batch file is stored or we should include the path address to that directory.

Let’s create a simple batch script to display “This is my first script”.

  • First, open up a text editor and save the file as a .bat file
  • Copy and paste the code above and click on that file to run the script

    It will generate an output like this.

Explanation of the program

Line 1:

If we don’t put @echo off at the top of the script, it will produce an output where ‘echo’ itself will also be displayed. And the output becomes like:

So to avoid the display of command itself, we must use @echo off at the top.

Line 2:

Line 2 just echoes ‘This is my first script’ onto the console.

Line 3:Pause is used to hold the screen until we press a key. If pause is not used, the output screen will vanish away within a blink of an eye and we won’t be able to see the output.

Batch File Programming – DOs and DON’Ts

You must always follow best programming practice while writing codes, be it batch file programming or any.

Even in short programs, we must maintain the habit of following better practice because while we write huge programs, it becomes a nightmare to debug it and also maintain it because no one else will understand your code if not properly documented.

So here are the few things that must be implemented and few things that must be avoided while coding.


  • Documenting code with comments
    Perhaps this is one of the most important one because without proper documentation it becomes tedious to maintain the code and debug it .
    So it is always a good idea to insert comments in programs or codes, explaining what the next lines or block of code is trying to accomplish, how and why.
    Either REM or : : is used for comments in batch file programming. Here is the example.
  • Validating input
    As simple as it is to code batch files, so is to hack and tweak the code because everything is like plain English in batch programs. Batch files are weakly typed, so it is always a better approach to validate all the inputs in batch file programming.
  • Check variables before using them
    Always check the new variable before using them or initializing them because they might already have been defined.
  • Indentation
    Without proper indentation, a program becomes confusing to intercept. So always use proper indentation in every line of code for better understanding of the code.


If there are few things to be considered for better programming practice, there are also certain things that must be avoided. Here are the few things that you as a programmer should try to avoid while coding.

  • Avoid one-liner codes (multiple command lines joined into a single one by ANDs and ORs) and use a block of code.
  • Avoid nested block codes (nested if else) and use subroutines instead.
  • Don’t use variable names as command names

Some final words of advice before you start playing around batch commands

Now that you have known about the batch files, in the coming tutorials, you will learn about advanced concepts in batch file programming.

But we should warn you that, batch file commands and scripts are too much powerful and if used without proper knowledge, it can crash your machine and software’s functionalities. Make sure you know what you are doing because with batch scripts we are playing with the core feature of Windows.

In this tutorial, you will learn about batch file commands and how they are used in batch file scripting or programming.

As discussed in the previous tutorial, a batch file is an unformatted text file or script file which contains multiple batch file commands or instructions to achieve a certain task. It has extension of .bat or .cmd.

Click here to go through the introduction of the batch file before learning batch file commands.

Batch file commands: Windows/DOS

For the ease of learning, we have listed all the batch file commands with relevant examples and explanations below. Please click on the commands to know the details.


Note: Batch file commands are not case sensitive


The batch command ASSOC associates a file extension with a file type, or list all associations.



As shown in above output, it displays the file association for .txt extension.

If only ASSOC is written and executed, it will display all the file associations for every extension, instead of just .txt extension.


The batch command ATTRIB is used to display the file attributes or set an attribute to a file in the working directory.


Now let us suppose we have a file note.txt in our working directory. We will display its file attributes and then make it hidden and read only by adding 'ah' and 'r' attributes to it. And finally, we will remove some attributes we added as well.


Here in this output, A means Archived, R means Read only and AH means Hidden file.


The batch command CD helps in navigating through different directories and changing directories or displaying current directory.




The batch command CHKDSK is used for checking error in the disk.



The batch command CHOICE provides a list of options to the user.



Now that script will produce following output.

Now the console waits for your input and once you enter your answer it will terminate.


The batch command CLS clears the screen.


This command just clears all the logs in command prompt screen.


The batch command CMD invokes a new command prompt window.



The batch command COMP compares the size of two files and checks if they are different in size.



The batch command CONVERTS the volumes or drives from one format to another i.e from FAT to NTFS.



The batch command COPY is used for copying files from one location to another.



The batch command DATE displays the current date in the system.



This command DATE displays system date in command prompt as shown above.


The batch command DEL is used for deleting files.


Note: DEL command only deletes files, not directories.


The batch command DIR lists all the contents of directories.



The batch command DISKPART shows the properties of a disk partition.


This script will ask for users permission to check the properties of disk partition and if allowed, will display following output in the console depending on disk properties.


The batch command DRIVERQUERY displays all the drivers installed and their properties.



This output shows the fraction of drivers list with their properties and installed date. DRIVERQUERY command shows all the drivers list, which is huge.


The batch command ECHO is used for echoing commands on/off and printing message to the console.



This command ECHO displays Hello in the console as shown above.

Besides printing message, echo is also used for deciding whether or not to display the command itself. Like in this example as well, in the first line we have turned OFF echo, which makes sure that commands themselves are not printed.

If that echo wasn’t turned off, then the output would have been like:


The batch command EXIT terminates and exits the console.

How to do a mail merge from excel. Example

In this example, as soon as HI is printed in the console, EXIT command will terminate the program and close the output console.


The batch command EXPAND extracts the contents of .cab file.


This script will extract all the contents of file to the same location where is located.


The batch command FC finds the difference between the two files and displays them to console.


This script will find the difference in the content of both files and list out all of them.


The batch command FIND search the given file to find the desired string and if located, it displays the corresponding line in which the string exists.


This script will search for the string “find me” in example.txt file and if it exists in example.txt, it will display the corresponding line on the console.


The batch command FORMAT is used for formatting a drive of format FAT 16/32 or NTFS in Windows.


This script will format E drive and overwrite previous contents.


This might be the one of the most important batch file commands because with this HELP command we can know about all the other commands used in batch file or command prompt.


Now this will display all the available commands with their functionalities in the console.

Since the list of commands is so much more, we have sliced the list and shown few here.

Now that we have a list of batch file commands, we can also view details of their syntax and functionalities in following way:

Now this will display details of the copy command.

As you can see, it HELP COPY displays all the details about COPY command.


The batch command IPCONFIG displays Windows IP configuration.


Create A Batch File

This script will generate following output.

P.S: We have hidden iPV6 address in above output.

Best Batch Files


The batch command LABEL displays the label of a drive or volume and is also is used for adding, setting or removing a disk label.


Now this will display the label of your working directory and you can set, remove or add another label.

For example, my working directory is D: and has label ‘apps’. So, it will generate following output:


The batch command MD creates a new directory or folder in the working directory.


This program will create a new directory abc in current working location.


The batch command MORE displays the content of a file one by one.


This program will display the contents of example.txt line by line, one at a time.


This batch command moves files from one directory to another, rename the directories and move the directories as well.


In this way, MOVE command can be used to move files, directories and rename directories.


The batch command NET is used for many network functionalities depending upon the commands used.


To display the users:

This will generate following output:

Like users, there are many other commands:

  • net accounts
  • net computer
  • net config
  • net continue
  • net file
  • net group
  • net help
  • net name
  • net pause
  • net print
  • net send
  • net session
  • net share
  • net start
  • net statistics
  • net stop
  • net time
  • net use
  • net view


The batch command PATH displays the path variable or it can be used to set path variable.


This program will display the path of the current working directory.


The batch command PAUSE is used for holding the output screen till user enters a variable or a value.


This program will print hi in the console and show the message ‘Press any key to continue.’ and wait for the input from the user.


More Than Basicbatchfiles

The batch command PING is used for sending ICMP/IP packets to the designated address over the network.



This script will send packets to address and output will be displayed as follows:


The batch command RD is used for removing the empty directories, directories with contents or files inside cannot be removed with RD command.



The batch command REM signifies comments in the batch script.


Anything written after REM is interpreted as a comment and is not executed in batch programs.


The batch command REN is used for renaming files and directories.



The batch command SET displays the list of environment variables of the system.



The batch command SHUTDOWN when invoked, shuts down the computer.



The batch command SORT is used to sort the content of the file alphabetically.


This script will sort the content of example.txt alphabetically either in ascending or descending order.


The batch command START is used to open a file or start a new program.


This program will start the application paint if it is in the working location, else you will have to explicitly indicate the path of that program as well.


The batch command SYSTEMINFO displays all the configuration of the computer and operating system.


This will generate following output depending upon the computer:

Of course, the details will be much more than this, but please try and look on your PC.


The batch command TASKKILL is used to terminate a running task


If you were to terminate the notepad running in your PC, then following script is used.


The batch command TASKLIST lists all the running tasks in the console.



The batch command TIME is used to display or set the system time.



The current time is displayed in the console.


The batch command TITLE sets new title for output console.


This script will set the title of output console to ‘New Console’. Thus the output console will look like:


The batch command TREE displays the tree diagram of the subdirectories to the last level.


I have a folder movies. SO, if I were to run TREE in that movie folder, it will create a tree of movies inside that folder and also the subdirectories where there is movie file and subtitle file.


This script will generate following output.


The batch command TYPE is used for displaying the content of a file to an output console.


This program will display all the contents of notes.txt to the console.


The batch command VER displays the version of Windows or MS-DOS.




The batch command VOL displays the current volume label of Windows.




The batch command XCOPY is similar to COPY command but COPY command copies single file whereas XCOPY command copies entire directories including subdirectories.


This script will copy test.txt from D drive to E drive.

So, these are the batch file commands along with examples.

We hope you find these batch file commands easy to learn. We will discuss more advanced concepts in next tutorials.