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
0 answers

VBScript: How to run an exe as administrator with password coded in it

I am using the following VBScript and was not able to install the exe. Please help me. Set objShell = WScript.CreateObject("WScript.Shell") objShell.Run "runas /user:D08DVS3\administrator ""C:\Temp\SQLSP3.exe"" /wait /qs…
3
votes
3 answers

reading a csv file in vbscript

I am using the below function to read a text file which has below format. I need to lookup the id (column1) and return date (column2). I cannot understand what is wrong with my code. So my code runs perfectly till readline, but some how it is not…
imba22
  • 651
  • 1
  • 13
  • 25
3
votes
1 answer

Classic ASP JSON sub collection

I am using the Classic ASP JSON class from http://www.aspjson.com/ to convert a JSON feed from an external source for use within my site. For a single level collection I'm doing fine. JSON looks like this: { "data":[ { …
Claire_Monkey
  • 109
  • 1
  • 2
  • 13
3
votes
2 answers

How to log into user account?

After someone has logged off (Start button→Logoff) for the night, at a certain time in the morning I want to have the Task Scheduler automatically log into the Windows 7 user account (the opposite of Start button→Logoff) that is password protected.…
mountainclimber11
  • 1,339
  • 1
  • 28
  • 51
3
votes
2 answers

vbscript open folder in same explorer window

I am not so good in VBScript at all, but thanks to Google I was able to put together script which is able to open file path in explorer.exe I would like to open the specific path in same window not in the new one. Is VBScript able to do it? Here is…
easy4mer
  • 75
  • 1
  • 2
  • 8
3
votes
1 answer

How Do you Pass a String Variable to an Exe(Made in Python) in VBS

I am trying to use the following script: E=inputbox("What do you want me to search?") Set sh = CreateObject("WScript.Shell") sh.Run "search.exe "+E, 0, True and for some reason I cannot discern, it will just quit automatically without any error…
Azing
  • 43
  • 4
3
votes
1 answer

.GetAbsolutePathName(".") in vbscript does not get correct path when run by task scheduler

I am trying to run a vb script using task scheduler. I am using the .GetAbsolutePathName(".") to get the full path of the script. When I run the script manually, it was able to get the correct path. But when run as scheduled task, it outputs…
May Ann
  • 51
  • 8
3
votes
2 answers

How can I abort an InstallShield Setup depending on a vbscript custom action result?

I created a vbscript custom action which checks for some registry keys and alters them if neccessary. In case a key could not be written or something like that, the setup should be aborted. In order to achieve this, I set a property which I tried to…
Marcus
  • 1,105
  • 2
  • 21
  • 35
3
votes
1 answer

Could someone expliane that VBscript script file i found in memory flash?

The file seems to be a virus. The real files with the script file in the memory flash is hidden and linked with .lnk files that open them and run the script at the same time. the antivirus alert about VBS/LNK.JENXCUS.Gen Trojan. what does the code…
Ahmed
  • 186
  • 1
  • 2
  • 10
3
votes
1 answer

How to set process priority when starting an executable from a batch file in Windows without launching terminal

I have a .bat file [1] starting from a .vbs script [2] which only launches without launching a terminal if I don't include 'Start /low' in the .bat file. The 'Start /low' part of the .bat file launches the command with the right (low) priority set,…
cdvonstinkpot
  • 33
  • 1
  • 3
3
votes
1 answer

How to execute vbs file in java, which directory contains spaces

Im currently coding a program, but i need to make it execute a vbs file. TempDir.vbs. However, the directory to this file contains spaces. Unfortunally, all other topics dont work when the directory contains spaces. In my case: C:\\Users\\"the…
Br4m3000
  • 53
  • 1
  • 2
  • 6
3
votes
1 answer

Refresh all icons in desktop and taskbar

I need to refresh the desktop and taskbar icons programmatically using a batch file or VBScript. I found this vbs that hits the usual refresh key, F5: Set WSHShell = CreateObject("WScript.Shell") WshShell.SendKeys "{F5}" but that obviously doens't…
Magsood
  • 31
  • 2
3
votes
1 answer

MS access mdb file has a "repair" status first time it is opened from Access 2007

I have created an .mdb file programatically from another .mdb file using VBScript. It creates a table in the second .mdb file and populates data. Everything works fine and data is populated correctly. This is done using JET 4.0 driver. The issue is…
egghead
  • 31
  • 1
3
votes
4 answers

Redirect response output with VBScript in ASP classic

In a plain .asp file, any content outside of <% %> tags is sent directly to the output buffer. Additionally, an expression in <%= %> tags is evaluated and sent to the output buffer. I want to redirect it so that, in some context that I establish,…
Thom Smith
  • 13,916
  • 6
  • 45
  • 91
3
votes
1 answer

How to select which member is exported to COM when using explicit interface implementation?

If my class implements IEnumerable, I can use VBScript's For Each loop: [ComVisible(true)] [ProgId("Stackoverflow.MyIssues")] [Guid("7D392CB1-9080-49D0-B9CE-05B214B2C448")] public class MyIssue : IEnumerable { readonly List issues = new…
Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137