Name
FileSystemObject.DeleteFile Method
Syntax
oFileSysObj.DeleteFileFileSpec[,Force]
oFileSysObjUse: Required
Data Type: FileSystemObject object
A FileSystemObject object.
FileSpecUse: Required
Data Type: String
The name and path of the file or files to delete.
ForceUse: Optional
Data Type: Boolean
If set to
True, the read-only flag on a file is ignored and the file deleted. Its default value isFalse; read-only files will not be deleted.
Description
Permanently removes a given file or files.
Rules at a Glance
FileSpeccan contain wildcard characters as the final path component, which allows multiple files to be deleted.FileSpeccan 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
FileSpecspecifies a path not ending in a path separator, the method will fail without generating an error. IfFileSpecspecifies 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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access