VBScript Delete Method
last modified April 9, 2025
The Delete
method in VBScript is part of the
FileSystemObject
. It permanently removes files or folders from the
file system. This method is powerful and should be used with caution as deleted
items cannot be recovered. It's commonly used in file cleanup and maintenance
scripts.
Delete
can remove both files and folders, with options for
recursive deletion. It permanently erases data without sending to recycle bin.
This tutorial covers Delete
with practical examples to demonstrate
its usage in various scenarios.
Delete Method Overview
The Delete
method takes one required parameter (the path) and one
optional parameter (force deletion). It returns no value but may raise errors if
the operation fails. The method is available through FileSystemObject
in VBScript scripting.
Key features include permanent deletion and recursive folder removal. It doesn't
provide undo functionality or move items to recycle bin. Delete
works with both absolute and relative paths. Understanding this method helps
create effective file management scripts.
Deleting a Single File
This example demonstrates the simplest use of Delete
to remove a
single file. It shows basic file deletion with error handling. The script checks
if the file exists before attempting deletion.
Set fso = CreateObject("Scripting.FileSystemObject") filePath = "C:\Temp\old_report.txt" If fso.FileExists(filePath) Then fso.DeleteFile(filePath) WScript.Echo "File deleted successfully" Else WScript.Echo "File does not exist" End If Set fso = Nothing
The script creates a FileSystemObject
and checks for file existence.
If the file exists, it calls DeleteFile
to remove it. The operation
is permanent and immediate. Always verify file existence to avoid errors.
Deleting a Folder with Contents
This example shows how to delete an entire folder including all its contents.
The DeleteFolder
method with True
parameter enables
recursive deletion. It removes all subfolders and files within the target.
Set fso = CreateObject("Scripting.FileSystemObject") folderPath = "C:\Temp\OldProjects" If fso.FolderExists(folderPath) Then fso.DeleteFolder folderPath, True WScript.Echo "Folder and contents deleted" Else WScript.Echo "Folder does not exist" End If Set fso = Nothing
The script checks folder existence before deletion attempt. The second parameter
True
forces deletion of all contents. This operation is irreversible
and removes everything in the folder hierarchy. Use with extreme caution.
Deleting Multiple Files with Wildcards
VBScript can delete multiple files matching a pattern using wildcards. This
example demonstrates batch deletion of temporary files. The script uses
GetFolder
and Files
collection to find matches.
Set fso = CreateObject("Scripting.FileSystemObject") tempFolder = "C:\Temp" Set folder = fso.GetFolder(tempFolder) For Each file In folder.Files If LCase(fso.GetExtensionName(file.Name)) = "tmp" Then file.Delete WScript.Echo "Deleted: " & file.Name End If Next Set fso = Nothing
The script iterates through all files in the Temp folder. It checks each file's extension and deletes .tmp files. This approach allows selective deletion based on file attributes. Wildcards provide powerful file management capabilities.
Handling Delete Errors
This example demonstrates proper error handling when using the Delete
method. It includes a subroutine to safely attempt deletion. Error handling
prevents script termination on failure.
Set fso = CreateObject("Scripting.FileSystemObject") filePath = "C:\Temp\locked_file.txt" On Error Resume Next fso.DeleteFile filePath If Err.Number <> 0 Then WScript.Echo "Error " & Err.Number & ": " & Err.Description Err.Clear Else WScript.Echo "File deleted successfully" End If On Error GoTo 0 Set fso = Nothing
The script uses On Error Resume Next
to handle potential errors.
If deletion fails, it displays the error message without crashing. This approach
is essential for robust file management scripts. Always clear errors after
handling them.
Deleting Read-Only Files
This example shows how to delete read-only files by first changing their attributes. The script removes the read-only flag before deletion. This technique works around common deletion obstacles.
Set fso = CreateObject("Scripting.FileSystemObject") filePath = "C:\Temp\important.log" If fso.FileExists(filePath) Then Set file = fso.GetFile(filePath) file.Attributes = 0 ' Remove all attributes file.Delete WScript.Echo "Read-only file deleted" Else WScript.Echo "File not found" End If Set fso = Nothing
The script first gets the file object to modify its attributes. Setting attributes to 0 removes all special flags including read-only. After attribute change, the file can be deleted normally. This approach ensures successful deletion of protected files.
Source
FileSystemObject Documentation
In this article, we have explored the Delete
method in VBScript,
covering its usage and practical applications. From single file deletion to
complex folder removal, these examples demonstrate reliable file system
management. With this knowledge, you can create robust cleanup and maintenance
scripts.
Author
List all VBScript tutorials.