The fso| example provides a real example in the following sample code to demonstrate many of the features available in the FileSystemObject object pattern. This code shows you how to use all of the features of the object pattern together, and how to use them effectively in your own code.
Note that because the code is extremely generic, you may need some other code and minor changes to make the code run on your machine. These changes are necessary because different methods are used for input and output to users between Active Server Pages and Windows scripting Host.
To run the code on Active Server Pages, take the following steps:
Create a standard Web page with a suffix named. asp.
Copy the following sample code to the file between <BODY>...</BODY> tags.
Encapsulate all the code in the <%...%> tag.
Move the Option Explicit statement from the current position to the top of the HTML page, even before the <HTML> start tag.
Place the <%...%> tag around the Option Explicit statement to ensure it runs on the server side.
Add the following code to the end of the sample code:
Sub Print (x)
Response.Write "<pre><font face=" "Song" "Size=" "1" ">"
Response.Write X
Response.Write "</FONT></PRE>"
End Sub
Main
The preceding code adds a printing process that will run on the server side but display the results on the client. To run the code on the Windows scripting Host, add the following code to the end of the sample code:
Sub Print (x)
WScript.Echo X
End Sub
Main
Here is the sample code:
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' FileSystemObject sample Code
'
' Copyright 1998
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' For code quality:
'
' 1 The following code has a number of string operations, using the "&" operator to concatenate short strings together. Because
' String concatenation is time-consuming, so this is a inefficient method of writing code. At any rate, it is
' A very well maintained write code method, and this method is used here because the program executes
' A lot of disk operations, and disk operations are much slower than the memory operations required to connect strings.
' Remember that this is the demo code, not the product code.
'
' 2 uses ' Option Explicit ' because accessing a declared variable is more than accessing a variable that is not declared
' A little faster. It also prevents errors in the code, for example, by misspelling the Drivetypecdrom
' Became a drivetypecdorm.
' We think you use InterDev as a development tool here.
’
' 3 in order for the code to be more readable, there is no error handling in the code. Although precautions have been taken to ensure that the code
' In normal circumstances there is no error, but the file system is unpredictable. In the product code, use the
' On Error Resume Next and ERR objects to catch errors that may occur.
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' Some of the easily obtained global variables
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim TabStop
Dim NewLine
Const testdrive = "C"
Const Testfilepath = "C:\Test"
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' The constant returned by Drive.drivetype
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Const drivetyperemovable = 1
Const drivetypefixed = 2
Const drivetypenetwork = 3
Const Drivetypecdrom = 4
Const Drivetyperamdisk = 5
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' The constant returned by file.attributes
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Const fileattrnormal = 0
Const fileattrreadonly = 1
Const Fileattrhidden = 2
Const Fileattrsystem = 4
Const Fileattrvolume = 8
Const fileattrdirectory = 16
Const fileattrarchive = 32
Const Fileattralias = 64
Const fileattrcompressed = 128
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' The constant used to open the file
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Const openfileforreading = 1
Const openfileforwriting = 2
Const openfileforappending = 8
OK, so here we are, let's take a look at a concrete example.