Name

File Object

Creatable

No

Returned by

Files.Item property

FileSystemObject.GetFile method

Library

Microsoft Scripting Runtime

Description

The File object represents a disk file that can be a file of any type and allows you to interrogate the properties of the file and to move upward in the filesystem hierarchy to interrogate the system on which the file resides. The process of instantiating a File object—for example, assigning a reference from the File object’s Item property to a local object variable—doesn’t open the file. An open file is represented in the File System object model by a TextStream object, which can be generated by the File object’s OpenAsTextStream method.

There are several methods of retrieving a reference to an existing File object:

  • If you want to work with a particular file, you can retrieve a reference to it directly by calling the GetFile method of the FileSystemObject object. For example:

    Dim oFS, oFile 
    Set oFS = WScript.CreateObject("Scripting.FileSystemObject")
    Set oFile = oFS.GetFile("C:\Documents\MyReport.doc")

    allows you to retrieve a reference to a File object representing the MyReport.doc file without having to use the File System object model to navigate the filesystem.

  • If you want to work with a file as a member of a folder or of a set of files, you can retrieve a reference to a File object that represents it from the Item property of the Files collection. (The Files collection is returned by the Files property of a Folder object.) The following code fragment, ...

Get VBScript in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.