In my ASP tutorial you will learn about ASP, and how to execute scripts on your server.

ASP Tutorial: 2008

ASP Content Rotator (ASP 3.0)



Examples


The
Content Rotator Component


This component displays a different HTML content string each time a user visits
or refreshes the page.




ASP Content Rotator Component


The ASP Content Rotator component creates a ContentRotator
object that displays a different HTML content string each time a user enters or
refreshes a page. A text file, called the Content Schedule File, includes the
information about the content strings.


The content strings can contain HTML tags so you can display
any type of content that HTML can represent: text, images, colors, or
hyperlinks.


Syntax








<%

Set cr=Server.CreateObject( "MSWC.ContentRotator" )

%>

The following example displays a different content each time a
user views the Web page. Create a text file named "textads.txt" in
your default Web Site folder, in a subfolder called text.


"textads.txt":








%% #1

This is a great day!!



%% #2

<h1>Smile</h1>



%% #3

<img src="smiley.gif">



%% #4

Here's a <a
href="http://www.w3schools.com">link.</a>

Notice the #number at the beginning of each content string.
This number is an optional parameter that indicates the relative weight of the
HTML content string. In this example, the Content Rotator will display the first
content string one-tenth of the time, the second string two-tenths of the time,
the third string three-tenths of the time, and the fourth string four-tenths of
the time.


Then, create an ASP file, and insert the following code:








<html>

<body>



<%

set cr=server.createobject("MSWC.ContentRotator")

response.write(cr.ChooseContent("text/textads.txt"))

%>



</body>

</html>

The ASP Content Rotator Component's methods are described
below:


Methods




















Method Description Example
ChooseContent Gets and displays a content string <%

dim cr

Set cr=Server.CreateObject("MSWC.ContentRotator")

response.write(cr.ChooseContent("text/textads.txt"))

%>

Output:



Smiley


GetAllContent Retrieves and displays all of the content strings in
the text file
<%

dim cr

Set cr=Server.CreateObject("MSWC.ContentRotator")

response.write(cr.GetAllContent("text/textads.txt"))

%>

Output:




This is a great day!!




Smile




Smiley




Here's a link.









ASP Content Linking Component



Examples


The
Content Linking Component


This example builds a table of contents.


The
Content Linking Component 2


The example uses the Content Linking Component to navigate between the pages in
a text file.




ASP Content Linking Component


The ASP Content Linking component is used to create a quick
and easy navigation system!


The Content Linking component returns a Nextlink object that
is used to hold a list of Web pages to be navigated.


Syntax








<%

Set nl=Server.CreateObject( "MSWC.NextLink" )

%>

First we create a text file - "links.txt". This file
contains the pages to be navigated. The pages must be listed in the same order
you want them to be displayed, and it also must contain a description for each
file name (use the tab key to separate file name from description). Note:
If you want to add a page to the list or change the order of the pages in the
list; all you have to do is to modify the text file! The navigation will
automatically be correct!


"links.txt":








asp_intro.asp ASP Intro

asp_syntax.asp ASP Syntax

asp_variables.asp ASP Variables

asp_procedures.asp ASP Procedures

On each of the pages listed above, put one line of code:
<!-- #include file="nlcode.inc"-->. This line will include the
code below on every page listed in "links.txt" and the navigation will
work.


"nlcode.inc":









<%
'Use the Content Linking Component
'to navigate between the pages listed
'in links.txt

dim nl
Set nl=Server.CreateObject("MSWC.NextLink")
if (nl.GetListIndex("links.txt")>1) then
Response.Write("<a href='" & nl.GetPreviousURL("links.txt"))
Response.Write("'>Previous Page</a>")
end if
Response.Write("<a href='" & nl.GetNextURL("links.txt"))
Response.Write("'>Next Page</a>")
%>


The ASP Content Linking Component's methods are described
below:


Methods


















































Method Description Example
GetListCount Returns the number of items listed in the Content
Linking List file
<%

dim nl,c

Set nl=Server.CreateObject("MSWC.NextLink")

c=nl.GetListCount("links.txt")

Response.Write("There are ")

Response.Write(c)

Response.Write(" items in the list")

%>

Output:


There are 4 items in the list


GetListIndex Returns the index number of the current item in the
Content Linking List file. The index number of the first item is 1. 0 is
returned if the current page is not in the Content Linking List file
<%

dim nl,c

