ObjFSO.DeleteFile with Wildcards (Visual Basic)

Send Us a Sign! (Contact Us!)
Word PDF XPS Text

Tipically, you will can use [gs wildcard]s, in a VBScript, using the following notation:

set objFSO = CreateObject("Scripting.FileSystemObject")

objFSO.DeleteFile(RootFolder & "*.txt")

Where RootFolder will be the full path (computed or [gs hardcode]d) in which reside all the text files we want to delete.

Advanced Scenario

Trying to build a [gs script] to go through some nested directories and delete any file with a specific extension.

Example paths are:

[tweet]

{root_path}\Folder1\{terminal_path}\somefile.fmi
{root_path}\Folder2\{terminal_path}\someotherfile.fmi

For about 25 folders.

Solution

This solution involves a directory structure of the following type:

{root_path}\ {VARIABLE_PATHS} \{terminal_path}\{wildcard_files}

So, we'll have to define an array that we'll contain all the {VARIABLE_PATHS}, so the subroutine EmptyFolders will delete any *.txt files founded during its recursive process, using the .DeleteFile [gs method] built on a simple string contatenation.

Option Explicit

Dim RootFolder,Folderpath
Dim i
Dim aFolderList()
RootFolder = "{root_path}\"
Folderpath = "\{terminal_path}\"

FillFolderList
EmptyFolders

on error resume next
Sub FillFolderList
ReDim aFolderList(3)
aFolderList(0) = "Folder1"
aFolderList(1) = "Folder2"
aFolderList(2) = "Folder3"
end sub
sub EmptyFolders()
On Error Resume Next
Dim objFSO
set objFSO = CreateObject("Scripting.FileSystemObject")
For i = 0 to UBOUND(aFolderList) - 1
objFSO.DeleteFile(RootFolder & aFolderList(i) & Folderpath & "*.txt")
next
end sub

SOURCE

LINK

LANGUAGE
ENGLISH

1 thought on “ObjFSO.DeleteFile with Wildcards (Visual Basic)”

Comments are closed.