Wrox Home  
Search P2P Archive for: Go

  Return to Index  

asp_web_howto thread: Filesystemobject Paging?


Message #1 by "Or Cingilli" <or@a...> on Mon, 11 Feb 2002 16:11:16
I am using the following code to display the contents of a directory. Is 

there a way to display a certain number of files per page rather than all 

of them?



Thanks,



Or



<%@ Language=VBScript %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<%

' ***** Create a dynamic navigation system to allow users to successfully

'       navigate, view, and read files placed in a directory on the web 

server.

'       This directory may contain any number of files of any file type as 

well

'       as any number of subfolders.  

'       Upload this file into the same folder as the folder to be shared, 

and

'       specify the folder's name in the variable 'sDirectory' below.



' ***** Which Folder to Share?

' ********** ********** **********

sDirectory = "Company"

' ********** ********** **********



' ***** Other configurable options

' Which file extensions to allow separated by commas ie: ","  Set to "" 

or "all" to allow all extensions

' note: Unless proper permissions are set, files that are not allowed will 

still be viewable if a user 

'       inputs the exact web location of the file. The method used here 

will only remove the link to the file.

AllowExt = "doc, xls, ppt, txt, mdb"

'AllowExt = "all"

'       DenyExt overrides AllowExt

DenyExt = "asp"





' Top navigation separator

Const sChevron = "&gt;" 	' "/"



' Always display the navigation path? 

Const bShowPath = True  	'False

' Always display the root directory? 

Const bShowRoot = False 	'True



' Page font tag

Const sFont = "<font face=""Verdana, Arial, Helvetica"" size=""2"">"

' Column header font tag

Const sColFont = "<font face=""Verdana, Arial, Helvetica"" size=""2"" 

COLOR=""#FFFFFF"">"

' Column header color

Const TblHeader = "#BFBFBF"	'Grey



' Directory grid alternating colors

Const FileRow1 = "#b6cbeb"	'Dark Blue

Const FileRow2 = "#cadfff"	'Light Blue

Const FolderRow1 = "#879966"	'Dark Green

Const FolderRow2 = "#c5e095"	'Light Green



' Some nice color pairs

'#91619b	'Dark Purple

'#be9cc5	'Light Purple

'#b6cbeb	'Dark Blue

'#cadfff	'Light Blue

'#879966	'Dark Green

'#c5e095	'Light Green

'#a7342a	'Dark Red

'#df867f	'Light Red

'#f8bc03	'Dark Yellow

'#f8e094	'Light Yellow







' ***** Begin Script

Dim sError

On Error Resume Next

sDirectory = trim(sDirectory)

If right(sDirectory,1) <> "/" Then sDirectory = sDirectory & "/" 



' ***** Get subfolder from passed querystring

sDir = sDirectory & Request.querystring("dir")

sDir = trim(sDir)

If right(sDir,1) <> "/" Then sDir = sDir & "/"



' ***** Important! Make sure the subfolder path is in the shared folder. 

This keeps 

'       users from browsing directories outside of the shared. ie: dir=../

'       You may want to include some logging code if this happens, here we 

just

'       put the user back into the default directory.

sFolder = Server.MapPath( sDir )

sDirFolder = Server.MapPath( sDirectory )

sSubFolder = right(sDir,len(sDir)-len(sDirectory))

If instr( sFolder , sDirFolder ) = 0 Then 

  sFolder = sDirFolder

  sSubFolder = ""

  sError = sError & " Path not authorized;"

End If





' ***** Load the file system and navigate to our shared folder.

Set objFileObject = Server.CreateObject("Scripting.FileSystemObject")

Set objFolder = objFileObject.GetFolder( sFolder )



' ***** Oops, missing or misspelled folder path.

If IsEmpty( objFolder ) Then 

  sFolder = sDirFolder

  sSubFolder = ""

  sDir = sDirectory 

  Set objFolder = objFileObject.GetFolder( sFolder )

  sError = sError & " Folder not found;"

End If



%>

<HTML><BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#000000" 

VLINK="#000000">

<TABLE BORDER=0 bgcolor="#f0f0f0" CELLPADDING=0 CELLSPACING=1 width="65%">

  <% ' ***** Feel free to edit the above table tag



' ***** Build path navigation

aNames = Split( sSubFolder , "/")

If bShowPath Then

   If UBound( aNames ) > 0 Or bShowRoot Then %>

  <TR BGCOLOR="#ffffff">

<TD><%= sFont %>&nbsp;</font></TD>

<TD COLSPAN="4"><%= sFont %>

<B>&nbsp; <A HREF="<%= Request.ServerVariables("SCRIPT_NAME") %>"><%= Left

(sDirectory,len(sDirectory)-1) %></A></B>

<% End If



For count = 0 to UBound( aNames ) -1 

  aURL = aURL & aNames(count) & "/"

   %>&nbsp;<%= sChevron %>&nbsp;<B><A HREF="<%= Request.ServerVariables

("SCRIPT_NAME") %>?dir=<%= Server.URLEncode( aURL ) %>"><%= aNames(count) %

></A></B><%

Next   %></font></TD></TR><% 

End If 



   For count = 0 to UBound( aNames ) -2

      aDirUp = aDirUp & aNames(count) & "/"

   Next %>



<TR BGCOLOR="<%= TblHeader %>">

  <TD BGCOLOR="#ffffff"><A HREF="<%= Request.ServerVariables

("SCRIPT_NAME") %>?dir=<%= Server.URLEncode( aDirUp ) %>"><font 

