ASP 101 - Active Server Pages 101 - Web06
The Place ASP Developers Go!

Please visit our partners


Windows Technology Windows Technology
15 Seconds
4GuysFromRolla.com
ASP 101
ASP Wire
VB Forums
VB Wire
WinDrivers.com
internet.commerce internet.commerce
Partners & Affiliates














ASP 101 is an
internet.com site
ASP 101 is an internet.com site
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash





ASP Source Code:
<%
Dim strPath   ' Path of directory to read
Dim objFSO    ' FileSystemObject variable
Dim objFolder ' Folder variable
Dim objFile   ' File variable used to loop through the files
' A recordset object variable and some constants from adovbs.inc.
Dim rstFiles
Const adVarChar = 200
Const adInteger = 3
Const adDate = 7
' Set the path to our sample folder.
strPath = "dir/"
' Create our FSO and get a handle on our folder
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(Server.MapPath(strPath))
' Im ignoring and subdirectories and just dealing with the files
' in the specified folder.  The ADO Recordset object is going to
' have no attached datasource.  I'm just creating it in memory
' and using it to make it easier to deal with the file data.
Set rstFiles = Server.CreateObject("ADODB.Recordset")
rstFiles.Fields.Append "Name", adVarChar, 255
rstFiles.Fields.Append "Size", adInteger
rstFiles.Fields.Append "Date", adDate
rstFiles.Fields.Append "Type", adVarChar, 255
rstFiles.Open
' Loop through the files adding the information to our Recordset
For Each objFile In objFolder.Files
	rstFiles.AddNew
	rstFiles.Fields("name").Value = objFile.Name
	rstFiles.Fields("size").Value = objFile.Size
	rstFiles.Fields("date").Value = objFile.DateCreated
	rstFiles.Fields("type").Value = objFile.Type
Next 'objFile
' All done!  Kill off our File System Object variables.
Set objFile = Nothing
Set objFolder = Nothing
Set objFSO = Nothing
' Now we've got our file data stored in the Recordset and the
' file system is closed.  Now we just use the Recordset object
' to do whatever we want with the data.
' I illustrate sorting in our Sorted Directort List sample:
' http://www.asp101.com/samples/dir_list_sort.asp
' so here I'm going to show how you can perform a query
' just like you would against a database and only show files
' that are smaller then 500 bytes.  That will rule out the
' .gif and .jpg files and should leave the .asp, .htm, .rtf,
' and .txt files.
rstFiles.Filter = "size < 500"
%>
<p>
Only showing files that are smaller then 500 bytes: 
</p>
<table border="3" bordercolor="#000000" cellspacing="0" cellpadding="2">
	<tr bgcolor="#CCCC99">
		<td><b>File Name:</b></td>
		<td><b>File Size (bytes):</b></td>
		<td><b>Date Created:</b></td>
	</tr>
<%
Do While Not rstFiles.EOF
	%>
	<tr bgcolor="#FFFFCC">
		<td align="left" ><%= rstFiles.Fields("name").Value %></td>
		<td align="right"><%= rstFiles.Fields("size").Value %></td>
		<td align="left" ><%= rstFiles.Fields("date").Value %></td>
	</tr>
	<%
	rstFiles.MoveNext
Loop
%>
</table>
<%
' Close our ADO Recordset object
rstFiles.Close
Set rstFiles = Nothing
%>

Back the the Sample Output

Back to the Sample Index


Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers