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

How to Close the CMD prompt window after successfull execution of VBScript

I am invoking a VBScript from another to run as administrator. following is the Invoker VBScript code Set objShell = CreateObject("Shell.Application") objShell.ShellExecute "cscript", "C:\Temp\XYZ.vbs", "", "runas", 0 Wscript.Quit 1 Following is…
3
votes
1 answer

Why set attribute fails?

Why this fails? set fldr = CreateObject("shell.application").BrowseForFolder(0, "Example", 0, 0).Self.Path but this works: fldr = CreateObject("shell.application").BrowseForFolder(0, "Example", 0, 0).Self.Path
Rahul
  • 10,830
  • 4
  • 53
  • 88
3
votes
1 answer

Script to replace the letters with diacritics Á, É, Í, ... by A, E, I,

Can you tell me what I need change in this script to get this output? "The output is Acailandia" Function EliminarAcentos(texto) Dim i, s1, s2 s1 = "ÁÀÉÈÍÏÓÒÚÜáàèéíïóòúüñç" s2 = "AAEEIIOOUUaaeeiioouunc" If Len(texto) <> 0 Then For i = 1 To…
Sanford
  • 21
  • 4
3
votes
1 answer

How to provide Information symbol in vbscript?

I want provide symbol of Information in vbscript. Here is my code: Set objShell = CreateObject( "WScript.Shell" ) lol=msgbox ("Update successful",10,"Information") I want to provide Information(Exclaim) symbol before the message "Update…
Nanji Mange
  • 2,155
  • 4
  • 29
  • 63
3
votes
3 answers

How to run several shortcuts (.lnk files) with a script in Windows?

I’m trying to create a script to run all the tools I use working in a particular project (dor instance, gulp, phpstorm, php server, etc.) I’d like to run shortcuts but not executable files. I can see two possible ways to achieve it in…
zhekaus
  • 3,126
  • 6
  • 23
  • 46
3
votes
1 answer

How can I create ASP class with "To" property (like CDONTS.NewMail)?

The CDONTS.NewMail class let's me send email like this: Set MailObj = Server.CreateObject("CDONTS.NewMail") MailObj.To = someone@example.com ' set other properties of MailObj MailObj.Send I can create my own class in ASP, but it won't let me define…
3
votes
1 answer

Trigger a VBScript when a file is added to a folder

I wish to automate some tasks. I need to move files from one folder to another. I have written the script below which is working fine. But my requirement is to trigger this VB script automatically, in the background, when a file is created or…
vicki
  • 157
  • 2
  • 23
3
votes
2 answers

Should I be concerned about a dual-defined VBS Function?

I'm took over an old classic ASP project and as part of migrating the database from Access to SQL I need to handle a few datetime cases at various points within the app. The thought is to include a little library file with my own set of functions…
jleach
  • 7,410
  • 3
  • 33
  • 60
3
votes
1 answer

search Whole word only in VBscript

I am trying to implement search whole word only in VBScript, I tried appeding characters like space, /, ],) etc. as these characters means end of word. I need to do as many search as the number of characters I want to include using or operator. Is…
Kailash Singh
  • 405
  • 2
  • 7
  • 12
3
votes
4 answers

How to prevent 'DROP BOBBY TABLES' when user enters a password with special characters?

In our ancient Classic ASP environment, we utilize OWASP to get the password from the request object and encrypt non-alphanumeric characters. This is a first line of defense to preventing sql injection. We use other methods for full sql injection…
crackedcornjimmy
  • 1,972
  • 5
  • 26
  • 42
3
votes
4 answers

Unused function in Excel addin causes crash only on second run and when run via VBScript

Sorry for the long title. I have several .xlsm files which share a lot of code, so I moved the repeated parts to an addin .xlam file. I have been using a .vbs script to open all the files one after another and run a macro in each. Problem The…
user847423
3
votes
0 answers

Export file to another directory

My application is generating a csv file that I want to move to another directory when user clicks export button. My VBScript code is: Sub ExportTocsv() Dim oDLG Set fso = CreateObject("Scripting.FileSystemObject") Set…
Kailash Singh
  • 405
  • 2
  • 7
  • 12
3
votes
0 answers

How to retrieve an Sql decimal(18,3) column in Vbscript

I did a fair amount of research on this issue but can't get my head around a solution. Here's the problem: Using HP UFT I read a column value from an ADO recordset with Vbscript. This column is a decimal(18,3) and vbScript recognises the decimal…
3
votes
2 answers

Getting list of files in current directory

I'm trying to get a script to read the contents of the directory where the script file is located, then identify a couple of specific files based on partial names and zip them. But I can't get the object.Files property to work. Can someone tell me…
Ffffrank
  • 33
  • 1
  • 4
3
votes
1 answer

vbscript to read a file in chunck

I need to read a text file in chunks so that I don't need show too much information on screen at once. I need that the user can see the next chunk from result set as he click next button (each chunk are separated by next keyword). I tried it showing…
user2816085
  • 655
  • 4
  • 19