Recommended: How to create a login verification page User login verification script, Chkpwd.asp The following is the referenced content: % '========= User login verification script====== 'If the Passed object is not defined yet, it is defined as false, indicating that there is no
In the sample code described in this section, real-life examples are provided to illustrate many of the features available in the FileSystemObject object pattern. This code shows all the features of how to use object patterns together, and how to use them effectively in your own code.
Note that since the code is extremely general, it may take some additional code and minor changes to make it work on your machine. These changes are necessary because different approaches are used to give users to input and output 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 the suffix named .asp.
Copy the following sample code into the file between the <BODY>...</BODY> tags.
Encapsulate all code into the <%...%> tag.
Move the Option Explicit statement from the current location to the top of the HTML page, even before <HTML> starts tagging.
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 example code:
Sub Print(x) Response.Write <PRE><FONT face=安SIZE=1> Response.Write x Response.Write </FONT></PRE> End Sub Main
The previous code adds a printing process that will run on the server side but displays the results on the client side. To run the code on Windows Scripting Host, add the following code to the end of the sample code:
Sub Print(x) WScript.Echo x End Sub Main
The following is the sample code:
| The following is the quoted content: -------------------------------------------------- ------------------------------ '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' ' FileSystemObject sample code ' 'Copyright 1998 Microsoft Corporation. All rights reserved. ' '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Option Explicit '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' |
'For code quality:
'
' 1) The following code has many string operations, using the "&" operator to concatenate short strings together. because
' String concatenation is time consuming, so this is an inefficient way to write code. Anyway, it is
' A very well maintained method of writing code, and this method is used here because the program executes
' A large number of disk operations, which are much slower than the memory operations required to connect strings.
' Remember this is demonstration code, not product code.
'
' 2) "Option Explicit" is used because accessing declared variables is more important than accessing undeclared variables
' A little faster. It can also prevent errors in the code, for example, mistakenly spelling DriveTypeCDROM
' Become DriveTypeCDORM.
'
' 3) To make the code more readable, there is no error handling in this code. Although preventive measures have been taken to ensure the code
'In normal cases there are no errors, but the file system is unpredictable. In the product code, use
' On Error Resume Next and Err objects to catch possible errors.
The following is the quoted content: ' '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' ' Some easily obtainable global variables ' '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Dim TabStop Dim NewLine Const TestDrive = "C" Const TestFilePath = "C:Test" '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' ' Constant returned by Drive.DriveType ' '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Const DriveTypeRemovable = 1 Const DriveTypeFixed = 2 Const DriveTypeNetwork = 3 Const DriveTypeCDROM = 4 Const DriveTypeRAMDisk = 5 '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' ' 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 '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' ' ShowDriveType ' ' Purpose: ' ' Generate a string that describes the drive type of a given Drive object. ' ' Demonstrate the following ' ' - Drive.DriveType ' '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Function ShowDriveType(Drive) Dim S Select Case Drive.DriveType Case DriveTypeRemovable S = "Removable" Case DriveTypeFixed S = "Fixed" Case DriveTypeNetwork S = "Network" Case DriveTypeCDROM S = "CD-ROM" Case DriveTypeRAMDisk S = "RAM Disk" Case Else S = "Unknown" End Select ShowDriveType = S End Function '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' ' ShowFileAttr ' ' Purpose: ' ' Generate a string that describes the properties of a file or folder. ' ' Demonstrate the following ' ' - File.Attributes ' - Folder.Attributes ' '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Function ShowFileAttr(File) ' File can be a file or a folder Dim S Dim Attr Attr = File.Attributes If Attr = 0 Then ShowFileAttr = "Normal" Exit Function End If If Attr And FileAttrDirectory Then S = S & "Directory" If Attr And FileAttrReadOnly Then S = S & "Read-Only" If Attr And FileAttrHidden Then S = S & "Hidden" If Attr And FileAttrSystem Then S = S & "System" If Attr And FileAttrVolume Then S = S & "Volume" If Attr And FileAttrArchive Then S = S & "Archive" If Attr And FileAttrAlias Then S = S & "Alias" If Attr And FileAttrCompressed Then S = S & "Compressed" ShowFileAttr = S End Function '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' ' GenerateDriveInformation ' ' Purpose: ' ' Generate a string that describes the current state of the available drive. ' ' Demonstrate the following ' ' - FileSystemObject.Drives ' - Iterating the Drives collection ' - Drives.Count ' - Drive.AvailableSpace ' - Drive.DriveLetter ' - Drive.DriveType ' - Drive.FileSystem ' - Drive.FreeSpace ' - Drive.IsReady ' - Drive.Path ' - Drive.SerialNumber ' - Drive.ShareName ' - Drive.TotalSize ' - Drive.VolumeName ' '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Function GenerateDriveInformation(FSO) Dim Drives Dim Drive Dim S Set Drives = FSO.Drives S = "Number of drives:" & TabStop & Drives.Count & NewLine & NewLine ' Construct the first line of the report. S = S & String(2, TabStop) & "Drive" S = S & String(3, TabStop) & "File" S = S & TabStop & "Total" S = S & TabStop & "Free" S = S & TabStop & "Available" S = S & TabStop & "Serial" & NewLine ' Construct the second line of the report. S = S & "Letter" S = S & TabStop & "Path" S = S & TabStop & "Type" S = S & TabStop & "Ready?" S = S & TabStop & "Name" S = S & TabStop & "System" S = S & TabStop & "Space" S = S & TabStop & "Space" S = S & TabStop & "Space" S = S & TabStop & "Number& NewLine ' Separate rows. S = S & String(105, "-") & NewLine For Each Drive In Drives S = S & Drive.DriveLetter S = S & TabStop & Drive.Path S = S & TabStop & ShowDriveType(Drive) S = S & TabStop & Drive.IsReady If Drive.IsReady Then If DriveTypeNetwork = Drive.DriveType Then S = S & TabStop & Drive.ShareName Else S = S & TabStop & Drive.VolumeName End If S = S & TabStop & Drive.FileSystem S = S & TabStop & Drive.TotalSize S = S & TabStop & Drive.FreeSpace S = S & TabStop & Drive.AvailableSpace S = S & TabStop & Hex(Drive.SerialNumber) End If S = S & NewLine Next GenerateDriveInformation = S End Function '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' ' GenerateFileInformation ' ' Purpose: ' ' Generate a string to describe the current state of the file. ' ' Demonstrate the following ' ' - File.Path ' - File.Name ' - File.Type ' - File.DateCreated ' - File.DateLastAccessed ' - File.DateLastModified ' - File.Size ' '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Function GenerateFileInformation(File) Dim S S = NewLine & "Path:" & TabStop & File.Path S = S & NewLine & "Name:" & TabStop & File.Name S = S & NewLine & "Type:" & TabStop & File.Type S = S & NewLine & "Attribs:" & TabStop & ShowFileAttr(File) S = S & NewLine & "Created:" & TabStop & File.DateCreated S = S & NewLine & "Accessed:" & TabStop & File.DateLastAccessed S = S & NewLine & "Modified:" & TabStop & File.DateLastModified S = S & NewLine & "Size& TabStop & File.Size & NewLine GenerateFileInformation = S End Function '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' ' GenerateFolderInformation ' ' Purpose: ' ' Generate a string to describe the current state of the folder. ' ' Demonstrate the following ' ' - Folder.Path ' - Folder.Name ' - Folder.DateCreated ' - Folder.DateLastAccessed ' - Folder.DateLastModified ' - Folder.Size ' '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Function GenerateFolderInformation(Folder) Dim S S = "Path:" & TabStop & Folder.Path S = S & NewLine & "Name:" TabStop & Folder.Name S = S & NewLine & "Attribs:" & TabStop & ShowFileAttr(Folder) S = S & NewLine & "Created:" & TabStop & Folder.DateCreated S = S & NewLine & "Accessed:" & TabStop & Folder.DateLastAccessed S = S & NewLine & "Modified:" & TabStop & Folder.DateLastModified S = S & NewLine & "Size:" & TabStop & Folder.Size & NewLine GenerateFolderInformation = S End Function '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' ' GenerateAllFolderInformation ' ' Purpose: ' ' Generate a string to describe the current status of a folder and all files and subfolders. ' ' Demonstrate the following ' ' - Folder.Path ' - Folder.SubFolders ' - Folders.Count ' '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Function GenerateAllFolderInformation(Folder) Dim S Dim SubFolders Dim SubFolder Dim Files Dim File S = "Folder:" & TabStop & Folder.Path & NewLine & NewLine Set Files = Folder.Files If 1 = Files.Count Then S = S & "There is 1 file& & NewLine Else S = S & " There are & Files.Count & " files" & NewLine End If If Files.Count <> 0 Then For Each File In Files S = S & GenerateFileInformation(File) Next End If Set SubFolders = Folder.SubFolders If 1 = SubFolders.Count Then S = S & NewLine & "There is 1 sub folder& & NewLine & NewLine Else S = S & NewLine & " There are & SubFolders.Count & " subfolders& & NewLine & NewLine End If If SubFolders.Count <> 0 Then For Each SubFolder In SubFolders S = S & GenerateFolderInformation(SubFolder) Next S = S & NewLine For Each SubFolder In SubFolders S = S & GenerateAllFolderInformation(SubFolder) Next End If GenerateAllFolderInformation = S End Function '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' ' GenerateTestInformation ' ' Purpose: ' ' Generate a string to describe the current status of the C:Test folder and all files and subfolders. ' ' Demonstrate the following ' ' - FileSystemObject.DriveExists ' - FileSystemObject.FolderExists ' - FileSystemObject.GetFolder ' '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Function GenerateTestInformation(FSO) Dim TestFolder Dim S If Not FSO.DriveExists(TestDrive) Then Exit Function If Not FSO.FolderExists(TestFilePath) Then Exit Function Set TestFolder = FSO.GetFolder(TestFilePath) GenerateTestInformation = GenerateAllFolderInformation(TestFolder) End Function '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' ' DeleteTestDirectory ' ' Purpose: ' ' Clean the test directory. ' ' Demonstrate the following ' ' - FileSystemObject.GetFolder ' - FileSystemObject.DeleteFile ' - FileSystemObject.DeleteFolder ' - Folder.Delete ' - File.Delete ' '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Sub DeleteTestDirectory(FSO) Dim TestFolder Dim SubFolder Dim File <a name=DeleteFile> ' There are two ways to delete a file: FSO.DeleteFile(TestFilePath & "BeatlesOctopusGarden.txt") Set File = FSO.GetFile(TestFilePath & "BeatlesBathroomWindow.txt") File.Delete ' There are two ways to delete a folder: FSO.DeleteFolder(TestFilePath & "Beatles") FSO.DeleteFile(TestFilePath & "ReadMe.txt") Set TestFolder = FSO.GetFolder(TestFilePath) TestFolder.Delete End Sub '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' ' CreateLyrics ' ' Purpose: ' ' Create two text files in the folder. ' ' ' Demonstrate the following ' ' - FileSystemObject.CreateTextFile ' - TextStream.WriteLine ' - TextStream.Write ' - TextStream.WriteBlankLines ' - TextStream.Close ' '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Sub CreateLyrics(Folder) Dim TextStream </a><a name=CreateTextFile> Set TextStream = Folder.CreateTextFile("OctopusGarden.txt") </a><a name=WriteToFile> TextStream.Write("Octopus' Garden") ' Note that this statement does not add line wraps to the file. TextStream.WriteLine("(by Ringo Starr)") TextStream.WriteBlankLines(1) TextStream.WriteLine("I'd like to be under the sea in an octopus' garden in the shade,") TextStream.WriteLine("He'd let us in, knows where we've been -- in his octopus' garden in the shade.") TextStream.WriteBlankLines(2) </a><a name=Close> TextStream.Close Set TextStream = Folder.CreateTextFile("BathroomWindow.txt") TextStream.WriteLine("She Came In Through The Bathroom Window (by Lennon/McCartney)") TextStream.WriteLine("") TextStream.WriteLine("She came in through the bathroom window protected by a silver spoon") TextStream.WriteLine("But now she sucks her thumb and wanders by the banks of her own lagoon") TextStream.WriteBlankLines(2) TextStream.Close End Sub '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' ' GetLyrics ' ' Purpose: ' ' Show the contents of the lyrics file. ' ' ' Demonstrate the following ' ' - FileSystemObject.OpenTextFile ' - FileSystemObject.GetFile ' - TextStream.ReadAll ' - TextStream.Close ' - File.OpenAsTextStream ' - TextStream.AtEndOfStream ' - TextStream.ReadLine ' '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Function GetLyrics(FSO) Dim TextStream Dim S Dim File 'There are multiple ways to open a text file, and multiple ways to read data from the file. ' Here are two ways to open and read files: Set TextStream = FSO.OpenTextFile(TestFilePath & "BeatlesOctopusGarden.txt", OpenFileForReading) </a><a name=ReadFromFile> S = TextStream.ReadAll & NewLine & NewLine TextStream.Close Set File = FSO.GetFile(TestFilePath & "BeatlesBathroomWindow.txt") Set TextStream = File.OpenAsTextStream(OpenFileForReading) Do While Not TextStream.AtEndOfStream S = S & TextStream.ReadLine & NewLine Loop TextStream.Close GetLyrics = S End Function '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' 'BuildTestDirectory ' ' Purpose: ' ' Create a directory hierarchy to demonstrate FileSystemObject. ' ' Create a hierarchical structure in this order: ' ' C:Test ' C:TestReadMe.txt ' C:TestBeatles ' C:TestBeatlesOctopusGarden.txt ' C:TestBeatlesBathroomWindow.txt ' ' ' Demonstrate the following ' ' - FileSystemObject.DriveExists ' - FileSystemObject.FolderExists ' - FileSystemObject.CreateFolder ' - FileSystemObject.CreateTextFile ' - Folders.Add ' - Folder.CreateTextFile ' - TextStream.WriteLine ' - TextStream.Close ' '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' </a><a name=FolderInfo> Function BuildTestDirectory(FSO) Dim TestFolder Dim SubFolders Dim SubFolder Dim TextStream ' Excludes (a) the drive does not exist, or (b) the directory to be created already exists. If Not FSO.DriveExists(TestDrive) Then BuildTestDirectory = False Exit Function End If If FSO.FolderExists(TestFilePath) Then BuildTestDirectory = False Exit Function End If Set TestFolder = FSO.CreateFolder(TestFilePath) Set TextStream = FSO.CreateTextFile(TestFilePath & "ReadMe.txt") TextStream.WriteLine("My song lyrics collection") TextStream.Close Set SubFolders = TestFolder.SubFolders Set SubFolder = SubFolders.Add("Beatles") CreateLyrics SubFolder BuildTestDirectory = True End Function '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' ' Main program ' ' First, it creates a test directory, as well as some subfolders and files. ' Then, it dumps some information about the available disk drives and test directories, ' Finally, clear the test directory and all its contents. ' '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Sub Main Dim FSO ' Set up global variables. TabStop = Chr(9) NewLine = Chr(10) </a><a name=CreateFSO> Set FSO = CreateObject("Scripting.FileSystemObject") If Not BuildTestDirectory(FSO) Then Print "Test directory already exists or cannot be created. Cannot continue." Exit Sub End If Print GenerateDriveInformation(FSO) & NewLine & NewLine Print GenerateTestInformation(FSO) & NewLine & NewLine Print GetLyrics(FSO) & NewLine & NewLine DeleteTestDirectory(FSO) End Sub |
Share: Interpret the usage of RegExp Object Function of Asp RegExp objects provide simple regular expression support. Usage of RegExp object: The following is the referenced content: Function RegExpTest(patrn, strng) Dim re