Questions tagged [vbscript]

VBScript (Visual Basic Scripting Edition) is an interpreted scripting language developed by Microsoft that is modeled on Visual Basic. VBScript is not the same thing as VBA or VB.NET. They are three different things, so use the correct tags.

VBScript (Visual Basic Scripting Edition) is an interpreted scripting language developed by Microsoft that is modeled on Visual Basic.

VBScript is commonly used for automating administrative and other tasks in the Windows operating systems (via Windows Script Host --) and for server-side scripting in web applications. It can also used for client-side scripting in (not other browsers), but is generally used for this purpose only in intranet web applications, where the browser can be limited to IE. VBScript is also the language used to create custom Outlook forms ().

Although VBScript has much common syntax with VBA, do not tag questions as unless you are specifically asking about both. It is also completely different than VB.NET.

Some differences between VBScript and VBA:

  • Does not support enumerated constants; replace with the numeric value:

    'VBScript
    Outlook.CreateItem(0)
    
    'VBA allows this:
    Outlook.CreateItem(olMailItem)
    
  • All variables are of type Variant, and are declared without a type specification:

    'VBScript
    Dim olApp  
    Dim msg
    
    'VBA allows this:
    Dim olApp As Outlook.Application  
    Dim msg As Outlook.MailItem
    
  • Method calls don't support named arguments.

    'VBScript
    wb.SaveAs "output.csv", 6, , , , False
    
    'VBA allows this:
    wb.SaveAs FileName:="output.csv", FileFormat:=xlCSV, CreateBackup:=False
    

Executing VBScripts with WSH -- WScript/CScript

VBScript can be executed locally either in GUI mode, in which output is displayed as a window:

wscript.exe C:\Script\Location\script.vbs

or in console mode, in which input is read from and written to a console window.

cscript.exe C:\Script\Location\script.vbs

Running wscript.exe or cscript.exe without specifying the path will run the script in the machine's architecture -- 32-bit on 32-bit machines, and 64-bit on 64-bit machines. On 64-bit machines, it is possible to have the script run in the 32-bit emulation layer:

C:\windows\SysWOW64\wscript.exe  C:\Script\Location\script.vbs
C:\windows\SysWOW64\cscript.exe  C:\Script\Location\script.vbs

Note: Scheduled VBScript tasks must be run with cscript.exe because computer/domain policies limit activation of GUI applications when no user is logged on.

Debugging scripts

Visual Studio (Community Edition, or the Integrated Shell of pre-2013 versions) can be used to step through scripts and locate errors.

  • //X will trigger the debugger if there is a runtime error, or a code breakpoint
  • //D will trigger the debugger at the beginning of the script

No variable expansion
Like other languages in the Visual Basic family, VBScript does not expand variables inside string literals, so in code like

var1 = "fox"
var2 = "The quick brown var1 jumps over the lazy dog."

the value of var2 will remain The quick brown var1 jumps over the lazy dog. instead of becoming The quick brown fox jumps over the lazy dog. To get the value of a variable in a string, the variable must be concatenated with the rest of the string:

var1 = "fox"
var2 = "The quick brown " & var1 & " jumps over the lazy dog."

Frequently Asked Questions

Resources

Related Tags

18718 questions
36
votes
8 answers

Error - "There is no script engine for file extension .vbs" when using "Git Bash Here" in Windows 7

I have the latest release version of git for windows installed. When I use the context menu option Git Bash Here, I get the following error. There is no script engine for file extension .vbs Any ideas how to fix it?
mkasberg
  • 16,022
  • 3
  • 42
  • 46
35
votes
4 answers

What is the difference between VBScript's + and & operator?

On every site that talks about VBScript, the '&' operator is listed as the string concatenation operator. However, in some code that I have recently inherited, I see the '+' operator being used and I am not seeing any errors as a result of this. Is…
Jeremy Cron
  • 2,404
  • 3
  • 25
  • 30
34
votes
3 answers

Can I throw an error in vbscript?

