

ASP ADVANCED
This section covers how to
work with files--how to COPY, move, and delete files; how to detect
whether a file exists; and how to retrieve the attributes of a file.
Copying, Moving, and
Deleting Files
There's more than one way
to copy, move, or delete a file. To
do this, you can use the methods of the FileSystemObject object
or the methods of the File object. The
methods of the FileSystemObject object are slightly more flexible because
you're not restricted to working with a single file.
The
following list describes the methods of the FileSystemObject for
manipulating files:
- CopyFile
source, destination, [overwrite].
This method copies a file from one location to another.
You can use wildcards in the source parameter to copy more than
one file at a time. The
optional overwrite parameter indicates whether to overwrite an
existing file. It can
have the value TRUE or the value FALSE.
- MoveFile
source, destination. This
method moves a file from one location to another.
You can use wildcards in the source parameter to move more than
one file at a time. If
the file already exists at the destination, an error is generated.
- DeleteFile
FileSpecifier. This
method deletes the specified file.
You can use wildcards to delete more than one file at a time.
If
you use wildcards and no matches are made, an error is
generated.
Before you can use any of
these methods, however, you first need to create an instance of the
FileSystemObject object. The
next
example shows how each of the methods is Used:
<%
' Create an instance of the
FileSystemObject object
Set MyFileObject=Server.CreateObject("Scripting.FileSystemObject")
' Create a file to manipulate
Set MyFile=MyFileObject.CreateTextFile("c:\test.txt")
MyFile.Writeline("Hello")
MyFile.Close
' Copy the file
MyFileObject.CopyFile "c:\test.txt","c:\test2.txt"
' Move the file
MyFileObject.MoveFile "c:\test2.txt","c:\test3.txt"
' Delete both files
MyFileObject.DeleteFile "c:\test.txt"
MyFileObject.DeleteFile "c:\test3.txt"
%>
Instead of using the
FileSystemObject object to copy, move, or delete files, you can also use
the File object. These are
the equivalent methods you can use with the File object:
- Copy newcopy,
[Overwrite]. This
method creates a new copy of the current file.
If the
optional overwrite parameter is set to TRUE, any preexisting file is
overwritten.
- Move newcopy.
This method moves the current file.
The current file will now refer to this file.
- Delete.
Deletes the current file.
Before you can use
these methods, you must first create an instance of the File object.
To create an instance of the File object,
you can use the GetFile( ) method of the FileSystemObject object.
Here's the preceding script, rewritten to use the methods of the
File object:
<%
' Create an instance of the
FileSystemObject object
Set MyFileObject=Server.CreateObject("Scripting.FileSystemObject")
' Create a file to manipulate
Set MyFile=MyFileObject.CreateTextFile("c:\test.txt")
MyFile.Writeline("Hello")
MyFile.Close
' Create an instance of the File object.
Set afile=MyFileObject.GetFile("c:\test.txt")
' Copy the file
afile.Copy "c:\test2.txt"
' Move the file
afile.Move "c:\test3.txt"
' Delete the original file
afile.Delete
%>
Detecting Whether a
File Exists
To detect whether or not a
particular file exists, you can use the FileExists( ) method of the
FileSystemObject object. Simply
pass the physical path of a file to this method and it will return either
TRUE or FALSE. Here's an
example of how this method is used:
<HTML>
<HEAD><TITLE> FileExists Example </TITLE></HEAD>
<BODY>
<%
MySelf=Request.ServerVariables("PATH_TRANSLATED")
' Create an instance of the FileSystemObject object
Set MyFileObject=Server.CreateObject("Scripting.FileSystemObject")
If MyFileObject.FileExists(Myself) THEN
Response.Write("l exist!")
ELSE
Response.Write("I do not exist.")
END IF
%>
</BODY>
</HTML>
This Active Server Page
checks whether or not it exists. The
server variable PATH_TRANSLATED is used to return the physical path of the
current file. The FileExists
method checks whether this file exists.
In this example, necessarily, the method must always return TRUE.
(In other words, A checks to see whether A itself exists, and of course it
does, so it comes back TRUE.)
Retrieving the
Attributes of Files
The File object includes a
number of properties that are useful when working with files.
The following list explains these properties:
- Attributes.
This property returns the attributes of the current file
(like the DOS ATTRIB command). For
example, you can use this property to determine whether a file is
hidden or read-only.
- Datecreated.
This property returns the date and time the file was
created.
- DateLastAccessed.
This property returns the date and time the file was last
accessed.
- DateLastModified.
This property returns the date and time the file was last
modified.
- Drive.
This property returns the drive where the file is located.
- Name.
This property returns the name of the file.
- ParentFolder.
This property returns the folder in which the file is
contained.
- Path.
This property returns the path of the file.
- Size.
This property returns the size of the file in bytes.
- Type.
This property returns the type of the file--for example,
Text Document, ASP File, or Internet Document (HTML).
To use any of these
properties, you must first create an instance of the File object.
The next example displays all the properties
for a file with the path c:\test.txt:
<HTML>
<HEAD><TITLE>File Properties</TITLE></HEAD>
<BODY>
<%
' Create an instance of the FileSystemObject object
Set MyFileObject=Server.CreateObject("Scripting.FileSystemObject")
' Create an instance of the File object.
Set afile=MyFileObject.GetFile("C:\test.txt")
%>
<BR>Name:
<%=afile.Name%>
<BR>Path: <%=afile.Path%>
<BR>Drive: <%=afile.Drive%>
<BR>Size: <%=afile.Size%>
<BR>Type: <%=afile.Type%>
<BR>Attributes: <%=afile.Attributes%>
<BR>Date Created: <%=afile.DateCreated %>
<BR>Date Last Accessed: <%=afile.DateLastAocessed%>
<BR>Date Last Modified: <%=afile.DateLastModified%>
</BODY>
</HTML>
The Attributes
property requires some explanation. This
property returns a number corresponding to the
sum of the file attributes that have been set.
The following table lists the file
attribute
values.
Attribute
Value
Normal
0
Read-only
1
Hidden
2
System
4
Volume
8
Directory
16
Archive
32
Alim
64
Compressed
128
The
file has its hidden and archive attributes set.
The combination of the Values 2 (for hidden) and 32 (for archive)
equals 34. There's no danger
of ambiguity, because every combination of attribute values yields a
unique number.
Some
of these properties not only can be read, but also can be set.
You can set the read-only, hidden, system, and archive properties.
For example, to make the file c:\test.txt hidden, you could use
the following script:
<%
' Create an instance of the FileSystemObject object
Set MyFileObject=Server.CreateObject("Scripting.FileSystemObject")
' Create an instance of the File object.
Set afile=MyFileObject.GetFile("c:\test.txt")
' Make it hidden
afile.attributes=2
%>
|