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. For example, if you try to open a non-existing file for reading using FSUtil.OpenFile, a valid File object will be returned - but the file itself would not be open. You can check if error returns 0 before proceeding to use the File object. |
path |
object:Path |
Returns the full pathname of the file as a Path object. |
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. If you want to use the SetAttr method to modify the attributes of a file you have created, you may want to call Close first otherwise the file system will set the A (archive) attribute on the file whether you want it set or not. You may also want to close a file manually if you want to delete it, as some scripting languages (e.g. JScript) have lazy garbage collection and otherwise may keep the file handle open much longer than you intend. |
Read |
<blob:target> |
int or |
Reads data from the file. If you provide a target Blob 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: b - move relative to the beginning of the file The return value is a FileSize object indicating the new cursor position. |
SetAttr |
object:FileAttr |
bool |
Modifies the attributes of this file. You can either pass a string indicating the attributes to set, or a FileAttr object. When using a string, valid attributes are: a - archive Note that both c and e attributes cannot be set at the same time. When you pass a string you can also use + and - to turn some attributes on or off without affecting others. For example, SetAttr("-r") would turn off the read-only attribute. The return value is True if the operation was successful. |
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. Timestamps are specified as local time - use SetTimeUTC to specify them as UTC. The return value is True for success. |
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. Timestamps are specified as UTC time - use SetTime to specify them as local time. The return value is True for success. |
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). The return value is True for success. |
Write |
<blob:source> or <string:source> |
int |
Writes data from the specified Blob (or array) or string to the file. |