Name

FileSystemObject.DeleteFile Method

Syntax

                  oFileSysObj
                  .DeleteFile FileSpec [, Force]
oFileSysObj

Use: Required

Data Type: FileSystemObject object

A FileSystemObject object.

FileSpec

Use: Required

Data Type: String

The name and path of the file or files to delete.

Force

Use: Optional

Data Type: Boolean

If set to True, the read-only flag on a file is ignored and the file deleted. Its default value is False; read-only files will not be deleted.

Description

Permanently removes a given file or files.

Rules at a Glance

  • FileSpec can contain wildcard characters as the final path component, which allows multiple files to be deleted.

  • FileSpec can be a relative or absolute path.

  • If any of the files specified for deletion are open, the method fails with a “Permission Denied” error.

  • If the specified file or files can’t be found, the method fails.

  • If only a filename is used in FileSpec, the application’s current drive and directory is assumed.

Programming Tips and Gotchas

  • If FileSpec specifies a path not ending in a path separator, the method will fail without generating an error. If FileSpec specifies a path that ends in a path separator, the method fails and generates runtime error 53, “File not found.”

  • The DeleteFile method differs from the Delete method of the File object in several respects. First, it allows you to delete a file directly, without first obtaining an object reference to it. Second, by supporting wildcards, it allows you to delete multiple files at once.

  • If an error occurs while deleting more ...

Get VBScript in a Nutshell, 2nd Edition 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.