Set nl=Server.CreateObject("MSWC.NextLink")

c=nl.GetListIndex("links.txt")

Response.Write("Item number ")

Response.Write(c)

%>

Output:


Item number 3


GetNextDescription Returns the text description of the next item listed in
the Content Linking List file. If the current page is not found in the
list file it returns the text description of the last page on the list
<%

dim nl,c

Set nl=Server.CreateObject("MSWC.NextLink")

c=nl.GetNextDescription("links.txt")

Response.Write("Next ")

Response.Write("description is: ")

Response.Write(c)

%>

Next description is: ASP Variables


GetNextURL Returns the URL of the next item listed in the Content
Linking List file. If the current page is not found in the list file it
returns the URL of the last page on the list
<%

dim nl,c

Set nl=Server.CreateObject("MSWC.NextLink")

c=nl.GetNextURL("links.txt")

Response.Write("Next ")

Response.Write("URL is: ")

Response.Write(c)

%>

Next URL is: asp_variables.asp


GetNthDescription Returns the description of the Nth page listed in the
Content Linking List file
<%

dim nl,c

Set nl=Server.CreateObject("MSWC.NextLink")

c=nl.GetNthDescription("links.txt",3)

Response.Write("Third ")

Response.Write("description is: ")

Response.Write(c)

%>

Third description is: ASP Variables


GetNthURL Returns the URL of the Nth page listed in the Content
Linking List file
<%

dim nl,c

Set nl=Server.CreateObject("MSWC.NextLink")

c=nl.GetNthURL("links.txt",3)

Response.Write("Third ")

Response.Write("URL is: ")

Response.Write(c)

%>

Third URL is: asp_variables.asp


GetPreviousDescription Returns the text description of the previous item
listed in the Content Linking List file. If the current page is not
found in the list file it returns the text description of the first page
on the list
<%

dim nl,c

Set nl=Server.CreateObject("MSWC.NextLink")

c=nl.GetPreviousDescription("links.txt")

Response.Write("Previous ")

Response.Write("description is: ")

Response.Write(c)

%>

Previous description is: ASP Variables


GetPreviousURL Returns the URL of the previous item listed in the
Content Linking List file. If the current page is not found in the list
file it returns the URL of the first page on the list
<%

dim nl,c

Set nl=Server.CreateObject("MSWC.NextLink")

c=nl.GetPreviousURL("links.txt")

Response.Write("Previous ")

Response.Write("URL is: ")

Response.Write(c)

%>

Previous URL is: asp_variables.asp







ASP Browser Capabilities Component



Examples


The
Browser Capabilities Component


This example shows how to determine the type, capabilities and version number of
each browser visiting your site.




ASP Browser Capabilities Component


The ASP Browser Capabilities component creates a BrowserType
object that determines the type, capabilities and version number of each browser
that visits your site.


When a browser connects to a server, an HTTP User Agent Header
is also sent to the server. This header contains information about the browser
(like browser type and version number). The BrowserType object then compares the
information in the header with information in a file on the server called
"Browscap.ini".


If there is a match between the browser type and version
number sent in the header and the information in the "Browsercap.ini"
file, you can use the BrowserType object to list the properties of the matching
browser. If there is no match for the browser type and version number in the
Browscap.ini file, it will set every property to "UNKNOWN".


Syntax








<%

Set MyBrow=Server.CreateObject("MSWC.BrowserType") 

%>

The example below creates a BrowserType object in an ASP file,
and displays a table showing some of the capabilities of the current browser:









<html>
<body>

<%
Set MyBrow=Server.CreateObject("MSWC.BrowserType")
%>

<table border="1" width="100%">
<tr>
<th>Client OS</th>
<th><%=MyBrow.platform%></th>
</tr><tr>
<td >Web Browser</td>
<td ><%=MyBrow.browser%></td>
</tr><tr>
<td>Browser version</td>
<td><%=MyBrow.version%></td>
</tr><tr>
<td>Frame support?</td>
<td><%=MyBrow.frames%></td>
</tr><tr>
<td>Table support?</td>
<td><%=MyBrow.tables%></td>
</tr><tr>
<td>Sound support?</td>
<td><%=MyBrow.backgroundsounds%></td>
</tr><tr>
<td>Cookies support?</td>
<td><%=MyBrow.cookies%></td>
</tr><tr>
<td>VBScript support?</td>
<td><%=MyBrow.vbscript%></td>
</tr><tr>
<td>JavaScript support?</td>
<td><%=MyBrow.javascript%></td>
</tr>
</table>

