May 19, 2009

List Images and Other Files Types in a Directory Using ASP

This is a nice and simple ASP+VBScript code snippet that lists the files present in the specified directory (folder). The ready-to-go example below displays the list of images. However, the code can display files depending on filename extension. The script also returns additional information about the files such as type, size and date modified.

Note that this example is non-recursive. This means the specified folder is scanned for files but its sub-folders are not.

The GetFiles() Function

<%
function GetFiles(pFolder, pPattern)

    dim Filelist
    set Filelist = Server.CreateObject("SCRIPTING.DICTIONARY")

    dim Regex
    set Regex = new RegExp
        Regex.IgnoreCase = true
        Regex.Pattern = pPattern

    dim Fso
    set Fso = Server.CreateObject("SCRIPTING.FILESYSTEMOBJECT")

    dim Folder
    set Folder = Fso.GetFolder(Server.MapPath(pFolder))

    dim File
    for each File in Folder.Files
        if Regex.Test(File.Name) then
            Filelist.add File.Name, Array(File.Type, File.Size, File.DateLastModified)
        end if
    next

    set File = nothing
    set Folder = nothing
    set Fso = nothing
    set Regex = nothing

    set GetFiles = Filelist

end function
%>

Example Usage

The following ASP+VBScript code demonstrates how you can use the above function to display the list of image files (bmp, gif, jpg, jpeg, png, tif) inside the "images" directory.

<table border="0" cellspacing="0" cellpadding="5">
    <tr>
        <th>Name</th>
        <th>Type</th>
        <th>Size</th>
        <th>Modified</th>
    </tr>
    <%
    dim Files
    set Files = GetFiles("images", "\.(bmp|gif|jpg|jpeg|png|tif)$")

    if Files.Count = 0 then
        %>
        <tr>
            <td colspan="4">There are no images to display. </td>
        </tr>
        <%
    end if

    dim Filename
    for each Filename in Files
        %>
        <tr>
            <td><%= Filename %></td>
            <td><%= Files(Filename)(0) %></td>
            <td><%= Files(Filename)(1) %></td>
            <td><%= Files(Filename)(2) %></td>
        </tr>
        <%
    next
    %>
</table>
Example Output: Images

set Files = GetFiles("images", "\.(bmp|gif|jpg|jpeg|png|tif)$")

Name Type Size Modified
source-image.png PNG Image 38392 5/10/2009 2:14:25 PM
example1-20px.jpg JPEG Image 37997 5/10/2009 1:24:22 PM
example2-40px.jpg JPEG Image 32299 5/10/2009 1:26:23 PM
Example Output: Documents

set Files = GetFiles("documents", "\.(doc|pub|ppt|xls)$")

Name Type Size Modified
Excel.xls Microsoft Excel Worksheet 11776 5/19/2009 1:07:58 PM
Publisher.pub Microsoft Office Publisher Document 14336 5/19/2009 1:08:12 PM
PowerPoint.ppt Microsoft PowerPoint Presentation 12800 5/19/2009 1:08:08 PM
Word.doc Microsoft Word Document 10752 5/19/2009 1:07:54 PM

Notes

This script uses the FileSystemObject and the Dictionary object, both of these are built into the Microsoft Windows scripting environment. The FileSystemObject allows you to perform filesystem related tasks, such as access, add, move, copy, create or delete files and folders (directories). The Dictionary object is a useful data container that can store key-item pairs.

There is one other object that is of particular interest — the RegExp object. The RegExp object provides simple regular expression support which can be used for pattern matching in strings. Regular expressions are pretty handy when dealing with complex string manipulation. An elementary knowledge is a must-have for any programmer.

The pattern used in the above example is \.(bmp|gif|jpg|jpeg|png|tif)$. This pattern matches strings that "end" with .bmp, .gif, .jpg and so-on.