• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3407
  • Last Modified:

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

0
rwickersham
Asked:
rwickersham
1 Solution
 
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now