Name

FileSystemObject.GetFile Method

Syntax

                  oFileSysObj
                  .GetFile(FilePath)
oFileSysObj

Use: Required

Data Subtype: FileSystemObject object

Any object variable returning a FileSystemObject object.

FilePath

Use: Required

Data Subtype: String

A path and filename.

Return Value

File object.

Description

Returns a reference to a File object.

Rules at a Glance

  • FilePath can be an absolute or a relative path.

  • If FilePath is a share name or network path, GetFile ensures that the drive or share exists as part of the process of creating the File object.

  • If any part of the path in FilePath can’t be contacted or doesn’t exist, an error occurs.

Programming Tips & Gotchas

  • The object returned by GetFile is a File object, not a TextStream object. A File object isn’t an open file; the point of the File object is to perform methods such as copying or moving files and interrogating a file’s properties. Although you can’t write to or read from a File object, you can use the File object’s OpenAsTextStream method to obtain a TextStream object. You can also save yourself a step and directly open a TextStream object’s OpenTextFile method.

  • You should first use GetAbsolutePathName to create the required FilePath string.

  • If FilePath includes a network drive or share, you could use the DriveExists method to confirm that the required drive is available prior to calling the GetFile method.

  • Since GetFile generates an error if the file designated in FilePath doesn’t exist, you should call the FileExists method before calling GetFile. ...

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.