faqts : Computers : Programming : Languages : Asp : ASP/VBScript : Common Problems : Arrays

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

13 of 16 people (81%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

Problem loading FileSystemObject files into an array

Jul 11th, 2000 20:44
unknown unknown, Mike Citro, heath


Problem:
I am trying to use the FileSystemObject to get a listing of files from a 
folder then load those files into an array. I can grab the files fine, 
but every time I try to load them into an array it loops through too 
many times.
I want the array index to increase ONCE for every file but instead it 
loops through and assigns all files in the folder to one array index 
number, then loops through again and assigns ALL the files to the next 
index number.
Solution:
******************************
'Get path info
Dim strPathInfo, strPhysicalPath
strPathInfo = Request.ServerVariables("PATH_INFO")
strPhysicalPath = Server.MapPath(strPathInfo)
Dim objFSO, objFile, objFileItem, objFolder, objFolderContents
'Create FileSystemObject instance
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strPhysicalPath)
Set objFolder = objFile.ParentFolder
Set objFolderContents = objFolder.Files
Dim arrImages()
Dim intIndex
Dim strNumber
For Each objFileItem in objFolderContents  ' gets number of files in 
directory
         strNumber = strNumber + 1
Next
strNumber = strNumber - 1               'sets variable to one less than 
total number of files
'Response.Write(strNumber & "<BR>")     'write it out for debugging 
purposes
ReDim arrImages(strNumber)              'redimensions arrImages to
  intIndex = -1                          'counter used in loop
For Each objFileItem in objFolderContents       'loop thru assign file 
to 
array index
          intIndex = intIndex + 1                        'increase 
counter
         ' Response.Write(intIndex & "<BR>")
          arrImages(intIndex) = objFileItem.Name
          Response.Write("Array Index #:" & intIndex & "File Name: " & 
objFileItem.Name &"<BR>")
Next
%>
end code
*************************************
And here is the Response.Write output:
**************
That produces the following output:
Array Index #:0File Name: 128K.jpg
Array Index #:1File Name: 128K2.jpg
Array Index #:2File Name: 28K.jpg
Array Index #:3File Name: 28K2.jpg
Array Index #:4File Name: 56K.jpg
Array Index #:5File Name: 56K2.jpg
etc...
*****************



© 1999-2004 Synop Pty Ltd