Click here to Skip to main content
15,888,065 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have one folder like 'exportreport' having 60 to 70 different folders inside.
Now i want to delete files from all subfolders using vbscript, I have tried this:

VB
Const DeleteReadOnly = TRUE

Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFile("D:\exportreport\*.xls"), DeleteReadOnly
Posted
Updated 5-Jan-16 1:26am
v2

1 solution

You need to do it recursively, take a look (it is just an example):
VB
sub RecursiveDeleteByExtension(byval strDirectory,strExtensionsToDelete)
	DIM objFolder, objSubFolder, objFile
	DIM strExt

	set objFolder = objFSO.GetFolder(strDirectory)
	for each objFile in objFolder.Files
		for each strExt in SPLIT(UCASE(strExtensionsToDelete),",")
			if RIGHT(UCASE(objFile.Path),LEN(strExt)+1) = "." & strExt then
				wscript.echo "Deleting:" & objFile.Path
				objFile.Delete
				exit for
			end if
		next
	next	
	for each objSubFolder in objFolder.SubFolders
		RecursiveDeleteByExtension objSubFolder.Path,strExtensionsToDelete
	next
end sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900