</body>
</html>


Output:









































Client OS WinNT
Web Browser IE
Browser version 5.0
Frame support? True
Table support? True
Sound support? True
Cookies support? True
VBScript support? True
JavaScript support? True






The Browscap.ini File


The "Browsercap.ini" file is used to declare
properties and to set default values for browsers.


This section is not a tutorial on how to maintain
"Browsercap.ini" files, it only shows you the basics; so you get an
idea what a "Browsercap.ini" file is all about.


The "Browsercap.ini" file can contain the following:









[;comments]
[HTTPUserAgentHeader]
[parent=browserDefinition]
[property1=value1]
[propertyN=valueN]
[Default Browser Capability Settings]
[defaultProperty1=defaultValue1]
[defaultPropertyN=defaultValueN]








































Parameter Description
comments Optional. Any line that starts with a semicolon are
ignored by the BrowserType object
HTTPUserAgentHeader Optional. Specifies the HTTP User Agent header to
associate with the browser-property value statements specified in
propertyN. Wildcard characters are allowed
browserDefinition Optional. Specifies the HTTP User Agent header-string
of a browser to use as the parent browser. The current browser's
definition will inherit all of the property values declared in the
parent browser's definition
propertyN Optional. Specifies the browser properties. The
following table lists some possible properties:


  • ActiveXControls - Support ActiveX® controls?
  • Backgroundsounds - Support background sounds?
  • Cdf - Support Channel Definition Format for
    Webcasting?

  • Tables - Support tables?
  • Cookies - Support cookies?
  • Frames - Support frames?
  • Javaapplets - Support Java applets?
  • Javascript - Supports JScript?
  • Vbscript - Supports VBScript?
  • Browser - Specifies the name of the browser
  • Beta - Is the browser beta software?
  • Platform - Specifies the platform that the browser
    runs on

  • Version - Specifies the version number of the
    browser


