Web Analytics


You might have seen it being used in some of the tips here on mintywhite. But what are BAT- and VBS-files, and what is the difference between them ?

The short version is that both kinds are small script files that can be used to execute functions or to do repetitive tasks. BAT is short for BATCH-file and is a very simple script-language from back then, when DOS was the ultimate PC OS. Some years later Microsoft decided (after much pun from the Linux, and Unix communities) to upgrade the functionality, thus creating VBS.

VBS is an abbreviation of Visual Basic Script, which is a light-version of the Visual Basic Language. Where the Batch-File mostly executes DOS commands the VBS is more powerful and can be used to create small applications, and process information. Where Batch-Files only run in DOS mode (Command Prompt), VBS is also used when creating standalone applications and in website programming. Both are a part of Windows, but might be more and more substituted by the new Powershell, which is available in Windows 7.

Both

You might wonder; If VBS is a more powerful alternative than BATCH, why keep the old Batch-language ? The answer is simple: Simplicity. Sometimes you don’ t need all the fancy functionality to do a task. And it requires much less effort and knowledge creating a Batch-File than a VBS-File.

Both Script languages can be created using Notepad. Save the file using either the .BAT or .VBS extension and Windows will know what to do.

What can I do with Batch ?

For the most parts, you execute DOS commands like Copy, Move, Rename etc. But you can also create simple multiple choice menus which need user input.

You don’t need to use (or learn) many codes or commands to get started. There are but a few, and the most important ones are ECHO, REM and PAUSE.

  • The Batch-File is designed to give you feedback for each action executed. You can turn this off, by using the ECHO Command. ECHO OFF, turns feedback off, and ECHO ON, turns it back on.  You can also use ECHO to display a message to the user, like this: “ECHO Press any Button to continue”
  • REM is short for REMARK, and is just a “programmers notes” inside the file.
  • PAUSE is used to temporarily halt execution of the Batch-File, awaiting input from the user.

Batch Example

I will write another article on this, but for now, open Notepad and type the following:

@ECHO OFF

dir "C:\Program Files" > C:\list_of_program_files.txt

Save the document as FolderList.BAT, and run it (Double-Click the icon). This Batch-File will create a text-file listing up every file and folder in your Programs-Folder.

Another Example

@ECHO OFFcd\MD TestFolderCD\Windows\TempCopy *.tmp c:\TestFolderDel *.tmp /qcd\TestFolder

Dir /p

Pause

Save the document as MoveTemp.bat, and execute it by double-clicking the icon. This Batch-File will move every TEMP-File from the windows temp-Folder into a new folder, then deleting them from the Windows Temp folder. It might be a crude example, but I think it gives you an idea of what you can do.

How do I use VBS ?

In short, VBS can do everything that Batch-Files can do – and more. As an object-oriented script language it requires a bit more know-how to use, but it’s not too difficult to overcome. If you remember the article I wrote on how to “make your computer greet you” then you have already seen VBS in action.

Common Expression

There are hundreds of expressions and functions that you can use, and I cannot cover everything in this article. The following is a crude and very short presentation of the most used functions or commands.

  • Logical Expression (IF…THEN…ELSE) which we use to do calculations or run functions depending on different criteria. If you want to rename files that has certain words or letters in the filename, you would use the IF statement.
  • Case Statement – is used to control what happens when different criteria are met. Similar to the above, but gives you a cleaner script.
  • Conditional Expressions, or loops (WHILE, WITH) are used to repeat a task until criteria are met. If you want to rename every file in a folder, you do a loop to repeat the action until there are no more files present.

If you read my articles on Macros in Excel (Part1, Part2) you have already some knowledge on VBS as it is the same script-language used in the MS Office Suite.

VBS Example

Dim speaks, speech
speaks="Oh No, Not YOU again! Please Log Off now!"
Set speech=CreateObject("sapi.spvoice")with speech
Set .voice = .getvoices.item(0)
.Volume = 100
.Rate = 0
end withspeech.Speak speaks

This is the script used in the “make your computer greet you” article

VBS Example 2

This example will unzip a certain file to a specific folder. This script can be set to collect information from the user before its run, but this example doesn’t do that.

pathToZipFile="C:\myzip.zip"
extractTo="C:\myfolder\" 'destination folder must exist!Set SA = CreateObject("Shell.Application")
Set filesInzip=SA.NameSpace(pathToZipFile).items
SA.NameSpace(extractTo).CopyHere(filesInzip)

One last thought

These are example of what you can do. To find more useful scripts to your own projects take a look here: http://gallery.technet.microsoft.com/ScriptCenter/en-us/.

About Thomas

Computer geek from the age of 7, which amounts to 30 years of computer experience. From the early days (when every computer company had their own OS) of DOS, Windows 1.0 through Seven...

Free PC tips by email

Search Windows Guides




Comments

3 thoughts on “The Difference Between BAT- and VBS-files”

  1. Bat Exterminator says:

    First, no one today should promote the use of batch files.  Is it easy?  Certainly.  Is it useful in today’s Windows networks?  Not unless you’re running Windows 9x clients, in which case you have bigger problems.  You also missed the most important distinction regarding the differences between command files (bat, cmd) and VBS, although you implied it in a roundabout way: command files use a shell scripting “host” which allows you to script only the limited set of objects built into the shell, while VBS (and other scripting languages) leverages WSH which allows you to use COM–a much more powerful object model–and which is NOT dependent on the scripting language itself.

    1. Rich says:

      I couldn’t disagree more. I use batch scripts all the time and I don’t know an IT shop that doesn’t. VBS scripts aren’t always the best option.

  2. .BatMan says:

    I use batch scripting every single day, and I support the northeast energy grid. I don’t know VBS, but so far I VASTLY prefer batch scripting over Powershell, as Powershell is clunky, slow, and in a red-tape-regulated environment like mine, batch runs on every server (and workstation), it runs FAST, is extremely easy to modify on the fly, and Powershell is a huge pain unless you’re familiar with it. And even if you are, you have to be running the right version, it has to be installed to work, certain ports have to be open in the firewall that doesn’t affect .batch. I love it, but I want to learn more technologies so I can keep deciding which works best in each scenario. It might be old, but it’s simple, and powerful (enough) to get a LOT of things done.

Comments are closed.


Computer tips in your inbox
Sign up for the Windows Guides newsletter to get PC tips and access to free Windows books (More details)

Subscribe now
Popular Guides

See which sites have been visited on your PC (even if private browsing mode is used)

Create a Windows 7 System Repair Disc

Best Free Anti-malware

Hibernate vs. Sleep vs. Shut-Down

i3, i5, and i7; Dual, Quad, Hexa Core Processors. How to they Differ?

Intel's Ivy Bridge Processor: new Features

Windows Guides on Facebook