![]() |
WebCheatSheet.com |
![]() |
ASP FileSystemObject Object |
The FileSystemObject object provides access to the file system on the web server, allowing us to manipulate text files, folders and drives from within our code. The properties and methods of the FileSystemObject object are as follows: Properties
Methods
How to use the ASP FileSystemObject Object In the example below we will create a new text file test.txt and write some text to it. At first we create an instance of the FilesystemObject object and then use the CreateTextFile method to create a new text file. We set the optional overwrite parameter to True, so any existing file with the same path and name will be overwritten. The CreateTextFile returns a TextStream object that we will use to write some text to the file. <%
Dim objFSO, objTStream 'Create an instance of the FileSystemObject object Set objFSO = Server.CreateObject("Scripting.FileSystemObject") 'Create a text file named myFile.txt, replacing any existing one with the same name Set objTStream = objFSO.CreateTextFile("C:\test.txt", True) 'Write some text to the file objTStream.WriteLine("Hello World!") objTStream.WriteLine() objTStream.WriteLine("This is my first text file!") 'Close the TextStream object objTStream.Close 'Free up resources Set objTStream = nothing Set objFSO = nothing %> In the following example we will read the test.txt file contents. At first we create an instance of the FileSystemObject object and then use the OpenTextFile method to open test.txt file for reading. This method returns a TextStream object that we will use to read data from the file. <% Dim objFSO, objTStream 'Create an instance of the FileSystemObject object Set objFSO = Server.CreateObject("Scripting.FileSystemObject") 'Opens for reading a text file named myFile.txt Set objTStream = objFSO.OpenTextFile("C:\test.txt", 1) 'Read one line at a time until the end of the file is reached Do Until objTStream.AtEndOfStream Response.Write "Line " & objTStream.Line & ": " & objTStream.ReadLine & "<br />" Loop 'Close the Textstream object objTStream.Close 'Free up resources Set objTStream = nothing Set objFSO = nothing %> |
![]() |
![]() |
Copyright © 2005-2007 www.WebCheatSheet.com All Rights Reserved. |