Computers & ProgrammingASPBackend Development

ASP FileSystem Object

In ASP, the FileSystemObject can be used to access the file system on a web server. Accessing file, folder, and drive information on the web server is completed using this object.

Before you can access specific information on the file system, you must first create the FileSystemObject. In the following example, we will create and destroy a FileSystemObject.

<%
Dim fsObj
Set fsObj = Server.CreateObject("Scripting.FileSystemObject")
Set fsObj = nothing
%>

The FileSystemObject object’s, like other ASP objects, has its own properties and methods

Properties

PropertyDescription
DrivesReturns a collection of all Drive objects on the server

Methods

MethodDescription
BuildPathAppends a name to an existing path
CopyFileCopies one or more files from one location to another
CopyFolderCopies one or more folders from one location to another
CreateFolderCreates a new folder
CreateTextFileCreate a text file or write to a file
DeleteFileDeletes one or more specified files
DeleteFolderDeletes one or more specified folders
DriveExistsChecks if a specified drive exists
FileExistsChecks if a specified file exists
FolderExistsChecks if a specified folder exists
GetAbsolutePathNameReturns the complete path from the root of the drive for the specified path
GetBaseNameReturns the base name of a specified file or folder
GetDriveReturns a Drive object corresponding to the drive in a specified path
GetDriveNameReturns the drive name of a specified path
GetExtensionNameReturns the file extension name for the last component in a specified path
GetFileReturns a File object for a specified path
GetFileNameReturns the file name or folder name for the last component in a specified path
GetFolderReturns a Folder object for a specified path
GetParentFolderNameReturns the name of the parent folder of the last component in a specified path
GetSpecialFolderReturns the path to some of the operating system folders
GetTempNameReturns a randomly generated temporary file or folder
MoveFileMoves one or more files from one location to another
MoveFolderMoves one or more folders from one location to another
OpenTextFileOpens a file and returns a TextStream object that can be used to access the file

Common Methods

BuildPath

BuildPath allows you to build a path string from a root path and a folder to navigate to. Neither of the two parameters are required to represent actual files on the file system in order to build the path string.

You can use absolute or relative references when building the path string. This function could be used to create an “explorer” type interface.

<%
Dim fsObj, newPath
Set fsObj = CreateObject("Scripting.FileSystemObject")
newPath = fsObj.BuildPath("path", "append")
Response.Write(newPath)
%>

CopyFile

The CopyFile method copies one or more files from one location to another. Syntax: FileSystemObject.CopyFile source,destination[,overwrite]

<%
Dim fsObj
Set fsObj=Server.CreateObject("Scripting.FileSystemObject")
fsObj.CopyFile "c:\website\folder1\*.htm","c:\website\folder2\"
Set fsObj=nothing
%>

CopyFolder

The CopyFolder method copies one or more folders from one location to another. Syntax: FileSystemObject.CopyFolder source,destination[,overwrite]

<%
Dim fsObj
Set fsObj=Server.CreateObject("Scripting.FileSystemObject")
fsObj.CopyFolder "c:\website\*","c:\website\archive\"
Set fsObj=nothing
%>

CreateFolder

The CreateFolder method creates a new folder on the server. Syntax: FileSystemObject.CreateFolder(name)

<%
Dim fsObj,fObj
Set fsObj=Server.CreateObject("Scripting.FileSystemObject")
Set fObj=fsObj.CreateFolder("c:\website\folder1")
Set fObj=nothing
Set fsObj=nothing
%>

CreateTextFile

The CreateTextFile method creates a new text file and returns a TextStream object that can be used to read from or write to the file. Specify false on the CreateTextFile parameter to not overwrite existing files. This is an optional value. Overwriting an existing file is True by default.

<%
Dim fsObj,folderObj,fileObj
Set fsObj=Server.CreateObject("Scripting.FileSystemObject")
Set folderObj=fsObj.GetFolder("c:\temp")
Set fileObj=folderObj.CreateTextFile("temp.txt",false)
fileObj.WriteLine("Hello World!")
fileObj.Close
Set fileObj=nothing
Set folderObj=nothing
Set fsObj=nothing
%>

Checking for and Deleting Files and Folders

The FileExists and FolderExists methods check for files and folders. The DeleteFile and DeleteFolder methods delete one or more specified folders. An error will occur if you try to delete a file and/or folder that does not exist.

Syntax:
FileSystemObject.DeleteFile(filename[,force])
FileSystemObject.DeleteFolder(foldername[,force])

Set the force value to True to force the deletion of read-only files or folders.  Default is false.

Example:
<%
Dim fsObj
Set fsObj=Server.CreateObject("Scripting.FileSystemObject")
if fsObj.FileExists("c:\temp\test.txt") then
    fsObj.DeleteFile("c:\temp\test.txt")
end if
if fsObj.FolderExists("c:\temp") then
    fsObj.DeleteFolder("c:\temp")
end if
set fsObj=nothing
%>

Opening Text Files

Opening, reading and/or writing to a text file is a very common practice. The OpenTextFile method opens a specified file and returns a TextStream object that can be used to access the file.

Syntax:
FileSystemObject.OpenTextFile(filename,mode,create,format)

In the syntax above, the first parameter, filename is the only required parameter. The other three: mode, create, and format are optional. The filename specifies the name of the file to open. The mode instructs ASP regarding how to open the file. The options are 1, 2, or 8 (Reading, Writing, or Appending to an existing file).

The next parameter defines whether a new file is created if the name specified does not exist. Default is false. The final parameter deals with the format. The default setting is 0 which indicates ASCII. A setting of -1 indicates Unicode and -2 indicates to use the system default.

Syntax:
FileSystemObject.DeleteFile(filename[,force])
FileSystemObject.DeleteFolder(foldername[,force])

Set the force value to True to force the deletion of read-only files or folders. Default is false.

Example:
<%
Dim fsObj,fObj
Set fsObj=Server.CreateObject("Scripting.FileSystemObject")
Set fObj=fsObj.OpenTextFile("c:\temp\test.txt",8,true)
fObj.WriteLine("This text will be added to the end of file")
fObj.Close
Set fObj=Nothing
Set fsObj=Nothing
%>

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top