Microsoft VBScript runtime error: Subscript out of range: 'uBound'

The following VBScript is to delete files & folders older than the specified age (60 days).

1. it fails to delete empty subfolders
2. I receive the following when rerunning script to attempt purging subfolders
filename.vbs(102, 2) Microsoft VBScript runtime error: Subscript out of range: 'uBound'
dim sOutputFileName,ts3, arrFiles() ,sTabs
dim iCt, iDelCt
 
sTabs = vbtab & vbtab
iCt=0
Set w = WScript.CreateObject("WScript.Shell")
 
' This code builds an output file.
call makeOutputFile
 
' This code builds an array of files.
call DeleteOldFiles("C:\Program Files\Merge eFilm\eFilm\CD")
 
' This code deletes old files.
' You need to set the parameter to True for file deletion or False
' to determine how much space will be freed if files are deleted.
call DeleteFilesFromArray(True)
 
' This code cleans up the directories.
ts3.Close
set ts3 = nothing
 
sub makeOutputFile
 ' This code creates an output file.
 dim ts2, fso
 
 Set fso = CreateObject("Scripting.FileSystemObject")
 
 ' This code creates a new filename.
sOutputFileName = IncrementFileName("c:\EfilmCDFilesLog\EfilmCDDeletedFiles" &_
 replace(FormatDateTime(date,mmddyy),"/","") & ".csv")
 
 Set ts2 = fso.CreateTextFile(sOutputFileName, True, False)
 ts2.close
 
 Set ts3 = fso.OpenTextFile(sOutputFileName, 2,0)
 ' This code sets up the first row.
 ts3.Writeline (chr(34) & "FILENAME" & chr(34) & "," &_
 		chr(34) & "DATEMODIFIED" & chr(34) & "," &_
 		chr(34) & "SIZE" & chr(34) & "," &_
 		chr(34) & "STATUS" & chr(34))
End sub
 
function DeleteOldFiles (folderName)
 
	dim fso, oMainFolder, oFolder, oSubFolders, oFiles
 
	' This code initializes the objects.
	Set fso = CreateObject("Scripting.FileSystemObject")
	Set oMainFolder = fso.Getfolder(folderName)
	Set oSubFolders = oMainFolder.SubFolders
 
	' This code loops through the subfolder collection and builds an array of files to delete.
	For Each oFile in oMainFolder.files
		if DeleteOld(oFile.DateLastModified) = True then
			' You need to insert the delete code here.
			iCt = iCt+1
			ReDim Preserve arrFiles(3, iCt)
			arrFiles(1,iCt) = oFile.path
			arrFiles(2,iCt) = oFile.DateLastModified
			arrFiles(3,iCt) = oFile.size
 
			' This code outputs to the screen.
			'wscript.echo oFile.path & sTabs & oFile.DateLastModified & sTabs & "---->>> DELETED <<<----"
 
			' This code tests the output to file.
	 		 ts3.Writeline (chr(34) & oFile.path & chr(34) & "," &_
	 		 		chr(34) & oFile.DateLastModified & chr(34) & "," &_
	 		 		chr(34) & oFile.Size & chr(34) & "," &_
	 		 		"DELETED")
 
		else
			' This code outputs to the screen.
			'wscript.echo oFile.path & sTabs & oFile.DateLastModified & sTabs & "---->>> Not Deleted. <<<----"
			' This code tests the output to file.
			 ts3.Writeline (chr(34) & oFile.path & chr(34) & "," &_
			 		chr(34) & oFile.DateLastModified & chr(34) & "," &_
			 		chr(34) & oFile.Size & chr(34) & "," &_
			 		"NOT DELETED")
		end if
	Next
 
	' This code recurses through each subdirectory.
	For Each oSubFolders In oMainFolder.SubFolders
		Call DeleteOldFiles(oSubFolders)
 	Next
 
end function
 
sub DeleteFilesFromArray(bolDelete)
	' This code performs the deletions that have looped through the collection.
	
	dim lngMegs
 
	dim fso
 
	' This code initializes the objects.
	Set fso = CreateObject("Scripting.FileSystemObject")
 
	lngMegs = 0
	
	For iDelCt = 1 to uBound(arrFiles,2)
		if bolDelete then
			' This code deletes the folder.
			fso.DeleteFile arrFiles(1,iDelCt),True
			
			' This code keeps a running total of the disk space recovered.
			lngMegs = lngMegs + arrFiles(3,iDelCt)
			
			' This code outputs the result of files deleted from the array.
			wscript.echo arrFiles(1,iDelCt) & sTabs &_
				 arrFiles(2,iDelCt) & sTabs &_
				 arrFiles(3,iDelCt) & sTabs & "---->>> Deleted.<<<----"
			
		else
			' This code outputs fake results to preview the files that 
			' will be deleted and show how much space can be recovered.
			wscript.echo arrFiles(1,iDelCt) & sTabs &_
				 arrFiles(2,iDelCt) & sTabs &_
				 arrFiles(3,iDelCt) & sTabs & "---->>> Deleted.<<<----"
			
			'This code keeps a running total of the disk space recovered.
			lngMegs = lngMegs + arrFiles(3,iDelCt)
		end if
	Next
 
	wscript.echo vbcrlf & uBound(arrFiles,2) & " Files Deleted. Have a Nice Day." & vbcrlf & vbcrlf &_
	"Space Recovered: " & formatnumber(lngMegs/1000000,2) &_ 
"MB" & vbcrlf &_
			 "Log File Located Here -----> " & sOutputFileName
End sub
 
function DeleteOld(sDate)
 
' This code returns a function of True if the folder is more than 60 days old.
	if sDate < (now -60) then
		DeleteOld = True
	else
		DeleteOld = False
	end if
end function
 
Function IncrementFileName(mFile)
 
 dim i
 dim fso
 
 Set fso = CreateObject("Scripting.FileSystemObject")
 
 For i = 1 To 999
 
 IncrementFileName = fso.GetParentFolderName(mFile) & "\" & fso.GetBaseName(mFile) _
 & "_" & FormatNumber(i, "000") & "." & fso.GetExtensionName(mFile)
 
 If Not fso.FileExists(IncrementFileName) Then Exit For
 
 Next
End Function

Open in new window

rwickershamAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sirbountyCommented:
Typically that would mean it's not an array...
Try:
sub DeleteFilesFromArray(bolDelete)
        ' This code performs the deletions that have looped through the collection.
        
        dim lngMegs
 
        dim fso
 
        ' This code initializes the objects.
        Set fso = CreateObject("Scripting.FileSystemObject")
 
        lngMegs = 0
        If arrFiles = "" Then Exit Sub 'added this line to prevent processing when array does not exist
        For iDelCt = 1 to uBound(arrFiles,2)

Open in new window

0
Krys_KCommented:
Hi

You can test the variable arrFiles to see if it is an Array
So

If Not IsArray(arrFiles) Then
' Do something here if its not an Array
' Like Quit
WScript.Quit
End If

Hope this helps

Krystian
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.