File Access Component | |
In addition to the native ASP objects
(Request, Response, etc.) and the various installable components (Ad
Rotator, Browser Capabilities, etc.), you also have access to a third
group of objects. These objects are instantiated directly from the
Microsoft Scripting Runtime DLL ( All file manipulation is performed by the FileSystemObject object. Your application will have only one of these, and it represents your application's "window" onto the system's file structures. With this object, you are able to perform some simple functions such as opening and closing files, but the real strength of this object is that through it you are able to instantiate the other file manipulation objects: Drive, Folder, and File. Through these objects, your application has almost all the power over the file system that you have through a command-line interface. |
Accessory Files/Required DLL Files | |
|
Instantiating Installable Components | |
To create an object variable containing an instance of the FileSystemObject component, use the CreateObject method of the Server object. The syntax for the CreateObject method is as follows: Set objMyObject = Server.CreateObject(strProgId) where:
Example <% ' The following code uses the CreateObject method of ' the Server object to instantiate a FileSystemObject. Dim fsFileSystemObject Set fsFileSystemObject = _ Server.CreateObject("Scripting.FileSystemObject") %> For more details on the use of the CreateObject method, see its entry in Chapter 9. |
Comments/Troubleshooting | |
The file access components of
|
AtEndOfLine (TextStream Object) | |
tsObj.AtEndOfLine | |
A Boolean value that indicates whether the file pointer is at the end of the current line. This is a read-only property. |
|
Parameters | |
None |
|
Example | |
The following code instantiates a FileSystemObject and a TextStream object. It then uses the Read method to read one character at a time until the end of the line is reached. Notice that the use of the AtEndOfStream and AtEndOfLine properties are identical. <% ' Set up constants. Const constForReading = 1 Const constTristateFalse = 0 ' Dimension local variables. Dim fsoObject ' FileSystemObject Dim tsObject ' TextStream Object Dim strReturned ' String variable to hold file contents ' Instantiate the FileSystemObject variable. Set fsoObject = Server.CreateObject( _ "Scripting.FileSystemObject") ' Using the OpenTextFile method of fsoObject, ' create a text file. Set tsObject = _ fsoObject.OpenTextFile("d:\docs\test.txt", _ constForReading, constTristateFalse) ' Read one character at a time until the end of the ' line has been reached. Do While Not tsObject.AtEndOfLine StrReturned = strReturned & tsObject.Read(1) Loop |
|
Notes | |
If you attempt to use the AtEndOfLine property with a text file opened for any purpose other than reading, you will receive an error. The AtEndOfLine property will not inform you that you have reached the end of the file. |
|
AtEndOfStream (TextStream Object) | |
tsObj.AtEndOfStream | |
A Boolean value that indicates whether you have reached the end of the current text file. This is a read-only property. |
|
Parameters | |
None |
|
Example | |
The following code instantiates a FileSystemObject and a TextStream object. Then it uses the Read method to read one character at a time until the end of the file is reached. Notice that the use of the AtEndOfStream and AtEndOfLine properties are identical. <% ' Set up constants. Const constForReading = 1 Const constTristateFalse = 0 ' Dimension local variables. Dim fsoObject ' FileSystemObject Dim tsObject ' TextStream Object Dim strReturned ' String variable to hold file contents. ' Instantiate the FileSystemObject variable. Set fsoObject = Server.CreateObject( _ "Scripting.FileSystemObject") ' Using the OpenTextFile method of fsoObject, create ' a text file. Set tsObject = _ fsoObject.OpenTextFile("d:\docs\test.txt", _ constForReading, constTristateFalse) ' Read one character at a time until the end of the ' file has been reached Do While Not tsObject.AtEndOfStream StrReturned = strReturned & tsObject.Read(1) Loop |
|
Notes | |
If you attempt to use the AtEndOfStream property with a text file opened for any purpose other than reading, you will receive an error. |
|
Attributes (File Object, Folder Object) | |||||||||||||||||||||||||||||||
Normal | |||||||||||||||||||||||||||||||
An integer containing a combination of values representing various file system attributes. This property is read-only or read/write depending on the specific file attribute in question. The following table lists the values that the Attributes property can contain. To determine whether a File or Folder object has a particular value, use the bitwise And operator to compare the Attributes property value and the specific constant in which you're interested. If the result is True, then that specific attribute is True. See the following examples.
|
|||||||||||||||||||||||||||||||
Parameters | |||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||
Example | |||||||||||||||||||||||||||||||
The following code uses the Attributes property first with a File object, and then with a Folder object. <% ' Dimension local variables. Dim fsoObject ' FileSystemObject Dim filObject ' File Object Dim fdrObject ' Folder Object ' Declare constants. Const Hidden = 2 Const Archive = 32 ' Instantiate the FileSystemObject variable. Set fsoObject = Server.CreateObject( _ "Scripting.FileSystemObject") ' Using the GetFile method of fsoObject, initialize the ' File object. Set filObject = fsoObject.GetFile("d:\docs\test.txt") ' Set the Hidden (value = 2) and Archive (value = 32) ' attributes for the Test.TXT file. filObject.Attributes = (Hidden + Archive) ' Using the GetFolder method of fsoObject, initialize ' the Folder object. Set fdrObject = fsoObject.GetFolder("d:\docs") ' Determine whether the folder is itself hidden. If (fdrObject.Attributes And Archive) Then ' Folder is hidden. Else ' Folder is NOT hidden. End If |
|||||||||||||||||||||||||||||||
Notes | |||||||||||||||||||||||||||||||
If you attempt to use the read-only attributes that deal only with File objects with a Folder object, the result is always a False value. However, if you attempt to set any of the read-only attributes for File or Folder objects, the result is an error. Note that you must explicitly declare constants for use with the File Access components. |
|||||||||||||||||||||||||||||||
AvailableSpace (Drive Object) | |
drvObj. AvailableSpace | |
The number of bytes of space left on the current drive. It is inaccurate for drives with over 2GB of available space. This is a read-only property. |
|
Parameters | |
None |
|
Example | |
<% ' Dimension local variables. Dim fsoObject ' FileSystemObject Dim drvObject ' Drive Object Dim lngAvailBytes ' Number of bytes available ' Instantiate the FileSystemObject variable. Set fsoObject = Server.CreateObject( _ "Scripting.FileSystemObject") ' Using the GetDrive method of fsoObject, initialize a ' Drive object. Set drvObject = fsoObject.GetDrive("\\PublicDocs") ' Retrieve the amount of space (in bytes) available ' on the drive. lngAvailBytes = drvObject.AvailableSpace |
|
Notes | |
The only time the value for the AvailableSpace property and the value for the FreeSpace property will be different is if the drive supports quotas. For all practical purposes, you can use these two properties interchangeably. |
|
DateCreated (File Object, Folder Object) | |
Obj.DateCreated | |
A date value that represents the date the file or folder was created. This is a read-only value controlled by the operating system. |
|
Parameters | |
None |
|
Example | |
<% ' Dimension local variables. Dim fsoObject ' FileSystemObject. Dim fdrObject ' Folder object. Dim datCreated ' Date variable. ' Instantiate the FileSystemObject variable. Set fsoObject = Server.CreateObject( _ "Scripting.FileSystemObject") ' Using the GetFolder method of fsoObject, initialize ' a Folder object Set fdrObject = fsoObject.GetFolder("c:\Docs") ' Retrieve the date the folder was created. datCreated = fdrObject.DateCreated |
|
Notes | |
The value of this property indicates the date the file was created, not the date the file was written to the current drive. |
|
Drive (File Object, Folder Object) | |
Obj.Drive | |
Returns a Drive with which the File or Folder object is associated. This property is read-only. |
|
Parameters | |
None |
|
Example | |
<% ' Dimension local variables. Dim fsoObject ' FileSystemObject Dim filObject ' File Object Dim objDrive ' Drive name ' Instantiate the FileSystemObject variable. Set fsoObject = Server.CreateObject( _ "Scripting.FileSystemObject") ' Using the GetFile method of fsoObject, initialize ' a File object. Set filObject = fsoObject.GetFile("PublicDocs.txt") ' Retrieve the drive name with which the File object ' is associated. Set objDrive = filObject.Drive ' Note that this drive is actually the current drive ' in this case. |
|
Notes | |
The Drive property can represent either a physical, local, or mapped drive or a network share. Because the Drive object's default property is Path, you can assign the drive name to a string as follows: strDrive = filObject.Drive This is really a shorthand version of: strDrive = filObject.Drive.Path If you wish to manipulate the Drive object, though, you must use the Set statement to assign the reference to an object variable. For example: Set objDrive = filObject.Drive |
|
FileSystem (Drive Object) | |
drvObj.FileSystem | |
A string value that represents the file system type used to format the current drive. The recognized file system types are CDFS, NTFS, FAT, and FAT32. This is a read-only property. |
|
Parameters | |
None |
|
Example | |
<% ' Dimension local variables. Dim fsoObject ' FileSystemObject Dim drvObject ' Drive Object Dim strFileSys ' File system of drive ' Instantiate the FileSystemObject variable. Set fsoObject = Server.CreateObject( _ "Scripting.FileSystemObject") ' Using the GetDrive method of fsoObject, initialize ' a Drive object. Set drvObject = fsoObject.GetDrive("\\PublicDocs") ' Retrieve the file system for the drive. This value ' will contain one of the following strings: ' NTFS, FAT, or CDFS. strFileSys = drvObject.FileSystem |
|
Notes | |
You can rely on the value of the FileSystem property of a Drive object to reflect cluster sizes and security features available for the current drive. |
|
IsReady (Drive Object) | |
drvObj.IsReady | |
A Boolean value representing whether the current drive is available for reading or writing. Use this property, for example, to determine whether a floppy disk or CD has been placed in a drive. This is a read-only property. |
|
Parameters | |
None |
|
Example | |
<% ' Dimension local variables. Dim fsoObject ' FileSystemObject Dim drvObject ' Drive Object ' Instantiate the FileSystemObject variable. Set fsoObject = Server.CreateObject( "Scripting.FileSystemObject") ' Using the GetDrive method of fsoObject, initialize a ' Drive object. Set drvObject = fsoObject.GetDrive("\\PublicDocs") ' Check to see if the drive is ready. If drvObject.IsReady Then ' Drive is ready for read/write. Else ' Drive is not ready. End If |
|
Notes | |
It is a good idea to use the IsReady property before attempting to do any drive access. It can be used to determine the readiness of removable-media drives (floppy and CD-ROM drives) and fixed-media drives. |
|
IsRootFolder (Folder Object) | |
fdr.IsRootFolder | |
A Boolean value that allows you to determine if the current folder is the root folder. This is a read-only property. |
|
Parameters | |
None |
|
Example | |
<% ' Dimension local variables. Dim fsoObject ' FileSystemObject Dim fdrObject ' Folder Object ' Instantiate the FileSystemObject variable. Set fsoObject = Server.CreateObject( _ "Scripting.FileSystemObject") ' Using the GetFolder method of fsoObject, initialize a ' File object. Set fdrObject = fsoObject.GetFolder("PublicDocs.txt") ' Determine whether the current folder is a root folder ' or if it is nested. If fdrObject.IsRootFolder Then ' Folder is located directly off the drive letter ' or share name. Else ' The folder is nested within at least one other ' folder. End If |
|
Notes | |
The Microsoft documentation shows how to use this property to determine to how many levels the current folder is nested. For convenience, the following code demonstrates this: <% ' Dimension local variables. Dim fsoObject ' FileSystemObject Dim fdrObject ' Folder Object Dim intNestedLevel ' Level to which the folder is nested ' Instantiate the FileSystemObject variable. Set fsoObject = Server.CreateObject( _ "Scripting.FileSystemObject") ' Using the GetFolder method of fsoObject, initialize a ' File object. Set fdrObject = fsoObject.GetFolder("PublicDocs.txt") ' Determine whether the current folder is a root folder ' or if it is nested. If fdrObject.IsRootFolder Then ' Folder is located directly off the drive letter or ' share name. Else ' For more on the ParentFolder property of the ' Folder object, see the following. Do Until fdrObject.IsRootFolder Set fdrObject = fdrObject.ParentFolder intNestedLevel = intNestedLevel + 1 Loop End If |
|
ParentFolder (File Object, Folder Object) | |
Obj.ParentFolder | |
Returns a Folder object representing the folder in which the file or folder is located. This is a read-only property. |
|
Parameters | |
None |
|
Example | |
The following code demonstrates the use of the ParentFolder property when used with a File object and then with a Folder object. Note that, because Name is the default property of a Folder object, the code in the ASP page appears to treat the value returned by the ParentFolder property as a string. <% ' Dimension local variables. Dim fsoObject ' FileSystemObject Dim filObject ' File Object Dim fdrObject ' Folder Object Dim strFileParent ' Parent folder of file object Dim strFolderParent ' Parent folder of folder object ' Instantiate the FileSystemObject variable. Set fsoObject = Server.CreateObject( _ "Scripting.FileSystemObject") ' Using the GetFile method of fsoObject, initialize the ' File object. Set filObject = fsoObject.GetFile("d:\docs\test.txt") ' Retrieve the name of the folder containing the file Test.TXT. ' In this example, the value of strFileParent is "docs". strFileParent = filObject.ParentFolder ' Using the GetFolder method of fsoObject, initialize ' the Folder object. Set fdrObject = fsoObject.GetFolder("d:\mystuff\docs") ' Retrieve the name of the folder that contains the ' folder "docs". In this example, the value of ' strFileParent is "mystuff". strFolderParent = fdrObject.ParentFolder |
|
Close (TextStream Object) | |
tsObj.Close | |
Closes a text file that has been opened as a TextStream object. |
|
Parameters | |
None |
|
Example | |
<% ' Dimension local variables. Dim fsoObject ' FileSystemObject Dim tsObject ' TextStream Object ' Instantiate the FileSystemObject variable. Set fsoObject = Server.CreateObject( _ "Scripting.FileSystemObject") ' Using the OpenTextFile method of fsoObject, initialize ' the File object. Set tsObject = fsoObject.OpenTextFile( _ "d:\docs\test.txt", ForReading, False) ' Read into the string the contents of the text file. strContents = tsObject.ReadAll ' Close the open text file. tsObject.Close |
|
Notes | |
You can have only a limited number of open files in your application (similar to the use of open files in Visual Basic), so it is important to close all open text files after you are finished with them. |
|
Copy (File Object, Folder Object) | |
obj.Copy strDestination [, blnOverWrite] | |
Copies a file from one location to another. |
|
Parameters | |
|
|
Example | |
<% ' Dimension local variables. Dim fsoObject ' FileSystemObject Dim filObject ' File Object ' Instantiate the FileSystemObject variable. Set fsoObject = Server.CreateObject( _ "Scripting.FileSystemObject") ' Using the GetFile method of fsoObject, initialize ' the File object. Set filObject = fsoObject.GetFile("d:\docs\test.txt") ' Copy the file to a temporary directory. filObject.Copy "e:\storage\temp\test_copy.txt", True |
|
Notes | |
The Copy method performs exactly the same function as the CopyFile and CopyFolder methods of the FileSystemObject object. However, it is important to note that the CopyFile and CopyFolder methods will allow you to copy more than one file or folder at a time. |
|
CopyFolder (FileSystemObject Object) | |
fsoObj.CopyFolder strSource, strDestination [, blnOverWrite] | |
Allows you to copy a folder and all of its contents from one location to another. |
|
Parameters | |
|
|
Example | |
<% ' Dimension local variables. Dim fsoObject ' FileSystemObject ' Instantiate the FileSystemObject variable. Set fsoObject = Server.CreateObject( _ "Scripting.FileSystemObject") ' Use the FileSystemObject object's CopyFolder method ' to copy the Temp directory and all its contents from ' the C drive to the D drive, overwriting if necessary. fsoObject.CopyFolder "c:\temp", "d:\temp", True |
|
Notes | |
If an error is raised when calling CopyFolder, the method stops immediately and does not reverse any actions already performed. The CopyFolder method is as fast as copying the folder using the command line. |
|
CreateFolder (FileSystemObject Object) | |
fsoObj.CreateFolder( strFolderName) | |
Creates a folder in a specified location. |
|
Parameters | |
|
|
Example | |
<% ' Dimension local variables. Dim fsoObject ' FileSystemObject ' Instantiate the FileSystemObject variable. Set fsoObject = Server.CreateObject( _ "Scripting.FileSystemObject") ' Create a new directory. fsoObject.CreateFolder("e:\storage\newdir") |
|
Notes | |
If you attempt to create a folder that already exists, an error will be raised. |
|
Delete (File Object, Folder Object) | |
Obj.Delete blnForce | |
Deletes a file or folder. |
|
Parameters | |
|
|
Example | |
<% ' Dimension local variables. Dim fsoObject ' FileSystemObject Dim filObject ' File Object ' Instantiate the FileSystemObject variable. Set fsoObject = Server.CreateObject( _ "Scripting.FileSystemObject") ' Using the GetFile method of fsoObject, initialize the ' File object. Set filObject = fsoObject.GetFile("d:\docs\test.txt") ' Delete the TEST.TXT file--even if the file is marked ' as read-only. filObject.Delete True |
|
Notes | |
The Delete method of the File and Folder objects is functionally the same as the DeleteFile and DeleteFolder methods of the FileSystemObject object. If you use the Delete method of a Folder object, that folder and all of its contents will be deleted. The method will not warn you if you attempt to delete a directory that contains files. |
|
GetBaseName (FileSystemObject Object) | |
fsoObj.GetBaseName( strPath) | |
Extracts the name of a file—minus any file extension—from a full file path. |
|
Parameters | |
|
|
Example | |
<% ' Dimension local variables. Dim fsoObject ' FileSystemObject ' Instantiate the FileSystemObject variable. Set fsoObject = Server.CreateObject( _ "Scripting.FileSystemObject") ' Using the GetBaseName method, retrieve the base ' names of several path strings. ' This example returns "searchstart" as the base name. Response.Write fsoObject.GetBaseName( _ "/apps/search/searchstart.asp") ' This example returns "search" as the base name. Response.Write fsoObject.GetBaseName("/apps/search/") ' This example returns "search" as the base name. Response.Write fsoObject.GetBaseName("/apps/search") ' This example returns "nofile" as the base name--even ' though the nofile.txt file does not exist. fsoObject.GetBaseName("/apps/search/nofile.txt") |
|
Notes | |
GetBaseName attempts to retrieve the base name for a file from a path string. If the last element in the path string is a folder, the folder name is returned—even if you include a closing slash (/) or backslash (\) character. The path string is not checked for its validity or its existence as a real path on the server. The method just looks at the path as a string. For this reason, the association of this method with the FileSystemObject object is deceiving, since no file manipulation actually occurs. |
|
GetParentFolderName (FileSystemObject Object) | |
fsoObj.GetFolderName ( strPath) | |
Determines the name of the last parent folder in a given path string. |
|
Parameters | |
|
|
Example | |
<% ' Dimension local variables. Dim fsoObject ' FileSystemObject ' Instantiate the FileSystemObject variable. Set fsoObject = Server.CreateObject( "Scripting.FileSystemObject") ' Using the GetParentFolderName method, retrieve the ' parent folder names of several path strings. ' This example returns "search" as the parent folder ' name. Response.Write fsoObject.GetParentFolderName( _ "/apps/search/searchstart.asp") ' This example return "apps" as the parent folder name Response.Write fsoObject.GetParentFolderName ("/apps/search/") ' This example also returns "apps" as the parent folder ' name. Response.Write fsoObject.GetParentFolderName ("/apps/search") ' This example returns "nofile" as the parent folder ' name--even though nofile.txt does not exist. Response.Write fsoObject.GetParentFolderName( _ "/apps/search/nofile.txt") |
|
Notes | |
Like the GetBaseName method of the FileSystemObject object, the GetParentFolderName method acts only on the path string itself. The path string argument is not checked for validity or existence. |
|
GetSpecialFolder (FileSystemObject Object) | |||||||||||||
fsoObj.GetSpecialFolder (intSpecialFolderType) | |||||||||||||
Retrieves the full physical path of a special folder on the web server. |
|||||||||||||
Parameters | |||||||||||||
|
|||||||||||||
Example | |||||||||||||
<% ' Dimension local variables. Dim fsoObject ' FileSystemObject ' Declare file constants. Const WindowsFolder = 0 Const SystemFolder = 1 Const TemporaryFolder = 2 ' Instantiate the FileSystemObject variable. Set fsoObject = Server.CreateObject( _ "Scripting.FileSystemObject") ' Use GetSpecialFolder to retrieve the physical path ' for the Windows, System, and Temp directories. ' This example returns something similar to "C:\WINNT". fsoObject.GetSpecialFolder(WindowsFolder) ' This example returns something similar to ' "C:\WINNT\SYSTEM32". fsoObject.GetSpecialFolder(SystemFolder) ' This example returns something similar to ' "C:\WINNT\TEMP" fsoObject.GetSpecialFolder(TemporaryFolder) |
|||||||||||||
Notes | |||||||||||||
Note that you must explicitly declare constants for use with the file access components. |
|||||||||||||
MoveFolder (FileSystemObject Object) | |
fsoObj.MoveFolder strSourcePath, strDestinationPath | |
Moves a folder and all its contents from one location to another. |
|
Parameters | |
|
|
Example | |
<% ' Dimension local variables. Dim fsoObject ' FileSystemObject ' Instantiate the FileSystemObject variable. Set fsoObject = Server.CreateObject( _ "Scripting.FileSystemObject") ' Using the MoveFolder method, move all the folders ' under C:\APPS to the D: drive. fsoObject.MoveFolder "C:\APPS\*.*", "D:\" |
|
Notes | |
If you attempt to move a folder to a destination that is already a filename, you will receive an error. If the destination you provide represents the name of a preexisting folder, you will receive an error unless the source argument ends with a wildcard or a backslash (\). In this case, the source folder (or folders) and all its contents will be moved to the destination folder. For example, the following code results in an error: <% ' Assume FileSystemObject object is instantiated 'already. Also assume that D:\ apps already exists. fsoObject.MoveFolder "C:\apps", "d:\apps" %> whereas the following code would not result in an error: <% ' Assume FileSystemObject object is instantiated ' already. Also assume that D:\ apps already exists. fsoObject.MoveFolder "C:\apps\*.*", "d:\apps" ' This last line create an apps folder in the D:\apps ' folder (making D:\apps\apps) %> Note that if the web server experiences an error when calling MoveFolder, all actions stop without any rollback of previous actions. For example, if you attempt to move a series of three folders with all their contents and an error occurs on the third folder to be moved, the first two folders remain moved even though the third is not. You must include your own code to check for which files and folders were actually moved and which were not. If you attempt to move folders between volumes, the underlying operating system must support this, and user security on the web server must allow for this. |
|
OpenAsTextStream (File Object) | |||||||||||||||||||||||||
tsObject =filObj.OpenAsTextStream [ intAccessMode][, intFormat] | |||||||||||||||||||||||||
Opens a file and creates a TextStream object that you can use to read or modify the text file. The method returns a TextStream object. |
|||||||||||||||||||||||||
Parameters | |||||||||||||||||||||||||
|
|||||||||||||||||||||||||
Example | |||||||||||||||||||||||||
<% ' Dimension local variables. Dim fsoObject ' FileSystemObject Dim filObject ' File Object Dim tsObject ' TextStream object ' Declare File Access constants. Const ForAppending = 8 Const TristateTrue = -1 ' Instantiate the FileSystemObject variable. Set fsoObject = Server.CreateObject( _ "Scripting.FileSystemObject") ' Using the GetFile method of fsoObject, initialize the ' File object. Set filObject = fsoObject.GetFile("d:\docs\test.txt") ' Use the OpenAsTextStream method to open the file for ' appending and in Unicode format. Set tsObject = filObject.OpenAsTextStream(ForAppending, TristateTrue) %> |
|||||||||||||||||||||||||
Notes | |||||||||||||||||||||||||
The OpenAsTextStream method is virtually equivalent to the OpenTextFile method of the FileSystemObject object. The only difference is that the OpenAsTextStream method also can be used to create a new text file if one does not already exist. Note that you must explicitly declare constants for use with the File Access components. |
|||||||||||||||||||||||||
ReadLine (TextStream Object) | |
tsObj.ReadLine | |
The ReadLine method is similar to the Read method of the TextStream object in that it allows you to read from a text file into a string variable or compare the results of such a read to another entity. However, unlike the Read method, which uses an argument to determine how many characters to read, the ReadLine method reads all characters from the current pointer location to the next newline character. |
|
Parameters | |
None |
|
Example | |
<% ' Dimension local variables. Dim fsoObject ' FileSystemObject Dim filObject ' File Object Dim tsObject ' TextStream object Dim strBuffer ' Holding buffer ' Declare file access constants. Const ForReading = 1 Const TristateFalse = 0 ' Instantiate the FileSystemObject variable. Set fsoObject = Server.CreateObject( _ "Scripting.FileSystemObject") ' Using the GetFile method of fsoObject, initialize the ' File object. Set filObject = fsoObject.GetFile("d:\docs\test.txt") ' Use the OpenAsTextStream method to open the file for ' reading and in ASCII format. Set tsObject = filObject.OpenAsTextStream(ForReading, TristateFalse) ' Use the ReadLine method to read the next line of text ' from the text file into the strBuffer variable. strBuffer = tsObject.ReadLine %> |
|
Notes | |
After calling the ReadLine method, the current location of the pointer within the file is the character immediately after the last newline character or at the end of file marker. Note that you must explicitly declare constants for use with the File Access components. |
|
Write (TextStream Object) | |
tsObj.Write(strWriteString) | |
Writes a specified string to an open text file at the current location of the file pointer. |
|
Parameters | |
|
|
Example | |
<% ' Dimension local variables. Dim fsoObject ' FileSystemObject Dim filObject ' File Object Dim tsObject ' TextStream object Dim strEnding ' Declare file access constants. Const ForAppending = 8 Const TristateFalse = 0 ' Initialize string variable. This string will be ' written to the end of the file opened next. strEnding = "This is the end, my only friend, the end..." ' Instantiate the FileSystemObject variable. Set fsoObject = Server.CreateObject( _ "Scripting.FileSystemObject") ' Using the GetFile method of fsoObject, initialize the ' File object. Set filObject = fsoObject.GetFile("d:\docs\test.txt") ' Use the OpenAsTextStream method to open the file for ' appending and in Unicode format. Set tsObject = filObject.OpenAsTextStream(ForAppending, TristateFalse) ' Write a short string to the end of the opened file. tsObject.Write strEnding |
|
Notes | |
The Write method does not place any characters at the beginning or end of each written string. For this reason, if you use the Write method to add to a file, make sure that you include any desired characters (like spaces or newline characters) at the beginning or end of the strings you write to the file. |
|
WriteLine (TextStream Object) | |
tsObj.WriteLine([strWriteString]) | |
Writes a string's value into an open file at the location of the pointer within the file. This method also writes a newline character to the end of the added string. Otherwise, it is exactly the same as the Write method.
|
|
Example | |
<% ' Dimension local variables. Dim fsoObject ' FileSystemObject Dim filObject ' File Object Dim tsObject ' TextStream object Dim strEnding ' Declare file access constants. Const ForAppending = 8 Const TristateFalse = 0 ' Initialize a string variable that will be written to ' the end of the file opened next. strEnding = "This is the end, my only friend, the end..." ' Instantiate the FileSystemObject variable. Set fsoObject = Server.CreateObject( _ "Scripting.FileSystemObject") ' Using the GetFile method of fsoObject, initialize the ' File object. Set filObject = fsoObject.GetFile("d:\docs\test.txt") ' Use the OpenAsTextStream method to open the file for ' appending and in Unicode format. Set tsObject = filObject.OpenAsTextStream(ForAppending, TristateFalse) ' Write a short string plus a newline character to the ' end of the opened file. tsObject.WriteLine strEnding |
|
Notes | |
After calling the WriteLine method, the file pointer will point to the character located immediately after the newline character added to the file. |
|