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.
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 ?
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.
I will write another article on this, but for now, open Notepad and type the following:
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.
@ECHO OFFcd\MD TestFolderCD\Windows\TempCopy *.tmp c:\TestFolderDel *.tmp /qcd\TestFolder
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.
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.
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.
extractTo="C:\myfolder\" 'destination folder must exist!Set SA = CreateObject("Shell.Application")
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/.