The File object lets you read and write binary data from and
to a file on disk (or in a Zip file, FTP site, etc). While the Microsoft
Scripting.FileSystemObject object lets you read and write files
already, it only supports text, not binary data. You can also use the
File object to modify a file's attributes and
timestamps.
You can obtain a File object using the
FSUtil.OpenFile and
Item.Open methods. You
can open a file in one of three modes:
Property Name |
Return Type |
Description |
---|---|---|
<default value> |
string |
Returns the full pathname of the file. |
error |
int |
Returns a Win32 error code that indicates the success or failure of the
last operation. If the previous operation succeeded this will generally be
0. |
size |
object:FileSize |
Returns a FileSize object representing the size of this file, in bytes. |
tell |
object:FileSize |
Returns a FileSize object representing the current position of the read or write cursor within this file, in bytes. |
Method Name |
Arguments |
Return Type |
Description |
---|---|---|---|
Close |
none |
none |
Closes the underlying file handle. After this call the
File object is still valid but it can no longer read or
write data. |
Read |
<blob:target> |
int or |
Reads data from the file. If you provide a targetBlob as the
first parameter, the data will be stored in that Blob.
Otherwise, a Blob will be created
automatically. |
Seek |
<int:delta> |
object:FileSize |
Moves the read or write cursor within this file. The delta
parameter specifies how many bytes to move - how this is interpreted
depends on the optional method parameter: |
SetAttr |
<string:attributes> |
bool |
Modifies the attributes of this file. Valid attributes
are: |
SetTime |
<date:modify> |
bool |
Modifies one or more of the file's timestamps. The create and
access parameters are optional. If you wish to specify no change
for a timestamp, specify 0. |
SetTimeUTC |
<date:modify> |
bool |
Modifies one or more of the file's timestamps. The create and
access parameters are optional. If you wish to specify no change
for a timestamp, specify 0. |
Truncate |
none |
bool |
Truncates the file at the current position of the write cursor. You can
use this in conjunction with the Seek method to
pre-allocate a file's space on disk, for greater performance (i.e. seek to
the final size of the file, truncate at that point, and then seek
back to the start and write the data). |
Write |
<blob:source> |
int |
Writes data from the specified Blob (or array) to the
file. By default the entire contents of the Blob will be
written - you can use the optional from parameter to specify the
source byte offset, and the size parameter to specify the number
of bytes to write. |