I'm used to programing in C#, which obviously has some pretty robust error handling. Now I'm working on a short project in VBScript. I've read about the error handling with VBscript using "On Error _______" and the Err global variable. However, is…
Eric Anastas
  • 21,675
  • 38
  • 142
  • 236
34
votes
2 answers

Can I try to ping a website through a specific adapter?

I hope this isn't too basic a question. The title kind of asks it all. :-)
richb01
  • 1,097
  • 2
  • 13
  • 25
34
votes
13 answers

How do I sort arrays using vbscript?

I'm scanning through a file looking for lines that match a certain regex pattern, and then I want to print out the lines that match but in alphabetical order. I'm sure this is trivial but vbscript isn't my background my array is defined as Dim…
Oskar
  • 2,234
  • 5
  • 28
  • 35
34
votes
6 answers

ByRef and ByVal in VBScript

I'm facing something weird in VBScript. When writing a procedure where I want the parameter to be passed by reference, the way of calling this procedure changes the way the parameter is passed ! Here is an example : Sub IncrementByRef(ByRef Value) …
Jérôme
  • 26,567
  • 29
  • 98
  • 120
34
votes
7 answers

Exit a while loop in VBS/VBA

Is there a method of exiting/breaking a while in VBS/VBA? Following code won't work as intended: num = 0 while (num < 10) if (status = "Fail") then exit while end if num = num+1 wend
maddy
33
votes
1 answer

Converting string to integer

PrinterLabel = Printer + PrinterNumber If Floors = 1 And (PrinterLabel) > 127 Then Wscript.Echo "Invalid Printer11 Selection " Wscript.Quit End If If Floors = 2 And PrinterLabel > 220 Then Wscript.Echo "Invalid Printerss…
Cocoa Dev
  • 9,361
  • 31
  • 109
  • 177
33
votes
6 answers

Base64 Encode String in VBScript

I have a web service load driver that's a Windows Script File (WSF), that includes some VBScript and JavaScript files. My web service requires that the incoming message is base64 encoded. I currently have a VBScript function that does this, but it's…
Patrick Cuff
  • 28,540
  • 12
  • 67
  • 94
33
votes
2 answers

Read utf-8 text file in vbscript

I have a text file saved as UTF-8 and when I try to read the file it gives me weird characters and not the correct characters (it contains Chinese characters). How can I make it give me the correct Chinese characters? Option Explicit Dim objFSO,…
Trish Pham
  • 439
  • 2
  • 6
  • 13
32
votes
4 answers

How to generate a GUID in VBScript?

I want to generate GUID strings in VBScript. I know that there's no built-in function in VBScript for generating one. I don't want to use random-generated GUIDs. Maybe there is an ActiveX object that can be created using CreateObject() that is sure…
vividos
  • 6,468
  • 9
  • 43
  • 53
32
votes
7 answers

How to resolve "The requested URL was rejected. Please consult with your administrator." error?

I have a ASP application. On click of a particular link, some VB scripts are executed and an ASP page is to be shown, but instead I get a screen that says: Information Not Available. The requested URL was rejected. Please consult with your…
suhas
  • 531
  • 1
  • 6
  • 15
32
votes
4 answers

Is it better to use NOT or <> when comparing values?

Is it better to use NOT or to use <> when comparing values in VBScript? is this: If NOT value1 = value2 Then or this: If value1 <> value2 Then better? EDIT: Here is my counterargument. When looking to logically negate a Boolean value you would…
Tester101
  • 8,042
  • 13
  • 55
  • 78
32
votes
2 answers

benchmarks: does python have a faster way of walking a network folder?

I need to walk through a folder with approximately ten thousand files. My old vbscript is very slow in handling this. Since I've started using Ruby and Python since then, I made a benchmark between the three scripting languages to see which would…
peter
  • 41,770
  • 5
  • 64
  • 108
32
votes
6 answers

Running vbscript from batch file

I just need to write a simple batch file just to run a vbscript. Both the vbscript and the batch file are in the same folder and is in the SysWOW64 directory as the vbscript can only be execute in that directory. Currently my batch file is as…
user918197
  • 1,129
  • 6
  • 17
  • 29