face="Wingdings" COLOR="<%= TblHeader %>">Ç</font></a></TD>

  <TD><%= sColFont %>&nbsp; <B>Filename:</B> &nbsp;</FONT></TD>

  <TD><%= sColFont %>&nbsp; <B>Size:</B> &nbsp;</FONT></TD>

  <TD><%= sColFont %>&nbsp; <B>File type:</B> &nbsp;</FONT></TD>

  <TD><%= sColFont %>&nbsp; <B>Date created:</B> &nbsp;</FONT></TD>

<%

' ***** Iterate through the subfolders in our shared folder.

For Each objFile In objFolder.SubFolders 



' ***** Alternate between these two row colors.

    If iAlternate = 0 Then 

      response.write "<TR BGCOLOR=""" & FolderRow1 & """>" 

      iAlternate = 1

    Else 

      response.write "<TR BGCOLOR=""" & FolderRow2 & """>"

      iAlternate = 0 

    End If



' ***** Display folder with link to navigate

%>  <TD align="center" BGCOLOR="<%= TblHeader %>"><font face="Wingdings" 

COLOR="#ffffff">0</font></TD>

  <TD><%= sFont %>&nbsp; <A HREF="<%= Request.ServerVariables

("SCRIPT_NAME") %>?dir=<%= Server.URLEncode(  sSubFolder & objFile.Name )%

>"><%= objFile.Name %></A> &nbsp;</font></TD>

  <TD align="right"><%= sFont %>&nbsp; <%= ByteConversion( objFile.Size ) %

> &nbsp;</font></TD>

  <TD><%= sFont %>&nbsp; <%= objFile.Type %> &nbsp;</font></TD>

  <TD><%= sFont %>&nbsp; <%= objFile.DateCreated %> &nbsp;</font></TD>

</TR>

<%

' ***** Next Folder

NEXT



' ***** Iterate through the files in our shared folder / subfolder.

For Each objFile In objFolder.Files



   sFileName = objFile.name



   ' ***** Only continue if it's a valid extension

   If ( IsValidFile (sFileName) ) Then 



   ' ***** Alternate between these two row colors.

   '       We'll use the same counter variable to continue alternating 

between 

   '       the light / dark shade according to the previous folder row 

color.

      If iAlternate = 0 Then 

          response.write "<TR BGCOLOR=""" & FileRow1 & """>" 

          iAlternate = 1

      Else 

         response.write "<TR BGCOLOR=""" & FileRow2 & """>"

         iAlternate = 0 

      End If

   ' ***** Display file with link to execute / dowload.

%>  <TD align="center" BGCOLOR="<%= TblHeader %>"><font face="Wingdings" 

COLOR="#ffffff"><</font></TD>

  <TD><%= sFont %>&nbsp; <A HREF="<%= sDir %><%= sFileName %>"><%= 

sFileName %></A> &nbsp;</font></TD>

  <TD ALIGN=RIGHT><%= sFont %>&nbsp; <%= ByteConversion( objFile.Size ) %> 

&nbsp;</font></TD>

  <TD><%= sFont %>&nbsp; <%= objFile.Type %> &nbsp;</font></TD>

  <TD><%= sFont %>&nbsp; <%= objFile.DateCreated %> &nbsp;</font></TD>

</TR><%

   End If



' ***** Next File

NEXT  



' ***** Clean up those nasty memory leaks

Set objFileObject = nothing

Set objFolder = nothing



' ***** Iterate through and approve extensions

Function IsValidFile(FileName)

   If Not AllowExt <> "" or LCase( AllowExt ) = "all" Then

     IsValidFile = True

   Else

     aAllowExt = Split( AllowExt & "," , ",")

     IsValidFile = False

     For iCnt = 0 to UBound( aAllowExt ) -1

       If right( FileName , len( FileName ) - InStrRev( FileName , "." ) ) 

= Trim(aAllowExt( iCnt )) Then IsValidFile = True

     Next

   End If

   If DenyExt <> "" Then

     aDenyExt = Split( DenyExt & "," , ",")

     For iCnt = 0 to UBound( aDenyExt ) -1

       If right( FileName , len( FileName ) - InStrRev( FileName , "." ) ) 

= Trim(aDenyExt( iCnt )) Then IsValidFile = False

     Next

   End If

End Function 



' ***** Display friendly byte size

Function ByteConversion(NumberOfBytes)

   If NumberOfBytes < 1024 Then 

      sDisplayBytes = NumberOfBytes & " Bytes"

   End If

   If NumberOfBytes >= 1024 Then  

      sDisplayBytes = FormatNumber( NumberOfBytes / 1024, 2) & " KB"

   End If

   If NumberOfBytes > 1048576 Then 

      sDisplayBytes = FormatNumber( NumberOfBytes / 1048576, 2) & " MB"

   End If

   Response.Write sDisplayBytes 

End Function 



' ***** Did we encounter an Error?

If Err <> 0 or sError <> "" Then

  response.write "<TR><TD bgcolor=""#ffffff"" colspan=""5""><font 

face=""Verdana, Arial, Helvetica"" color=""red"" size=""1"">ERROR: " & 

sError & space(1) & "ASP: " & Err.description & ";</font></TD></TR>"

End If





%>



</TABLE>

</BODY></HTML>
Message #2 by "phil griffiths" <pgtips@m...> on Wed, 13 Feb 2002 14:16:30
One way would be to load all the filenames into a js array and on the 

client-side just display the appropriate number of files, adding 

next/previous links to display the others.



HTH

Phil

> I am using the following code to display the contents of a directory. Is 

> there a way to display a certain number of files per page rather than 

all 

> of them?


  Return to Index