In ASP, the FileSystem Object 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 FileSystem Object. In the following example, we will create and destroy a filesystem object.
<%
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
Property | Description |
Drives | Returns a collection of all Drive objects on the server |
Methods
Method | Description |
BuildPath | Appends a name to an existing path |
CopyFile | Copies one or more files from one location to another |
CopyFolder | Copies one or more folders from one location to another |
CreateFolder | Creates a new folder |
CreateTextFile | Create a text file or write to a file |
DeleteFile | Deletes one or more specified files |
DeleteFolder | Deletes one or more specified folders |
DriveExists | Checks if a specified drive exists |
FileExists | Checks if a specified file exists |
FolderExists | Checks if a specified folder exists |
GetAbsolutePathName | Returns the complete path from the root of the drive for the specified path |
GetBaseName | Returns the base name of a specified file or folder |
GetDrive | Returns a Drive object corresponding to the drive in a specified path |
GetDriveName | Returns the drive name of a specified path |
GetExtensionName | Returns the file extension name for the last component in a specified path |
GetFile | Returns a File object for a specified path |
GetFileName | Returns the file name or folder name for the last component in a specified path |
GetFolder | Returns a Folder object for a specified path |
GetParentFolderName | Returns the name of the parent folder of the last component in a specified path |
GetSpecialFolder | Returns the path to some of the operating system folders |
GetTempName | Returns a randomly generated temporary file or folder |
MoveFile | Moves one or more files from one location to another |
MoveFolder | Moves one or more folders from one location to another |
OpenTextFile | Opens 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
%>
Get File, Folder, and Drive Details
In the next few articles, we will cover the details regarding file, folder, and drive information.
Did you find the page informational and useful? Share it using one of your favorite social sites.
Recommended Books & Training Resources