Home | | Internet & World Wide Web HOW TO PROGRAM | | Internet Programming | | Web Programming | File System Objects - Active Server Pages (ASP)

Chapter: Internet & World Wide Web HOW TO PROGRAM - Active Server Pages (ASP)

| Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail |

File System Objects - Active Server Pages (ASP)

File System Objects (FSOs) provide the programmer with the ability to manipulate files, directories and drives.

File System Objects


File System Objects (FSOs) provide the programmer with the ability to manipulate files, directories and drives. FSOs also allow the programmer to read and write text and are an essential element for Active Server Pages that persist data. We overview FSO features and then provide a “live-code™” example that uses FSOs.


FSOs are objects in the Microsoft Scripting Runtime Library. These FSO types include: FileSystemObject, File, Folder, Drive and TextStream. Each type is summarized in Fig. 25.6.



The programmer can use FileSystemObjects to create directories, move files, determine whether a Drive exists, etc. Figure 25.7 summarizes some common methods of FileSystemObject.


The File object allows the programmer to gather information about files, manipulate files and open files. Figure 25.8 lists some common File properties and methods.

Property Path contains the File’s path in long name format (the operating system does not abbreviate the name when it exceeds the 8.3 format). Property ShortName con-tains, if applicable, the file name in short name format (a file name exceeding the 8.3 format is abbreviated). For example, “ABCDEFGHIJ.doc” is a file name in long name format. That same file name in short name format might be abbreviated as “ABCDEF~1.doc.”

The Folder object allows the programmer to manipulate and gather information about directories. Figure 25.9 lists some common Folder properties and methods.

Property IsRootFolder indicates whether the folder is the root folder for the Drive (i.e., the folder that contains everything on the drive). If the folder is not the root folder, method ParentFolder may be called to get the folder’s parent folder (i.e., the folder in which the selected folder is contained). Method Size returns the total number of bytes the folder contains. The size includes subfolders (i.e., folders inside the selected folder) and files.


The Drive object allows the programmer to gather information about drives. Figure 25.10 lists some common Drive properties. Property DriveLetter contains the Drive’s letter. Property SerialNumber contains the Drive’s serial number. Property FreeSpace contains the number of bytes available.


The TextStream object allows the programmer to manipulate text files. Figure 25.11 list TextStream properties and methods.


Figure 25.12 is an Active Server Page for a guest book, which allows the visitors to enter their name, e-mail and comments. File system objects are used to write the visitor information to a file on the server.

<% @LANGUAGE = VBScript %>


    <% ' Fig. 25.12 : guestbook.asp

    ' Demonstrating File System Objects

    Option Explicit


Fig. 25.12  Guest book Active Server Page


          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"



          <html xmlns = "http://www.w3.org/1999/xhtml">




          <title>GuestBook Example</title>


          <style type = "text/css">

          hr      { size: 1; color: blue }

          table  { text-align: center }

          td      { font-size: 12pt }

          p       { font-size: 14pt; color: blue }

          .font { font-family: arial, sans-serif }





          Dim fileObject, textFile, guestBook, mailtoUrl


          ' get physical path for this ASP page and

          ' concatenate guestbook.txt to it

          guestbook = Request.ServerVariables( "APPL_PHYSICAL_PATH" ) _

          & "\guestbook.txt"

          ' instantiate a FileSystemObject

          Set fileObject = Server.CreateObject( _

          "Scripting.FileSystemObject" )


          ' Check if this request is after the user has posted the form

          If Request( "hiddenInput" ) = "true" Then


          ' Print a thank you

          Call Response.Write( "Thanks for your entry, " & _

          Request( "username" ) & "!" )


          <hr />


          ' build the mailtoUrl

          mailtoUrl = Date() & " <a href = " & Chr( 34 ) _

          & "mailto:" & Request( "email" ) & Chr( 34 ) _

          & ">" & Request( "username" ) & "</a>:  "



          ' open the guestbook, 8 is for appending

          ' create the guestbook if it does not exist

          Set textFile = _

          fileObject.OpenTextFile( guestbook, 8, True )


          ' write data to guestbook.txt

          Call textFile.WriteLine( "<hr />" & mailtoUrl & _

          Request( "comment" ) )

          Call textFile.Close()

          End If



          <p>Please leave a message in our guestbook.</p>



          <!-- write form to the client -->

          <form action = "guestbook.asp" method = "post">



          <td>Your Name:  </td>

          <td><input class = "font"

          type = "text" size = "60"

          name = "username" /></td>




          <td>Your email address:</td>

          <td><input class = "font"

          type = "text" size = "60"

          name = "email"

          value = "user@isp.com" />





          <td>Tell the world:  </td>

          <td><textarea name = "comment" rows = "3"

          cols = "50">

          Replace this text with the information

          you would like to post.</textarea></td>




          <input type = "submit" value = "submit" />

          <input type = "reset" value = "clear" />

          <input type = "hidden" name = "hiddenInput"

          value = "true" />




          ' check if the file exists

          If fileObject.FileExists( guestBook ) = True Then



          ' open the guestbook, "1" is for reading

          Set textFile = fileObject.OpenTextFile( guestbook, 1 )


          ' read the entries from the file and write them to

          ' the client.

          Call Response.Write( "Guestbook Entries:<br />" & _

          textFile.ReadAll() )

          Call textFile.Close()


          End If







