Deleting Files in VB Script

Single or multiple files can be deleted using the function "DeleteFile". Following is the syntax of "DeleteFile" function. Please keep in mind, files deleted using this method will not go in to the "Recycle Bin"

DeleteFile "File Name" [,force]

The argument "File Name" is the file name with complete path. This argument can accept wild cards like ? or *. By using these wild cards in file name, we can delete multiple files.

The argument "force" is a boolean argument (True or False). If you give force as True, the file will be deleted even if its a read-only or system file. The default is False, which means, you will get a "Permission Denied Error" while deleting read-only files and system files.

Following is a sample code to delete multiple files

Dim FileName
Dim FilePath
Dim ObjFso 

FileName = "*.txt"
FilePath = "c:\fold-1"

'Create file system object
Set ObjFso = CreateObject("Scripting.FileSystemObject")

'Delete Files
ObjFso.DeleteFile FilePath & "\" & FileName, True
Powered by Bullraider.com