Windows Batch Scripting for Beginners and how to use it.

What is a Batch file?

Most people will never really understanding what the extension .bat is in Windows or even DOS.  However it is something that is native to programmers around the world because it is the first thing most people who make programs or works with scripting start to use way before they do anything else.  It should be considered the first linguistic languages that is very powerful.  It is designed to be used in DOS but can also be used in Windows as a DOS command easily and effectively through Windows OS.   Some of these are what you should explore even more to learn how to program and create scripts even more.  However this blog post is just a introduction into doing it and how you can use this in your everyday IT Job.

According to Wikipedia:

A batch file is a script file in DOS, OS/2 and Microsoft Windows. It consists of a series of commands to be executed by the command-line interpreter, stored in a plain text file. A batch file may contain any command the interpreter accepts interactively and use constructs that enable conditional branching.

Understanding scripting is the first part in creating powerful scripts for everyday use.  However, I am one of the few to use the help command in DOS:

HELP [command]

This is a good tool for those who might want to explore the power of using a Batch to get your job done or to creating a batch file because you will need to know what you can use in the batch file.

Here are a few commands you might want to use in a batch file:

  • SET /P variable=[promptString]
  • Start of the batch file ECHO OFF and ECHO ON after the batch file is complete.
  • If and THEN (Wikipedia)
  • CALL filename.bat

Creating Batch files

I’m not going to go into much details about creating the batch in the way you might need but I will be talking about this in general about ways you can create a batch and which programs are good at helping you create a batch file.

First you need a program to create it.  You can always use Wordpad or Notepad but you could find it isnt’ a good as Notepad ++.   I have used it in the past for programming in many different programming languages such as Pascal, C++, COBOL, and SHELL Scripting.  I’ve talked about SHELL scripting in past for LINUX which you can use Notepad++ for it also to create Linux shell scripting.  I will say you can do a lot with the open-sourced program that is free to use.  So I always want to use this with anything I am doing because it shows you where you might have a problem in your scripting language and helps you identify how to fix it.  You’re best advice is to just try to think about what you want to accomplish with a batch file and start thinking logically about how you would do that.

You will need to make sure the .batextension is on the file name.  Make the filename something that you can easily run in DOS because you don’t want to make the batch file anything complicated because that might not work properly if you use a call command in another batch file.  So consider the naming convention of your batch file(s).

Trial and Error

After creating the batch file you will need to do some trial and error test to make sure it will work with what you need to use it for and also make sure there isn’t something unexpected.   This is what programmers do all the time by compiling  the program and running it on different systems to see what it will do in different Windows environments.

Finally, You will see that you can do so much more with a batch that you previously did and it can be a great asset in doing repetitive stuff at work or at home.   If you do this right it can even help speed up your life.   Here are a few Blog posts that I have talked about previous:

As you can see these will help you understanding scripting but also help you to figure out what you might want to do with a Batch and why you might need to create a batch file.  Even I know this is only a few blog post that will at least hopefully give you even more ideas on what will help you to create batch files.

Don’t forget to like and subscribe to my blog page for even more updates.   Do you have a favorite DOS command?  why not share it with others so they can learn even more.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.