Preparing for Java Interview?

My books Grokking the Java Interview and Grokking the Spring Boot Interview can help

Download a FREE Sample PDF

Wednesday, August 11, 2021

How to Create and Run a batch file in Windows - .bat File Example

batch files and shell scripts are developers best friend, they help to improve productivity a lot. They not only help you to automate boring, time consuming task but also saves lot of time doing and doing them again. Batch file also reduce chances of error, once tested and encourage re-usability by sharing them across team or people with similar needs. Though not every programmer, developer or computer user has mindset to use batch file, something it's too difficult to write, some just not able to fight with initial resistance and many of them are not even aware of batch file.

As a Java programmer, you really don't need to learn these stuff, as almost everybody who use windows operating system for more than year, knows about it. Anyway knowing is not enough, the main thing is adapting and taking full advantage of them, developing mentality towards scripting, automation and writing batch files.

Remember, shell scripting is one of the important skill to get a programming job and more important to support application running on UNIX operating sytem in Investment banking domain. Writing batch scripts are not full blown scripting, but it's lighter form of it, It works best to train yourself and develop that scripting mentality to improve productivity.

In this article, we will learn how to write a batch file in windows 8 operating system and revisit some important things about batch file.

Sample Batch file in Windows to clean directories and remove files:

I often need to clean persistent files, logs file and other data files before running some of my testing script. If you are working in large, multi-module application and your testing involves starting and stopping multiple module than it would be pain to do it manually every time.

If you are running your application in Linux and run your test consistently, you can write a whole shell script to automate everything. Since for small tests, I prefer windows, I found myself doing same task of cleaning files, which motivates me to write following batch file, good for demonstration.

Batch file to delete directories and Files

REM batch file clean directories and remove log files

echo "clearing module1 "
rmdir /S /Q D:\projects\module1\data
del D:\projects\module1\logs\module1.log

echo "clearing module2"
rmdir /S /Q D:\projects\module2\data
del D:\projects\module2\logs\module2.log

echo "clearing module3"
rmdir /S /Q D:\projects\module3\data
del D:\projects\module3\logs\module3.log

echo "clearing module4"
rmdir /S /Q D:\projects\module4\data
del D:\projects\module4\logs\module4.log

This is one of the simplest batch file, which is just removing some directories and log files. You can even use variables here removing some hardcoding e.g. project location can be set as variable. You can use set command create an environment variable and set its value.

Two things to pay attention is REM keyword and ECHO OFF. REM keyword is used to write comments inside batch file. Comments are lines, which are not executed by windows shell. ECHO OFF is to disable echo, which means you will not see actual commands on command prompt, you will only see output of commands. Though you can also print useful information by using echo command.

This is one of the must use while writing batch file, to see clean output. For beginners, DEL command is used to delete files in widows and rmdir is to remove directories, rmdir /S /Q are two options to remove quietly, without asking and /S to remove all files and sub-directories.

How to Run Batch File in Windows 

Running batch file is very simple, you can either double click on the batch file itself, or open a command prompt by typing cmd on the run window and invoke them by providing an absolute path. If you are on the same directory, then just type the name of the batch file in the command prompt as shown below.

"clearing module1 "
Could Not Find D:\projects\module1\logs\module1.log
"clearing module2"
Could Not Find D:\projects\module2\logs\module2.log
"clearing module3"
Could Not Find D:\projects\module3\logs\module3.log
"clearing module4"
Could Not Find D:\projects\module4\logs\module4.log

The output says could not file log files, because it was already removed from the earlier run. By the way, if you are a Java programmer, and constantly work on Eclipse, then you can invoke batch files from Eclipse itself. Just follow the steps given in that post.

How to Run Batch File in Windows 8

Things to remember

1) You can create batch files by using any text editor e.g. notepad or WordPad. I personally prefer notepad++ because of syntax highlighting and advanced features on find and replace.

2) Make sure your batch file has extension .bat not .txt. When you write something in a text editor, windows by default provide them .txt extension. Double check the extension of your batch file. In windows 8, a batch file has a different icon the text file, so it's easy to recognize, as shown below.

- icon for batch file
- icon for text file

3) Batch file is nothing but a set of commands. Instead of writing commands one by one in the command prompt, you write them in a file and ask the command prompt or shell to execute them. By putting multiple commands together, you can create small programs for your day-to-day task like cleaning data before running tests, taking archive or backup of log files, etc. For Java Programmers and developers, if you think you are doing the same task repeatedly, consider making a batch file to automate that task.

4) You can not only use simple batch commands e.g. MKDIR, RMDIR, DEL, or ECHO in a batch file, but also you can access environment variables e.g. JAVA_HOME, PATH or CLASSPATH by including them between % sign e.g. %PATH% will be replaced by value of  PATH environment variable, when you run batch file.

5) For Eclipse User, you can even run any batch file directly from Eclipse, without opening a command prompt or clicking on batch file.

That's all on how to create  a batch file in windows guys. I know, many of you already familiar with batch files and may also be using in your day to day work, but there is no excuse for those programmers, who are familiar but not using it. They tend to lose too much time, doing trivial activities manually.


Anonymous said...

You can use either "REM" or "::" for comments in batch files. But there is a catch - second type of commenting is working only if you put the symbol in front of your comment! Works a little bit different but is compact.

Anonymous said...

What is similarity and difference between shell script and a batch script?

Gordon said...

Eehh... .bat files? YUCK :(

Best option:
Install cygwin and use bash/linux scripting. Works like a charm on windows, too.

Second option:
ms powerShell

.bat files is sooooo 1980'ies :)
A horrible relict from the early days of MS-DOS, that msGod still havent had the guts to exterminate.

Anonymous said...

get rid of habbit of writing .bat file, learn python it works in all operating system, doesn't matter whether its windows or Linux. No more shell script or .bat script, Python rocks.

Post a Comment