FileSystemObject objects are used to access the file system on the server. This object can handle file, folder and directory paths. It is also possible to use it to retrieve file system information.
The following code creates a text file and writes some text:
<%
dim fs,fname
set fs=Server.CreateObject("Scripting.FileSystemObject")
set fname=fs.CreateTextFile("c:/test.txt",true)
fname.WriteLine("Hello World!")
fname.Close
set fname=nothing
set fs=nothing
%>
The properties and methods of the FileSystemObject object are as follows:
1. Attributes
Drives: Returns a set of all Drive objects on the computer.
grammar:
[drivecoll=]FileSystemObject.Drives
2. Method
Bulidpath: Add a name to an existing path.
CopyFile: Copy one or more files from one place to another.
CopyFolder: Copy one or more folders from one place to another.
CreateFolder: Create a new folder.
CreateTextFile: Creates a text file and returns a TextStream object for reading and writing the created text file.
DeleteFile: Delete one or more specified files.
DeleteFolder: Delete one or more specified folders.
DriveExists: Checks whether the specified drive exists.
FileExists: Checks whether the specified file exists.
FolderExists: Checks whether the specified folder exists.
GetAbsolutePathName: Returns the full path of the specified path.
GetBaseName: Returns the base name of the specified file or folder.
GetDrive: Returns the corresponding Drive object in the drive at the specified path.
GetDriveName: Returns the drive name of the specified path.
GetExtensionName: Returns the file extension of the last part of the specified path.
GetFile: Returns a file object for the specified path.
GetFileName: Returns the last part of the file name or folder name in the specified path.
GetFolder: Returns a folder object on the specified path.
GetParentFolderName: Returns the last part of the parent folder name in the specified path.
GetSpecialFolder: Returns the path to a special folder in Windows.
GetTempName: Returns a randomly generated temporary file or folder.
MoveFile: Move one or more files from one place to another.
MoveFolder: Move one or more files from one place to another.
OpenTextFile: Opens a file and returns a TextStream object for reading and writing the opened file.
BuildPath method
The BuildPath method adds a name to an existing path.
1. Grammar
[newpath=]FileSystemObject.BuildPath(path,name)
Parameter description:
path: required. path.
name: The name to be added.
2. Examples
<%
dim fs,path
set fs=Server.CreateObject("Scripting.FileSystemObject")
path=fs.BuildPath("c:/mydocuments","test")
response.write(path)
set fs=nothing
%>
Output:
c:/mydocuments/test
CopyFile method
The CopyFile method copies one or more files from one place to another.
1. Grammar
FileSystemObject.CopyFile source,destination[,overwrite]
Parameter description:
source: required. The file to be copied.
destination: required. The destination to copy to.
overwrite: optional. Is a Boolean value that indicates whether to overwrite existing files. True means coverage, False means no coverage. Default is True.
2. Examples
<%
dimfs
set fs=Server.CreateObject("Scripting.FileSystemObject")
fs.CopyFile "c:/mydocuments/web/*.htm","c:/webpages/"
set fs=nothing
%>
CopyFolder method
The CopyFolder method copies one or more files from one place to another.
1. Grammar
FileSystemObject.CopyFolder source,destination[,overwrite]
Parameter description:
source: required. The folder to copy.
destination: required. The destination to copy to.
overwrite: optional. Is a Boolean value that indicates whether to overwrite existing files. True means coverage, False means no coverage. Default is True.
2. Examples
<%
'copy all the folders in c:/mydocuments/web
'to the folder c:/webpages
dimfs
set fs=Server.CreateObject("Scripting.FileSystemObject")
fs.CopyFolder "c:/mydocuments/web/*","c:/webpages/"
set fs=nothing
%>
<%
'copy only the folder test from c:/mydocuments/web
'to the folder c:/webpages
dimfs
set fs=Server.CreateObject("Scripting.FileSystemObject")
fs.CopyFolder "c:/mydocuments/web/test","c:/webpages/"
set fs=nothing
%>
CreateFolder method
CreateFolder method creates a new folder.
1. Grammar
FileSystemObject.CreateFolder(name)
Parameter description:
name: required. The name of the folder to create.
2. Examples
<%
dim fs,f
set fs=Server.CreateObject("Scripting.FileSystemObject")
set f=fs.CreateFolder("c:/asp")
set f=nothing
set fs=nothing
%>
CreateTextFile method
The CreateTextFile method creates a new text file in the current folder and returns a TextStream object for reading and writing this new file.
1. Grammar
FileSystemObject.CreateTextFile(filename[,overwrite[,unicode]])
FolderObject.CreateTextFile(filename[,overwrite[,unicode]])
Parameter description:
filename: required. The name of the file to be created.
overwrite: optional. Is a Boolean value indicating whether to overwrite an existing file. True means coverage, False means no coverage. Default is True.
unicode: optional. A Boolean value indicating whether the file being created is a Unicode file or an ASCII file. True is a Unicode file, False is an ASCII file. The default is False.
2. Examples
Example of FileSystemObject:
<%
dim fs,tfile
set fs=Server.CreateObject("Scripting.FileSystemObject")
set tfile=fs.CreateTextFile("c:/somefile.txt")
tfile.WriteLine("Hello World!")
tfile.close
settfile=nothing
set fs=nothing
%>
Example of Folder object:
<%
dim fs,fo,tfile
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set fo=fs.GetFolder("c:/test")
Set tfile=fo.CreateTextFile("test.txt",false)
tfile.WriteLine("Hello World!")
tfile.Close
settfile=nothing
set fo=nothing
set fs=nothing
%>
DeleteFile method
The DeleteFile method deletes one or more specified files.
NOTE: An error will occur if you attempt to delete a file that does not exist.
1. Grammar
FileSystemObject.DeleteFile(filename[,force])
Parameter description:
filename: required. The name of the file to be deleted.
force: optional. A Boolean value indicating whether to delete read-only files. True means yes, False means no. The default is False.
2. Examples
<%
dimfs
Set fs=Server.CreateObject("Scripting.FileSystemObject")
fs.CreateTextFile("c:/test.txt",True)
if fs.FileExists("c:/test.txt") then
fs.DeleteFile("c:/test.txt")
end if
set fs=nothing
%>
DeleteFolder method
DeleteFolder method DeleteFile method deletes one or more specified folders.
NOTE: An error will occur if you attempt to delete a folder that does not exist.
1. Grammar
FileSystemObject.DeleteFolder(foldername[,force])
Parameter description:
foldername: required. The name of the file to be deleted.
force: optional. A Boolean value indicating whether to delete read-only folders. True means yes, False means no. The default is False.
2. Examples
<%
dimfs
set fs=Server.CreateObject("Scripting.FileSystemObject")
if fs.FolderExists("c:/temp") then
fs.DeleteFolder("c:/temp")
end if
set fs=nothing
%>
DriveExists method
The DriveExists method returns a Boolean value indicating whether the specified drive exists. True means existence, False means no.
1. Grammar
FileSystemObject.DriveExists(drive)
Parameter description:
drive: required. A drive letter or a complete path description.
2. Examples
<%
dimfs
set fs=Server.CreateObject("Scripting.FileSystemObject")
if fs.DriveExists("c:")=true then
response.write("Drive c: exists!")
else
response.write("Drive c: does not exist.")
endIf
set fs=nothing
%>
FileExists method
The FileExists method returns a Boolean value indicating whether the specified file exists. True means existence, False means no.
1. Grammar
FileSystemObject.FileExists(filename)
Parameter description:
filename: required. The name of the file to be checked.
2. Examples
<%
dimfs
set fs=Server.CreateObject("Scripting.FileSystemObject")
if fs.FileExists("c:/asp/introduction.asp")=true then
response.write("File c:/asp/introduction.asp exists!")
else
response.write("File c:/asp/introduction.asp does not exist!")
end if
set fs=nothing
%>
FolderExists method
The FolderExists method returns a Boolean value indicating whether the specified folder exists. True means existence, False means no.
1. Grammar
FileSystemObject.FolderExists(foldername)
Parameter description:
foldername: required. The name of the folder to check.
2. Examples
<%
dimfs
set fs=Server.CreateObject("Scripting.FileSystemObject")
if fs.FolderExists("c:/asp")=true then
response.write("Folder c:/asp exists!")
else
response.write("Folder c:/asp does not exist!")
end if
set fs=nothing
%>
GetAbsolutePathName method
The GetAbsolutePathName method returns the complete path to the specified path (converts the specified path to an absolute path).
1. Grammar
FileSystemObject.GetAbsolutePathName(path)
Parameter description:
path: required. The path to convert to an absolute path.
2. Examples
Assume the current directory is c:/temp/test:
Example 1
<%
dim fs,path
set fs=Server.CreateObject("Scripting.FileSystemObject")
path=fs.GetAbsolutePathName("c:")
response.write(path)
%>
Output:
c:/temp/test
Example 2
<%
dim fs,path
set fs=Server.CreateObject("Scripting.FileSystemObject")
path=fs.GetAbsolutePathName("mydoc.txt")
response.write(path)
%>
Output:
c:/temp/test/mydoc.txt
Example 3
<%
dim fs,path
set fs=Server.CreateObject("Scripting.FileSystemObject")
path=fs.GetAbsolutePathName("private/mydoc.txt")
response.write(path)
%>
Output:
c:/temp/test/private/mydoc.txt
GetBaseName method
The GetBaseName method returns the base name of the file or folder in the specified path.
1. Grammar
FileSystemObject.GetBaseName(path)
Parameter description:
path: required. The path to the file or folder.
2. Examples
<%
dimfs
set fs=Server.CreateObject("Scripting.FileSystemObject")
Response.Write(fs.GetBaseName("c:/winnt/cursors/3dgarro.cur"))
set fs=nothing
%>
Output:
3dgarro
GetDrive method
The GetDrive method returns a Drive object specified by the drivespec parameter.
1. Grammar
FileSystemObject.GetDrive(drivespec)
Parameter description:
drivespec: required. This can be a drive letter ©, or a drive letter followed by a colon (c:), or a drive letter followed by a colon and a path separator (c:/), or a network share description (//computer2/share1).
2. Examples
<%
dim fs,d
set fs=Server.CreateObject("Scripting.FileSystemObject")
set d=fs.GetDrive("c:/")
set fs=nothing
%>
GetDriveName method
The GetDriveName method returns a string containing the name of the drive at the specified path.
1. Grammar
FileSystemObject.GetDriveName(path)
Parameter description:
path: required. The specified path.
2. Examples
<%
dim fs,dname
set fs=Server.CreateObject("Scripting.FileSystemObject")
dname=fs.GetDriveName("c:/test/test.htm")
Response.Write(dname)
set fs=nothing
%>
Output:
c:
GetExtensionName method
The GetExtensionName method returns a string containing the file extension of the last portion of the file in the specified path.
1. Grammar
FileSystemObject.GetExtensionName(path)
Parameter description:
path: required. The specified path.
2. Examples
<%
dimfs
set fs=Server.CreateObject("Scripting.FileSystemObject")
Response.Write(fs.GetExtensionName("c:/test/test.htm"))
set fs=nothing
%>
Output:
htm
GetFile method
The GetFile method returns a File object for the specified path.
1. Grammar
FileSystemObject.GetFile(path)
Parameter description:
path: required. About the path to a specific file.
2. Examples
<%
dim fs,f
set fs=Server.CreateObject("Scripting.FileSystemObject")
set f=fs.GetFile("c:/test/test.htm")
Response.Write("The file was last modified on: ")
Response.Write(f.DateLastModified)
set f=nothing
set fs=nothing
%>
Output:
The file was last modified on 01/01/20 4:23:56 AM
GetFileName method
The GetFileName method returns a string containing the last part of the file or folder name in the specified path.
1. Grammar
FileSystemObject.GetFileName(path)
Parameter description:
path: required. About the path to a specific file or folder.
2. Examples
<%
dim fs,p
set fs=Server.CreateObject("Scripting.FileSystemObject")
p=fs.getfilename("c:/test/test.htm")
response.write(p)
set fs=nothing
%>
Output:
test.htm
<%
dim fs,p
set fs=Server.CreateObject("Scripting.FileSystemObject")
p=fs.getfilename("c:/test/")
response.write(p)
set fs=nothing
%>
Output:
test
GetFolder method
The GetFolder method returns a Folder object for the specified path.
1. Grammar
FileSystemObject.GetFolder(path)
Parameter description:
path: required. The path to a specific folder.
2. Examples
<%
dim fs,f
set fs=Server.CreateObject("Scripting.FileSystemObject")
set f=fs.GetFolder("c:/test/")
Response.Write("The folder was last modified on: ")
Response.Write(f.DateLastModified)
set f=nothing
set fs=nothing
%>
Output:
The folder was last modified on 01/01/20 4:23:56 AM
GetParentFolderName method
The GetParentFolderName method returns the name of the last part of the parent folder in the specified path.
1. Grammar
FileSystemObject.GetParentFolderName(path)
Parameter description:
path: required. The path of the file or folder whose parent folder name is to be returned.
2. Examples
<%
dim fs,p
set fs=Server.CreateObject("Scripting.FileSystemObject")
p=fs.GetParentFolderName("c:/winnt/cursors/3dgarro.cur")
Response.Write(p)
set fs=nothing
%>
Output:
c:/winnt/cursors
GetSpecialFolder method
The GetSpecialFolder method returns the path to a specific Windows folder.
1. Grammar
FileSystemObject.GetSpecialFolder(foldername)
Parameter description:
foldername: required.
foldername value description:
0=WindowsFolder (contains files installed by the windows operating system);
1=SystemFolder (contains libraries, fonts, and device drivers)
2=TemporaryFolder (used to store temporary files)
2. Examples
<%
dim fs,p
set fs=Server.CreateObject("Scripting.FileSystemObject")
set p=fs.GetSpecialFolder(1)
Response.Write(p)
set p=nothing
set fs=nothing
%>
Output:
C:/WINNT/system32
GetTempName method
The GetTempName method returns a randomly generated temporary file or folder.
1. Grammar
FileSystemObject.GetTempName
2. Examples
<%
dim fs,tfolder,tname, tfile
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set tfolder=fs.GetSpecialFolder(2)
tname=fs.GetTempName
Set tfile=tfolder.CreateTextFile(tname)
Response.write(tfile)
%>
Output:
trb2007.tmp
MoveFile method
The MoveFile method moves one or more files from one place to another.
1. Grammar
FileSystemObject.MoveFile source,destination
Parameter description:
source: required. The path to the file to be moved.
destination: required. The location to move to.
2. Examples
<%
dimfs
set fs=Server.CreateObject("Scripting.FileSystemObject")
fs.MoveFile "c:/web/*.gif","c:/images/"
set fs=nothing
%>
MoveFolder method
The MoveFolder method moves one or more folders from one place to another.
1. Grammar
FileSystemObject.MoveFolder source,destination
Parameter description:
source: required. The path of the folder to be moved.
destination: required. The location to move to.
2. Examples
<%
dimfs
set fs=Server.CreateObject("Scripting.FileSystemObject")
fs.MoveFolder "c:/test/web/","c:/windows/"
set fs=nothing
%>
OpenTextFile method
The OpenTextFile method opens a specified file and returns a TextStream object for accessing the file.
1. Grammar
FileSystemObject.OpenTextFile(fname,mode,create,format)
Parameter description:
fname: required. The name of the file to open.
mode: optional. How to open it. 1=ForReading (open for reading only), 2=ForWriting (open for writing), 8=ForAppending (open for appending, written content will be added to the end of the file).
create: optional. Sets whether to create the opened file if it does not exist. True means yes, False means no. The default is False.
format: optional. The format of the file. 0=TristateFalse (open in ASCII format, which is the default), -1=TristateTrue (open in Unicode format), -2=TristateUseDefault (open in the system default mode)
2. Examples
<%
dim fs,f
set fs=Server.CreateObject("Scripting.FileSystemObject")
set f=fs.OpenTextFile(Server.MapPath("testread.txt"),8,true)
f.WriteLine("This text will be added to the end of file")
f.Close
set f=Nothing
set fs=Nothing
%>
Property
PropertyDescription
describe
Attributes
Sets or returns the attributes of a specified file
Sets or returns the properties of the specified file
DateCreated
Returns the date and time when a specified file was created
Returns the date and time the specified file was created
DateLastAccessed
Returns the date and time when a specified file was last accessed
Returns the date and time the specified file was last accessed
DateLastModified
Returns the date and time when a specified file was last modified
Returns the date and time the specified file was last modified
Drive
Returns the drive letter of the drive where a specified file or folder resides
Returns the drive letter of the drive letter where the specified file or folder is located
Name
Sets or returns the name of a specified file
Sets or returns the name of the specified file
ParentFolder
Returns the folder object for the parent of the specified file
Returns the parent folder of the specified file
Path
Returns the path for a specified file
Returns the path to a specified file
ShortName
Returns the short name of a specified file (the 8.3 naming convention)
Returns the short name of a specified file (according to 8.3 naming rules)
ShortPath
Returns the short path of a specified file (the 8.3 naming convention)
Returns the short path of a specified file (according to 8.3 naming rules)
Size
Returns the size, in bytes, of a specified file
Returns the number of bytes contained in the specified file
Type
Returns the type of a specified file
Returns the type of the specified file
Methods
method
Method
MethodDescription
describe
Copy
Copies a specified file from one location to another
Copy files on the local machine to a remote machine
DeleteDeletes a specified file
Delete specified file
Move
Moves a specified file from one location to another
Move files on the local machine to a remote machine
OpenAsTextStream
Opens a specified file and returns a TextStream object to access the file
Open the specified file and return a TextStream object
This article is introduced here. It is recommended to continue to read the relevant articles below to learn more.