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
23
votes
2 answers

How to get the fully qualified path for a file in VBScript?

I am using the Shell.Application object, which allows me to script creation of a zip file. But in order for this to work, I need to full path of the zip file. File.zip doesn't work. I need c:\the\full\path\file.zip, even if the script is running…
Cheeso
  • 189,189
  • 101
  • 473
  • 713
23
votes
4 answers

Lists in VBScript

I'm trying to create a simple list in a VBscript, but I'm unable to find something similar. Basically, I'm working on Active directory, and I need to get all the groups a user is a member of for all the users within a domain. Now, every user might…
NullPointer
  • 2,084
  • 7
  • 24
  • 38
22
votes
6 answers

Count how many specific characters in string

I have a search box. My admin user might search for "@MG @EB dorchester". In ASP, I need to count how many times the symbol "@" appears in the string. How is the possible?
TheCarver
  • 19,391
  • 25
  • 99
  • 149
22
votes
7 answers

How do I get the Date & Time (VBS)

How do I get the current date and time using VBS (for Windows. I'm not looking for VBScript for ASP/ASPX or webpages).
Cocoa Dev
  • 9,361
  • 31
  • 109
  • 177
22
votes
1 answer

Access network share from within VBScript eg FileSystemObject

Is there a good way to access network shares from within a VBS script, with alternative credentials (not the credentials with which the VBS script is running)? The intention is to perform two tasks: programmatically navigate a remote share file…
Tao
  • 13,457
  • 7
  • 65
  • 76
22
votes
6 answers

Hide command prompt window when using Exec()

I'm trying to execute this simple test script, but a command shell window is appearing after I execute the script.: Set objShell = WScript.CreateObject("WScript.Shell") strCommand = "cmd /C tasklist" Set objExecObject =…
Black
  • 18,150
  • 39
  • 158
  • 271
22
votes
2 answers

How do I read a file line by line in VB Script?

I have the following to read a file line by line: wscript.echo "BEGIN" filePath = WScript.Arguments(0) filePath = "C:\Temp\vblist.txt" Set ObjFso = CreateObject("Scripting.FileSystemObject") Set ObjFile = ObjFso.OpenTextFile(filePath) StrData =…
EGr
  • 2,072
  • 10
  • 41
  • 61
22
votes
15 answers

Use clipboard from VBScript

I am looking for a method to place some text onto the clipboard with VBScript. The VBScript in question will be deployed as part of our login script. I would like to avoid using anything that isn't available on a clean Windows XP system. Edit: In…
Zoredache
  • 37,543
  • 7
  • 45
  • 61
21
votes
3 answers

If I set a variable using `CreateObject()`, do I need to clean it up by setting it to `Nothing` after use?

If I set a variable using CreateObject(), do I need to clean it up by setting it to Nothing after use? Dim foo Set foo = CreateObject("SomeAssembly") foo Bar Set foo = Nothing I just found this post by Eric Lippert: The script engine will…
Nick Strupat
  • 4,928
  • 4
  • 44
  • 56
21
votes
2 answers

VBA : save a file with UTF-8 without BOM

it's probably sthg simple, here is what I tried : Set objStream = CreateObject("ADODB.Stream") Set objStreamNoBOM = CreateObject("ADODB.Stream") With objStream .Open .Charset = "UTF-8" .WriteText "aaaaaa" …
Julien
  • 3,743
  • 9
  • 38
  • 67
21
votes
5 answers

How to run VBScript from command line without Cscript/Wscript

I am a beginner in VBScript. I googled it & got to know that we can run VBScript from command line by executing below command: For Example my vbscript name is Converter.vbs & it's present in folder D:\VBS. I can run it through following…
Solution Seeker
  • 595
  • 4
  • 9
  • 16
21
votes
4 answers

About using Double quotes in Vbscript

I have a very basic doubt in vb scripting: Msgbox "This is myName" ' This works fine Msgbox "This is "myName"" ' This gives an error Msgbox "This is ""myName""" 'This works fine My question is if I need to save (in a variable) or display…
user1925406
  • 713
  • 3
  • 15
  • 33
21
votes
9 answers

VBScript : checking if the user input is an integer

Within a VBScript, I need to make sure the user inputs a integer. Here is what I have now : WScript.Echo "Enter an integer number : " Number = WScript.StdIn.ReadLine If IsNumeric(Number) Then ' Here, it still could be an integer or a floating…
Jérôme
  • 26,567
  • 29
  • 98
  • 120
21
votes
5 answers

Copy a file from one folder to another using vbscripting

Can anyone please tell me how to copy a file from one folder to another using vbscripting I had tried this below one from the information provide in the internet. dim filesys set filesys=CreateObject("Scripting.FileSystemObject") If…
maddy
20
votes
3 answers

Using DLLs in VBScript

I've compiled C# code into a DLL, but have little experience with them. My C# code contains a class HelloWorld with a static method Print(). I'd like to use this DLL in VBScript to call the method Print(). I know this is base, but I'm using this as…
steventnorris
  • 5,656
  • 23
  • 93
  • 174