Fig. 25.12  Guest book Active Server Page


The guest book page displayed in the browser consists of a form (to be filled in by the user) and a list of guest book entries (initially, there are no entries in this list). The form (lines 67–98) contains two text fields and a text area for inputting the name, e-mail and user comment.


Line 67 indicates that a post request occurs upon form submission. The action for the form requests the same ASP page in which the form is contained—guestbook.asp. A form’s action is not required to request a different document.

Upon submission, guestbook.asp is requested and passes parameter hidden-Input—which has the value "true." The name hiddenInput is programmer-defined—developers of course may choose any name they prefer. We use this technique to determine whether this ASP page is being requested by a form submitted from guest-book.asp.


Lines 30–31 pass Request method ServerVariables the server key APPL_PHYSICAL_PATH to retrieve the physical path of the virtual directory where this ASP document resides. We concatenate this with the name of the file, guestbook.txt, and assign the result to variable guestbook. Fig. 25.13 lists some server variable keys.


Lines 34–35 create an FSO instance (i.e., an object) and assign it to reference file-Object. When assigning an object to a reference in VBScript, keyword Set is required.


We want only lines 41–60 to execute when the page is loaded with a post request. Line 38 uses the Request object to get hiddenInput’s value and test it against the string "true." When this page is requested by a client for the first time, hiddenInput has the value "" (an empty string), and lines 41–60 are not executed. Variable hiddenInput is only assigned value "true" during the post operation (line 67).


Lines 41–42 print Thanks for your entry, followed by the user’s name. The Request object gets the value posted in the username field of the submitted form.


The user's submitted name and e-mail are combined with XHTML tags and assigned to string mailtoUrl (lines 47–49). This string, when displayed in the browser, shows the submitted name as a mailto link. Clicking this link opens an e-mail message editor with the person’s name in the To: field. Line 47 calls VBScript function Date to assign the current server date to the beginning of mailtoUrl. The Request object is used to retrieve the values from the email field (line 48) and the username field (line 49). The value 34 is passed to the VBScript function Chr to get a double quote (") character. We call this func-tion because the interpreter would treat a double quote as the end of the mailtoUrl string. The XHTML tags are stored in mailtoUrl.

Lines 54–55 call method OpenTextFile to get a TextStream object for accessing the text file guestbook.txt. The constant value 8 indicates append mode (writing to the end of the file), and True indicates that the file will be created if it does not already exist. Read and write modes are specified with constant values 1 and 2, respectively.


Lines 58–59 write text to guestbook.txt using the TextStream method WriteLine. After writing the text to the file, TextStream method Close is called in line 60 to close the file.


Every time a client requests this Active Server Page, lines 100–115 execute. This VBScript code displays a list of all the users who have made guest book entries. Line 102 uses FSO method FileExists to check if the guestbook.txt file exists. If this func-tion returns True, lines 106–112 execute. In line 106, guestbook.txt is opened for reading. Lines 110–111 read the entries from the file and write XHTML to the client. The entire contents of guestbook.txt are read by calling TextStream method ReadAll in line 111. This text is written to the client using Response.Write. This text contains XHTML markup which is rendered in the client browser. The TextStream method Close is called in line 112 to close the file. Fig. 25.14 shows the contents of guest-book.txt after two users have submitted comments.


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail

Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.