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

Properties Description
Drives The Drives property returns a collection of all Drive objects on the computer. This includes network drives that are mapped from this machine. You can iterate through this property with the For…Each…Next construct.

Methods

Methods Description
BuildPath The BuildPath method adds the file or folder name to an existing path.
CopyFile The CopyFile method copies one or more files from one folder to another.
CopyFolder The CopyFolder method copies one or more folders from one folder to another.
CreateFolder The CreateFolder method creates a new folder. An error occurs if this folder already exists.
CreateTextFile The CreateText File method creates a new text file and returns a TextStream object that refers to it.
DeleteFile The DeleteFile method deletes one or more specified files.
DeleteFolder The DeleteFolder method deletes one or more specified folders.
DriveExists The DriveExist method determines whether specified drive exists. Returns True if the specified drive exists, or False if not.
FileExists The FileExist method determines whether specified file exists. Returns True if the specified file exists, or False if not.
FolderExists The FolderExist method determines whether specified folder exists. Returns True if the specified folder exists, or False if not.
GetFile The GetFile method returns a File object for a specified path. This can be a relative or absolute path to the required file.
GetFileName The GetFileName method returns the file name or the last folder name in a specified path. It does not check for existence of the file or folder.
GetFolder The GetFolder method returns a Folder object for a specified path. This can be a relative or absolute path to the required folder.
GetParentFolderName The GetParentFolderName method returns the name of the parent folder of the file or folder in a specified path.
GetSpecialFolder The GetSpecialFolder method returns a Folder object corresponding to one of the Windows’ special folders.
GetDrive The GetDrive method returns a Drive object of a specified path.
GetDriveName The GetDriveName method returns the drive name in a specified path. This can be an absolute path to a file or folder, or just the drive letter such as “c:” or just “c”.
GetAbsolutePathName The GetAbsolutePathName method returns the full path from the root of the drive for the specified path.
GetBaseName The BaseName method returns the base name of a specified file or folder, i.e. with the path and file extension removed.
GetExtensionName The GetExtensionName method returns the file extension name in a specified path.
GetTempName The GetTempName method returns a randomly generated file of folder. This method only returns a temporary filename but does not create the actual file.
MoveFile The MoveFile method moves one or more files from one location to another. This method is similar to the File object’s Move method, but no File object is required.
MoveFolder The MoveFolder method moves one or more folders from one location to another.
OpenTextFile The OpenTextFile method creates a new file or opens an existing one, and returns TextStream object that refers to it. You can then read from, write to, or append to this file.

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
%>
admin

admin

Leave a Reply

Your email address will not be published.