Share via


VBS Functions

The lack of a decent GUI for VBS development has always been a problem for most people.  The advantages of using VBS are great, but writing the scripts can be labourious, especially if you don’t have a lot of experience and know all the commands/functions etc.  Nowadays, there are plenty of 3rd-party development environments available now, in fact, I use the excellent VBSEdit as it has code completion and comes with a whole load of code snippets covering all sorts of areas.  The code completion feature is super helpful, especially when trying to remember the syntax of a particular function.

For reference purposes, I have included below a list of all the VBS functions as I am always looking up this information, so I thought I’d post it here so that it is more accessible to everyone (including me).

Abs

Returns the absolute value of a number. Wscript.Echo(Abs(-5)). returns: 5

Asc

Returns the ASCII character code for the first character of a string. Wscript.Echo(ASC ("ABC")). returns: 65

Chr

Returns the corresponding character of passed ASCII code number. Wscript.Echo(Chr(65)). returns: A

Cos

Returns cosine value of a number. Wscript.Echo(Cos(1)). returns: 0.54030230586814

Date

Returns the current system date in month, day, year format. Wscript.Echo(date()). returns: 8/2/02 ->with current date

DateAdd

Adds the specified date or time value to specified date. Wscript.Echo(DateAdd("d","10","6-7-02")). returns: 6/17/02 -> Adds 10 days to specified date. d=day, m=month, y=year, h=hour, n=minute

Day

Returns the day of the month. Wscript.Echo(Day(date)). returns: 9 ->with current day number.

Fix

Returns the integer part of a number. Wscript.Echo(fix(5.567)). returns: 5

FormatCurrency

Returns an expression formatted as a currency value. Wscript.Echo(FormatCurrency(10.888)). returns: $10.89

FormatDateTime

Returns a formatted date or time. Wscript.Echo(FormatDateTime(date)). returns: 8/2/02-> with current date.

FormatNumber

Returns an expression formatted as a number value. Wscript.Echo(FormatNumber(1000.1111,2)). returns: 1,000.11

FormatPercent

Returns an expression formatted as a percent Wscript.Echo(FormatPercent(.22)). returns: 22.00%

Hex

Returns the hexadecimal value of a number. Wscript.Echo(Hex(10)). returns: A

Hour, Minute, Second

Returns the number representing hour, minute, second of the day, hour, or minute. Wscript.Echo(Hour()). returns: 11 -> Assuming it's the hour is 11.

Int

Returns the integer part of a number. Wscript.Echo(Int(5.567)). returns: 5

IsNumeric

Returns Boolean value [true or false] indicates whether is number or not. Wscript.Echo(IsNumeric(10.888)). returns: True

LCase

Converts all the characters to lowercase. Wscript.Echo(LCase("CASE")). returns: case

Left

Returns specified number of characters starting from left. Wscript.Echo(left("language",3)). returns: lan

LTrim

Removes all the leading spaces from the string. Wscript.Echo(LTrim("far ")). returns: far, with no spaces

Month

Returns the month of the year. Wscript.Echo(Month(date)). returns: 9 ->with current month number.

MonthName

Returns the name of the specified month. Wscript.Echo(MonthName(9)). returns: September

MsgBox

Returns dialog box with specified expression or arguments. msgbox"Hello World" returns: Hello World -> On dialog box.

Now()

Returns current system date and time. Wscript.Echo(now()). returns: 8/2/02 10:46:33 AM ->with current date & time

Right

Returns specified number of characters staring from right. Wscript.Echo(Right("language")). returns: age

Rnd

Returns random number. Wscript.Echo(Rnd). returns: 0.7055475 ->Needs some code to randomize.

Round

Returns rounded number. Wscript.Echo(Round(10.888)). returns: 11

RTrim

Removes all the trailing spaces from the string. Wscript.Echo(RTrim(" far")). returns: far, with no spaces

Sgn

Returns 1, 0, or -1 for positive, zero or negative number Wscript.Echo(Sgn(-22)). returns: -1

Sin

Returns sine value of a number. Wscript.Echo(sin(1)). returns: 0.841470984807897

Sqr

Returns square root of a number. Wscript.Echo(sqr(25)). returns: 5

String

Takes a number and character code argument and returns the character, repeated a number of times: Wscript.Echo(String(3,80)). returns: BBB

Tan

Returns tangent value of a number. Wscript.Echo(tan(1)). returns: 1.5574077246549

Time

Returns the current system time. Wscript.Echo(Time()). returns: 10:11:36 AM ->with current time.

Timer

Returns the number of seconds since 12:00 AM. Wscript.Echo(Timer). returns: 40055.84 -> at 11:07 AM

TimeSerial

Returns the time for a specific hour, minute, and second converted from military clock. Wscript.Echo(TimeSerial(17,30,05)). returns: 5:30:05 PM.

TimeValue(time)

Returns the current system time. Wscript.Echo(Time(time))returns: 11:18:00 AM -> with current time.

Trim

Returns the string argument, with leading and trailing spaced removed. Wscript.Echo(Trim(" mid ")). returns: mid, with no spaces.

UCase

Converts all the characters to upper case. Wscript.Echo(UCase("Case")). returns: CASE

Val

Returns number value of a string. Stops as soon it hits string char. Wscript.Echo(Val("22 Maple st.")). returns: 22

WeekDay

Returns number representing the day of the week. Wscript.Echo(WeekDay(date)). returns: 6 -> Assuming today is Friday

WeekdayName

Returns the name of the specified week day. Wscript.Echo(WeekdayName(6)). returns: Friday

 

These functions are built-in to VBS so all you need to do is pass the correct values!  I can’t remember where I sourced all of this information from, so if you want to be credited, just let me know!

Comments

  • Anonymous
    April 14, 2009
    Thanks, i am always having to look this up as well.  you are the best!

  • Anonymous
    November 25, 2009
    Thanks, nice blog and useful tips & tools, you're still #1