日期:2013-02-05  浏览次数:20848 次

Tutorials & Code Snips: Graphics & Charts: Images\\


This ASP Script recurses through a directory tree and loads images into a DHTML preloader.

First off, big thanks to Brian from Script Asylum for letting me use his DHTML site preloader. This
version will be even less work, because all you do is tell the ASP to drill down through a directory
structure looking for images, and it will place all the image names into an array, and off it goes.
The setup for this is incredibly simple. First off, open Preloader.asp, and change the following variables:
- boolRecurse: Tell the script to drill down through subdirectories within the folder you choose
(True/False)
- strVirtualRoot: The folder that contains all the images

<%
boolRecurse = True ' Recurse through subdirectories? True/False
strVirtualRoot = "../../Images" ' Directory
strRootFolder = Server.MapPath(strVirtualRoot) ' Grab the directory
intSize = 0

Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strRootFolder)

strOutput = TraverseFolder(objFolder, strVirtualRoot, boolRecurse)
strOutput = mid(strOutput, 1, Len(strOutput)-2)

Set objFSO = Nothing
Set objFolder = Nothing

Function TraverseFolder(objFolder, strVirtualRoot, boolRecurse)
strOutput = ""

arrImages = Array("gif", "jpg", "png", "jpeg")

'Only process directories that do NOT start with
'an underscore.
    If Not Left(objFolder.Name, 1) = "_" then
          Dim objFile, strPath, strFileName, strFileSize, strExtension
          
          'Iterate through each file in the folder
          For Each objFile in objFolder.Files
              ' Obtain the extension of the current file
              strPath = objFile.Path
              strFileName = objFile.Name
              intFileSize = objFile.Size
              strExtension = Ucase(Right(strPath, Len(strPath) - InStrRev(strPath, ".")))
              

              ' See if file is an image
              For x = LBound(arrImages) to UBound(arrImages)
                    If strExtension = Ucase(arrImages(x)) then
                        strOutput = strOutput & "'" & strVirtualRoot & "/" & strFileName & "', "
                        intSize = intSize + intFileSize
                    End If
              Next
          Next