valueN Optional. Specifies the value of propertyN. Can be a
string, an integer (prefix with #), or a Boolean value
defaultPropertyN Optional. Specifies the name of the browser property to
which to assign a default value if none of the defined
HTTPUserAgentHeader values match the HTTP User Agent header sent by the
browser
defaultValueN Optional. Specifies the value of defaultPropertyN. Can
be a string, an integer (prefix with #), or a Boolean value

A "Browsercap.ini" file might look something like
this:









;IE 5.0
[IE 5.0]
browser=IE
Version=5.0
majorver=#5
minorver=#0
frames=TRUE
tables=TRUE
cookies=TRUE
backgroundsounds=TRUE
vbscript=TRUE
javascript=TRUE
javaapplets=TRUE
ActiveXControls=TRUE
beta=False

;DEFAULT BROWSER
[*]
browser=Default
frames=FALSE
tables=TRUE
cookies=FALSE
backgroundsounds=FALSE
vbscript=FALSE
javascript=FALSE






ASP AdRotator Component



Examples


Simple
AdRotator Example


This example shows how to use the AdRotator component to display a different
advertisement image, each time a user visits or refreshes the page.


AdRotator
- The Images are Hyperlinks


This example shows how to use the AdRotator component to display a different
advertisement image, each time a user visits or refreshes the page. In addition,
the images are hyperlinks.




ASP AdRotator Component


The ASP AdRotator component creates an AdRotator object that
displays a different image each time a user enters or refreshes a page. A text
file includes information about the images.


Syntax








<%

set adrotator=server.createobject("MSWC.AdRotator")

adrotator.GetAdvertisement("textfile.txt")

%>






Example


Assume we have a file called "banners.asp". It looks
like this:








<html>

<body>

<%

set adrotator=Server.CreateObject("MSWC.AdRotator")

response.write(adrotator.GetAdvertisement("ads.txt"))

%>

</body>

</html>

The file "ads.txt" looks like this:








*

w3schools.gif

http://www.w3schools.com/

Visit W3Schools

80

microsoft.gif

http://www.microsoft.com/

Visit Microsoft

20

The lines below the asterisk in the file "ads.txt"
specifies the images to be displayed, the hyperlink addresses, the alternate
text (for the images), and the display rates in percent of the hits. We see that
the W3Schools image will be displayed for 80 % of the hits and the Microsoft
image will be displayed for 20 % of the hits in the text file above.


Note: To get the links to work when a user clicks on
them, we will have to modify the file "ads.txt" a bit:








REDIRECT banners.asp

*

w3schools.gif

http://www.w3schools.com/

Visit W3Schools

80

microsoft.gif

http://www.microsoft.com/

Visit Microsoft

20

The redirection page (banners.asp) will now receive a
querystring with a variable named URL containing the URL to redirect to.


Note: To specify the height, width, and border of the
image, you can insert the following lines under REDIRECT:








REDIRECT banners.asp

WIDTH 468

HEIGHT 60

BORDER 0

*

w3schools.gif

...

...

The last thing to do is to add some lines of code to the
"banners.asp" file:








<%

url=Request.QueryString("url")

If url<>"" then Response.Redirect(url)

%>

<html>

<body>

<%

set adrotator=Server.CreateObject("MSWC.AdRotator")

response.write(adrotator.GetAdvertisement("textfile.txt"))

%>

</body>

</html>



That's all!!




Properties

























Property Description Example
Border Specifies the size of the borders around the
advertisement
<%

set adrot=Server.CreateObject("MSWC.AdRotator")

adrot.Border="2"

Response.Write(adrot.GetAdvertisement("ads.txt"))

%>
Clickable Specifies whether the advertisement is a hyperlink <%

set adrot=Server.CreateObject("MSWC.AdRotator")

adrot.Clickable=false

Response.Write(adrot.GetAdvertisement("ads.txt"))

%>
TargetFrame Name of the frame to display the
advertisement
<%

set adrot=Server.CreateObject("MSWC.AdRotator")

adrot.TargetFrame="target='_blank'"

Response.Write(adrot.GetAdvertisement("ads.txt"))

%>

Methods















Method Description Example
GetAdvertisement Returns HTML that displays the advertisement in the
page
<%

set adrot=Server.CreateObject("MSWC.AdRotator")

Response.Write(adrot.GetAdvertisement("ads.txt"))

%>





Introduction to ADO



ADO can be used to access databases from your
web pages.




Accessing a Database from an ASP Page


The common way to access a database from inside an ASP page is
to:



  1. Create an ADO connection to a database
  2. Open the database connection
  3. Create an ADO recordset
  4. Open the recordset
  5. Extract the data you need from the recordset
  6. Close the recordset
  7. Close the connection




What is ADO?



  • ADO is a Microsoft technology
  • ADO stands for ActiveX Data Objects
  • ADO is a Microsoft Active-X component
  • ADO is automatically installed with Microsoft IIS
  • ADO is a programming interface to access data in a database




Where to go next?


If you want to study more ADO, read our ADO
tutorial
.



ASP Dictionary Object



The Dictionary object is used to store
information in name/value pairs (referred to as key and item)




Examples


Does
a specified key exist?


This example demonstrates how to first create a Dictionary object, and then use
the Exists method to check if a specified key exists.


Return
an array of all items


This example demonstrates how to use the Items method to return an array of all
the items.


Return
an array of all keys


This example demonstrates how to use the Keys method to return an array of all
the keys.


Return
the value of an item


This example demonstrates how to use the Item property to return the value of an
item.


Set
a key


This example demonstrates how to use the Key property to set a key in a
Dictionary object.


Return
the number of key/item pairs


This example demonstrates how to use the Count property to return the number of
key/item pairs.




The Dictionary Object


The Dictionary object is used to store information in
name/value pairs (referred to as key and item). The Dictionary object might seem
similar to Arrays, however, the Dictionary object is a more desirable solution
to manipulate related data.


Comparing Dictionaries and Arrays:



  • Keys are used to identify the items in a Dictionary object
  • You do not have to call ReDim to change the size of the
    Dictionary object

  • When deleting an item from a Dictionary, the remaining
    items will automatically shift up

  • Dictionaries cannot be multidimensional, Arrays can
  • Dictionaries have more built-in functions than Arrays
  • Dictionaries work better than arrays on accessing random
    elements frequently

  • Dictionaries work better than arrays on locating items by
    their content


The following example creates a Dictionary object, adds some
key/item pairs to it, and retrieves the item value for the key gr:









<%
Dim d
Set d=Server.CreateObject("Scripting.Dictionary")
d.Add "re","Red"
d.Add "gr","Green"
d.Add "bl","Blue"
d.Add "pi","Pink"
Response.Write("The value of key gr is: " & d.Item("gr"))
%>

Output:


The value of key gr is: Green 


The Dictionary object's properties and methods are described
below:


Properties

























Property Description
CompareMode Sets or returns the comparison mode for comparing keys
in a Dictionary object
Count Returns the number of key/item pairs in a Dictionary
object
Item Sets or returns the value of an item in a
Dictionary object
Key Sets a new key value for an existing key
value in a Dictionary object

Methods

































Method Description
Add Adds a new key/item pair to a Dictionary object
Exists Returns a Boolean value that indicates whether a
specified key exists in the Dictionary object
Items Returns an array of all the items in a
Dictionary object
Keys Returns an array of all the keys in a
Dictionary object
Remove Removes one specified key/item pair from
the Dictionary object
RemoveAll Removes all the key/item pairs in the
Dictionary object





ASP Folder Object




The Folder Object is used to return information about a specified
folder.





The Folder Object


The Folder object is used to return information about a specified folder.


To work with the properties and methods of the Folder object, you will have
to create an instance of the Folder object through the FileSystemObject object.
First; create a FileSystemObject object and then instantiate the Folder object
through the GetFolder method of the FileSystemObject object.


The following code uses the GetFolder method of the FileSystemObject object
to instantiate the Folder object and the DateCreated property to return the date
when the specified folder was created:





<%
Dim fs,fo
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set fo=fs.GetFolder("c:\test")
Response.Write("Folder created: " & fo.DateCreated)
set fo=nothing
set fs=nothing
%>

Output:

Folder created: 10/22/2001 10:01:19 AM

The Folder object's collections, properties, and methods are described below:


Collections












CollectionDescription
FilesReturns a collection of all the files in a specified
folder
SubFoldersReturns a collection of all subfolders in a specified
folder

Properties













































PropertyDescription
AttributesSets or returns the attributes of a specified folder
DateCreatedReturns the date and time when a specified folder was
created
href="prop_datelastaccessed_folder.asp">DateLastAccessedReturns the date and time when a specified folder was last
accessed
href="prop_datelastmodified_folder.asp">DateLastModifiedReturns the date and time when a specified folder was last
modified
DriveReturns the drive letter of the drive where the specified folder
resides
IsRootFolderReturns true if a folder is the root folder and false if
not
NameSets or returns the name of a specified folder
ParentFolderReturns the parent folder of a specified folder
PathReturns the path for a specified folder
ShortNameReturns the short name of a specified folder (the 8.3 naming
convention)
ShortPathReturns the short path of a specified folder (the 8.3 naming
convention)
SizeReturns the size of a specified folder
TypeReturns the type of a specified folder

Methods


















MethodDescription
CopyCopies a specified folder from one location to another
DeleteDeletes a specified folder
MoveMoves a specified folder from one location to another
CreateTextFileCreates a new text file in the specified folder and returns a
TextStream object to access the file




ASP File Object



The File object is used to return information
about a specified file.




Examples


When
was the file created?


This example demonstrates how to first create a FileSystemObject object, and
then use the DateCreated property of the File object to get the date and time a
specified file was created.


When
was the file last modified?


This example demonstrates how to use the DateLastModified property to get the
date and time a specified file was last modified.


When
was the file last accessed?


This example demonstrates how to use the DateLastAccessed property to get the
date and time a specified file was last accessed.


Return
the attributes of a specified file


This example demonstrates how to use the Attributes property to return the
attributes of a specified file.




The File Object


The File object is used to return information about a
specified file.


To work with the properties and methods of the File object,
you will have to create an instance of the File object through the
FileSystemObject object. First; create a FileSystemObject object and then
instantiate the File object through the GetFile method of the FileSystemObject
object or through the Files property of the Folder object.


The following code uses the GetFile method of the
FileSystemObject object to instantiate the File object and the DateCreated
property to return the date when the specified file was created:









<%
Dim fs,f
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set f=fs.GetFile("c:\test.txt")
Response.Write("File created: " & f.DateCreated)
set f=nothing
set fs=nothing
%>

Output:


File created: 9/19/2001 10:01:19 AM


The File object's properties and methods are described below:


Properties

























































Property Description
Attributes Sets or returns the attributes of a
specified file
DateCreated Returns the date and time when a specified
file was created
DateLastAccessed Returns the date and time when a specified
file was last accessed
DateLastModified Returns the date and time when a specified
file was last modified
Drive Returns the drive letter of the drive
where a specified file or folder resides
Name Sets or returns the name of a specified
file
ParentFolder Returns the folder object for the parent
of the specified file
Path Returns the path for a specified file
ShortName Returns the short name of a specified file
(the 8.3 naming convention)
ShortPath Returns the short path of a specified file
(the 8.3 naming convention)
Size Returns the size, in bytes, of a specified
file
Type Returns the type of a specified file

Methods

























Method Description
Copy Copies a specified file from one location
to another
Delete Deletes a specified file
Move Moves a specified file from one location
to another
OpenAsTextStream  Opens a specified file and returns a
TextStream object to access the file





ASP Drive Object



The Drive object is used to return information
about a local disk drive or a network share.




Examples


Get
the available space of a specified drive


This example demonstrates how to first create a FileSystemObject object, and
then use the AvailableSpace property to get the available space of a specified
drive.


Get
the free space of a specified drive


This example demonstrates how to use the FreeSpace property to get the free
space of a specified drive.


Get
the total size of a specified drive


This example demonstrates how to use the TotalSize property to get the total
size of a specified drive.


Get
the drive letter of a specified drive


This example demonstrates how to use the DriveLetter property to get the drive
letter of a specified drive.


Get
the drive type of a specified drive


This example demonstrates how to use the DriveType property to get the drive
type of a specified drive.


Get
the file system of a specified drive


This example demonstrates how to use the FileSystem property to get the file
system of a specified drive.


Is
the drive ready?


This example demonstrates how to use the IsReady property to check whether a
specified drive is ready.


Get
the path of a specified drive


This example demonstrates how to use the Path property to get the path of a
specified drive.


Get
the root folder of a specified drive


This example demonstrates how to use the RootFolder property to get the root
folder of a specified drive.


Get
the serialnumber of a specified drive


This example demonstrates how to use the Serialnumber property to get the
serialnumber of a specified drive.




The Drive Object


The Drive object is used to return information about a local
disk drive or a network share. The Drive object can return information about a
drive's type of file system, free space, serial number, volume name, and more.


Note: You cannot return information about a drive's
content with the Drive object. For this purpose you will have to use the Folder
object.


To work with the properties of the Drive object, you will have
to create an instance of the Drive object through the FileSystemObject object.
First; create a FileSystemObject object and then instantiate the Drive object
through the GetDrive method or the Drives property of the FileSystemObject
object.


The following example uses the GetDrive method of the
FileSystemObject object to instantiate the Drive object and the TotalSize
property to return the total size in bytes of the specified drive (c:):









<%
Dim fs,d
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set d=fs.GetDrive("c:")
Response.Write("Drive " & d & ":")
Response.Write("Total size in bytes: " & d.TotalSize)
set d=nothing
set fs=nothing
%>

Output:


Drive c: Total size in bytes: 4293563392



The Drive object's properties are described below:


Properties

























































Property Description
AvailableSpace Returns the amount of available space to a
user on a specified drive or network share
DriveLetter Returns one uppercase letter that
identifies the local drive or a network share
DriveType Returns the type of a specified drive
FileSystem Returns the file system in use for a
specified drive
FreeSpace Returns the amount of free space to a user
on a specified drive or network share
IsReady Returns true if the specified drive is
ready and false if not
Path Returns an uppercase letter followed by a
colon that indicates the path name for a specified drive
RootFolder Returns a Folder object that represents
the root folder of a specified drive
SerialNumber Returns the serial number of a specified
drive
ShareName Returns the network share name for a
specified drive
TotalSize Returns the total size of a specified
drive or network share
VolumeName Sets or returns the volume name of a
specified drive





ASP TextStream Object



The TextStream object is used to access the
contents of a text file.




Examples


Read
textfile


This example demonstrates how to use the OpenTextFile method of the
FileSystemObject to create a TextStream Object. The ReadAll method of the
TextStream Object reads from the opened text file.  


Read
only a part of  a textfile


This example demonstrates how to only read a part of a TextStream file.


Read
one line of a textfile


This example demonstrates how to read one line from a TextStream file.


Read
all lines from a textfile


This example demonstrates how to read all the lines from a TextStream file.


Skip
a part of a textfile


This example demonstrates how to skip a specified number of characters when
reading the TextStream file.


Skip
a line of a textfile


This example demonstrates how to skip a line when reading the TextStream file.


Return
line-number


This example demonstrates how to return the current line number in a TextStream
file.


Get
column number


This example demonstrates how to get the column number of the current character
in a file.




The TextStream Object


The TextStream object is used to access the contents of text
files.


The following code creates a text file (c:\test.txt) and then
writes some text to the file (the variable f is an instance of the TextStream
object):









<% 
dim fs, f 
set fs=Server.CreateObject("Scripting.FileSystemObject") 
set f=fs.CreateTextFile("c:\test.txt",true) 
f.WriteLine("Hello World!")
f.Close
set f=nothing
set fs=nothing
%>


To create an instance of the TextStream object you can use the
CreateTextFile or OpenTextFile methods of the FileSystemObject object, or you
can use the OpenAsTextStream method of the File object.

The TextStream object's properties and methods are described
below:


Properties

























Property Description
AtEndOfLine Returns true if the file pointer is
positioned immediately before the end-of-line marker in a TextStream
file, and false if not
AtEndOfStream Returns true if the file pointer is at the
end of a TextStream file, and false if not
Column Returns the column number of the current
character position in an input stream
Line Returns the current line number in a
TextStream file

Methods













































Method Description
Close Closes an open TextStream file
Read Reads a specified number of characters
from a TextStream file and returns the result
ReadAll Reads an entire TextStream file and
returns the result
ReadLine Reads one line from a TextStream file and
returns the result
Skip Skips a specified number of characters
when reading a TextStream file
SkipLine Skips the next line when reading a
TextStream file
Write Writes a specified text to a TextStream
file
WriteLine Writes a specified text and a new-line
character to a TextStream file
WriteBlankLines Writes a specified number of new-line
character to a TextStream file





ASP FileSystemObject Object



The FileSystemObject object is used to access
the file system on the server.




Examples


Does
a specified file exist?


This example demonstrates how to first create a FileSystemObject Object, and
then use the FileExists method to check if the file exists.


Does
a specified folder exist?


This example demonstrates how to use the FolderExists method to check if a
folder exists.


Does
a specified drive exist?


This example demonstrates how to use the DriveExists method to check if a drive
exists.


Get
the name of a specified drive


This example demonstrates how to use the GetDriveName method to get the name of
a specified drive.


Get
the name of the parent folder of a specified path


This example demonstrates how to use the GetParentFolderName method to get the
name of the parent folder of a specified path.


Get
the file extension


This example demonstrates how to use the GetExtensionName method to get the file
extension of the last component in a specified path.


Get
file name


This example demonstrates how to use the GetFileName method to get the file name
of the last component in a specified path.


Get
the base name of a file or folder


This example demonstrates how to use the GetBaseName method to return the base
name of the file or folder, in a specified path.




The FileSystemObject Object


The FileSystemObject object is used to access the file system
on the server. This object can manipulate files, folders, and directory paths.
It is also possible to retrieve file system information with this object.


The following code creates a text file (c:\test.txt) and then
writes some text to the file:








<%

dim fs,fname

set fs=Server.CreateObject("Scripting.FileSystemObject")

set fname=fs.CreateTextFile("c:\test.txt",true)

fname.WriteLine("Hello World!")

fname.Close

set fname=nothing

set fs=nothing

%>



The FileSystemObject object's properties and methods are described below:

Properties













Property Description
Drives Returns a collection of all Drive objects
on the computer

Methods









































































































Method Description
BuildPath Appends a name to an existing path
CopyFile Copies one or more files from one location
to another
CopyFolder Copies one or more folders from one
location to another
CreateFolder Creates a new folder
CreateTextFile Creates a text file and returns a
TextStream object that can be used to read from, or write to the file
DeleteFile Deletes one or more specified files
DeleteFolder Deletes one or more specified folders
DriveExists Checks if a specified drive exists
FileExists Checks if a specified file exists
FolderExists Checks if a specified folder exists
GetAbsolutePathName Returns the complete path from the root of
the drive for the specified path
GetBaseName Returns the base name of a specified file
or folder
GetDrive Returns a Drive object corresponding to
the drive in a specified path
GetDriveName Returns the drive name of a specified path
GetExtensionName Returns the file extension name for the
last component in a specified path
GetFile Returns a File object for a specified path
GetFileName Returns the file name or folder name for
the last component in a specified path
GetFolder Returns a Folder object for a specified
path
GetParentFolderName Returns the name of the parent folder of
the last component in a specified path
GetSpecialFolder Returns the path to some of Windows'
special folders
GetTempName Returns a randomly generated temporary
file or folder
MoveFile Moves one or more files from one location
to another
MoveFolder Moves one or more folders from one
location to another
OpenTextFile Opens a file and returns a TextStream
object that can be used to access the file





ASP ASPError Object



The ASPError object is used to display detailed
information of any error that occurs in scripts in an ASP page.




The ASPError Object


The ASPError object was implemented in ASP 3.0 and is
available in IIS5 and later.


The ASPError object is used to display detailed information of
any error that occurs in scripts in an ASP page. The ASPError object is created
when Server.GetLastError is called, so the error information can only be
accessed by using the Server.GetLastError method.


The ASPError object's properties are described below (all
properties are read-only):


Note: The properties below can only be accessed through
the Server.GetLastError() method. 


Properties













































Property Description
ASPCode Returns an error code generated by IIS
ASPDescription Returns a detailed description of the error (if the
error is ASP-related)
Category Returns the source of the error (was the error
generated by ASP? By a scripting language? By an object?)
Column Returns the column position within the file that
generated the error
Description Returns a short description of the error
File Returns the name of the ASP file that generated the
error
Line Returns the line number where the error was detected
Number Returns the standard COM error code for the error
Source

Returns the actual source code of the line where the
error occurred







ASP Server Object



The ASP Server object is used to access
properties and methods on the server.




Examples


When
was a file last modified?


Checks when this file was last modified.


Open
a text file for reading


This example opens the file "Textfile.txt" for reading.


Homemade
hit counter


This example reads a number from a file, adds 1 to the number, and writes the
number back to the file.




Server Object


The ASP Server object is used to access properties and methods
on the server. Its properties and methods are described below:


Properties













Property Description
ScriptTimeout Sets or returns the maximum number of
seconds a script can run before it is terminated

Methods





































Method Description
CreateObject Creates an instance of an object
Execute Executes an ASP file from inside another ASP file
GetLastError() Returns an ASPError object that describes the error
condition that occurred
HTMLEncode Applies HTML encoding to a specified string
MapPath Maps a specified path to a physical path
Transfer Sends (transfers) all the information created in one
ASP file to a second ASP file
URLEncode Applies URL encoding rules to a specified string





ASP Session Object



The Session object is used to store information
about, or change settings for a user session. Variables stored in the Session
object hold information about one single user, and are available to all pages in
one application.




Examples


Set
and return the LCID


This example demonstrates the "LCID" property. This property sets or
returns an integer that specifies a location or region. Contents like date,
time, and currency will be displayed according to that location or region.


Return
the SessionID


This example demonstrates the "SessionID" property. This property
returns a unique id for each user. The id is generated by the server.


A
session's timeout


This example demonstrates the "Timeout" property. This example sets
and returns the timeout (in minutes) for the session.




Session Object


When you are working with an application, you open it, do some
changes and then you close it. This is much like a Session. The computer knows
who you are. It knows when you start the application and when you end. But on
the internet there is one problem: the web server does not know who you are and
what you do because the HTTP address doesn't maintain state.


ASP solves this problem by creating a unique cookie for each
user. The cookie is sent to the client and it contains information that
identifies the user. This interface is called the Session object.


The Session object is used to store information about, or
change settings for a user session. Variables stored in the Session object hold
information about one single user, and are available to all pages in one
application. Common information stored in session variables are name, id, and
preferences. The server creates a new Session object for each new user, and
destroys the Session object when the session expires.


The Session object's collections, properties, methods, and
events are described below:


Collections

















Collection Description
Contents Contains all the items appended to the session through
a script command
StaticObjects Contains all the objects appended to the session with
the HTML <object> tag

Properties

























Property Description
CodePage Specifies the character set that will be
used when displaying dynamic content
LCID Sets or returns an integer that specifies
a location or region. Contents like date, time, and currency will be
displayed according to that location or region
SessionID Returns a unique id for each user. The
unique id is generated by the server
Timeout Sets or returns the timeout period (in
minutes) for the Session object in this application

Methods





















Method Description
Abandon Destroys a user session
Contents.Remove Deletes an item from the Contents
collection
Contents.RemoveAll() Deletes all items from the Contents
collection

Events

















Event Description
Session_OnEnd Occurs when a session ends
Session_OnStart Occurs when a session starts