Archives de catégorie : Scripting

vbscript – BuildPath

METHOD:  FileSystemObject.BuildPath


Implemented in version 2.0

[newfullpath = ]object.BuildPath(path, name)

This method is used to append a name onto an existing path. The format of the new path is ‘existingpath\name’.

Code:
<%
dim filesys, newfullpath
set filesys=CreateObject(« Scripting.FileSystemObject »)
newfullpath = filesys.BuildPath(c:\inetpub\wwwroot, « images »)
%>

Output:
« c:\inetpub\wwwroot\images »

vbscript – AtEndOfStream

Property:  TextStream.AtEndOfStream


Implemented in version 2.0

object.AtEndOfStream

The AtEndOfStream property returns a Boolean value. If the file pointer is positioned at the end of the file, the value is True. Otherwise, the value is False. The TextStreamobject must be open for reading or an error will occur when using this method.

The following code uses the AtEndOfStream property to get the last character in text file.

Code:
<%
dim filesys, text, readfile, contents
set filesys = CreateObject(« Scripting.FileSystemObject »)
Set text = filesys.CreateTextFile(« c:\somefile2.txt »)
text.Write « Find the last character in the text file »
text.close
set readfile = filesys.OpenTextFile(« c:\somefile2.txt », 1, false)
do while readfile.AtEndOfStream <> true
contents = readfile.Read(1)
loop
readfile.close
Response.Write « The last character in the text file is ‘ » & contents & « ‘. »
%>

Output:
« The last character in the text file is ‘e’. »

vbscript – AtEndOfLine

Property:  TextStream.AtEndOfLine


Implemented in version 2.0

object.AtEndOfLine

The AtEndOfLine property returns a Boolean value. If the file pointer is positioned immediately before the file’s end-of-line marker, the value is True. Otherwise, the value is False. The TextStream object must be open for reading or an error will occur when retreiving this property this method.

The following code uses the AtEndOfLine property to get the last character in the first line of text in a file.

Code:
<%
dim filesys, text, readfile, contents
set filesys = CreateObject(« Scripting.FileSystemObject »)
Set text = filesys.CreateTextFile(« c:\somefile2.txt »)
text.Write « Find the last character in the text file »
text.close
set readfile = filesys.OpenTextFile(« c:\somefile2.txt », 1, false)
do while readfile.AtEndOfLine <> true
contents = readfile.Read(1)
loop
readfile.close
Response.Write « The last character in the text file is ‘ » & contents & « ‘. »
%>

Output:
« The last character in the text file is ‘e’. »

vbscript – Asc

FUNCTION:  Asc()


Implemented in version 1.0

Asc(String)

The Asc function returns the ANSI character code value for the first character in a string.

In these two examples, note that the ANSI value is returned only for the « a ».

Code:
<% =Asc(« abcde fghij klmno pqrst uvwxyz ») %>

Output:
97

Code:
<% =Asc(« a ») %>

Output:
97

vbscript – Array

FUNCTION:  Array( )


Implemented in version 2.0

Array(List)

The Array function is used to create a static one-dimension array. You cannot declare a dynamic array using the Array function.

Note that the first element in an array is always labeled zero, for example,
myarray(0).

The List argument is a listing of values that will become the elements of the array.

Code:
<% myarray = array(« A », « B », « C », « D ») %>
<% =myarray(0) %>
<% =myarray(1) %>
<% =myarray(2) %>
<% =myarray(3) %>

Output:
A
B
C
D

Code:
<% myarray = array(111, 222, 333, 444, 555) %>
<% =myarray(0) %>
<% =myarray(1) %>
<% =myarray(2) %>
<% =myarray(3) %>
<% =myarray(4) %>

Output:
111
222
333
444
555

A dynamic array is declared using the Dim and the ReDim statements. First, you use the Dim statement to declare the dynamic array by using empty parenthesis. Then, at a later point in your program, you use the ReDim statement to declare the number of elements. In fact, you can re-declare a dynamic array as many times as you desire.

Code:
<%
Dim SomeArray()

ReDim SomeArray(22)

ReDim SomeArray(500)
%>

Arrays can have up to sixty dimensions. If you wish to create a multiple-dimension array, you need to declare it using the Dim statement. For example, the following code creates a three dimensional array. The first dimension has 23 elements, the second dimension has 15 elements and the third dimension has 201 elements. Therefore, there are a total of 23x15x201 = 69345 elements in this array.

The number of dimensions that you can create is limited by the available memory. If you exceed the available memory while declaring an array, you will get an error message.

Code:
<% Dim ThreeDimensionArray(22, 14, 200) %>

vbscript – And

OPERATOR:  And


Implemented in version 1.0

And

The And operator is used to perform a logical conjunction on two expressions, where the expressions are Null, or are of Boolean subtype and have a value of True orFalse.

The And operator can also be used a « bitwise operator » to make a bit-by-bit comparison of two integers. If both bits in the comparison are 1, then a 1 is returned. Otherwise, a 0 is returned.

When using the And to compare Boolean expressions, the order of the expressions is not important.

Code:
<% =True And True %>
<% =True And False %>
<% =False And True %>
<% =False And False %>
<% =True And Null %>
<% =Null And True %>
<% =False And Null %>
<% =Null And False %>
<% =Null And Null %>

Output:
True
False
False
False
(Null output)
(Null output)
False
False
(Null output)

Code:
<% anyexpression = True %>
<% someexpression = False %>
<% =anyexpression And someexpression %>

Output:
False

In this example, the And performs a bitwise comparison on the 1 (in binary 001) and the 2 (in binary 010), and returns a 0 (in binary 000).

Code:
<%
Expression1 = 1
Expression2 = 2
Result = Expression1 And Expression2
Response.Write « Result =  » & Result
%>

Output:
Result = 0