Scripting.FileSystemObject Read Subfolders

Read subfolders with the Scripting.FileSystemObject:

<%
Dim fso
Dim ObjFolder
Dim ObjSubFolders
Dim ObjSubFolder
Set fso = CreateObject(“Scripting.FileSystemObject”)
Set ObjFolder = fso.GetFolder([folder path])
Set ObjSubFolders = ObjFolder.SubFolders
Response.Write(“<ul>”)
For Each ObjSubFolder In ObjSubFolders
Response.Write(“<li>” & ObjSubFolder.Name & ” – ” & ObjSubFolder.Path & “</li>”)
Next
Response.Write(“</ul>”)
%>

How to display ASP errors on Windows 2008 II7

By default ASP errors are not displayed on a Windows 2008, II7 server. The default error message displayed simply says:

Server Error
500 – Internal server error.
There is a problem with the resource you are looking for, and it cannot be displayed.

This is for very good reason. Detailed error message can display sensitive information that you do not want public. But for development purposes we need to see the error message to properly resolve the error. Here are the steps to display the detailed error message.

In the IIS settings for the website:

1. Go to “ASP” (under IIS).

2. Set “Send Errors to Browser” to true.

If it still doesn’t display the detailed error follow these steps.

1. Go to “Configuration Editor” (under Management section).

2. Under system.webServer, select “httpErrors”.

3. Set errorMode to “Detailed”

Be sure to add proper error handling when taking the website live.

ASP On Error Resume Next

Last night I found a page with an “On Error Resume Next” that has a script error that has been causing problems on one of my servers for months. It took a long time to narrow down the source to a single site but then it didn’t take long to find the page with the problem. Then I found an “On Error Resume Next” line in the code and just wanted to shake my fist at somebody (in a good natured sort of way, of course). The code was really, really bad. But I was so relieved to have found the problem.

 

To clarify how this can be a problem, you don’t want code to continue to execute in a loop when there is an error. For example. This would be bad:

 

On Error Resume Next

Set rs = oConn.Execute(“Select * From Orders”)

If Not rs.Eof Then

                Do While Not rs.Eof

                                Response.Write(“OrderID=” & rs(“OrderID”) & “<br>”)

                                rs.MoveNxt

                Loop

End If

 

Since the error (“rs.MoveNxt”) is the move to the next record it will be stuck on the first record and the end of data will never be reached. Yes the script should timeout but an accumlation of errors can cause memory issues on the server and even hang the web service.

 

Here is the recommended way to use the ASP error handler:

 

On Error Resume Next

               

                … do code here

 

If Err.Number <> 0 Then

                sMessage=”There was an error: “ & Err.Description

                Err.Clear

                On Error Goto 0

End If

 

Redirect to a single site

Use this ASP code to redirect your pages to a single site. Search engines may penalize you for duplicate content if they find content at yoursite.com and www.yoursite.com or multiple domains pointing at the same site.

‘—————————————–
If  lcase(Request.ServerVariables(“HTTP_HOST”))=”lyonscom.com” OR lcase(Request.ServerVariables(“HTTP_HOST”))=”www.sitenumber2.com” OR lcase(Request.ServerVariables(“HTTP_HOST”))=”sitenumber2.com” Then
 Response.Status = “301 Moved Permanently”
 If lCase(Request.ServerVariables(“URL”)) = “/default.asp” Then   ‘ change home page here
  If Len(Request.Querystring)=0 Then
   Response.AddHeader “Location”, “http://www.lyonscom.com/
  Else
   Response.AddHeader “Location”, “http://www.lyonscom.com/?” & Request.Querystring
  End If
 Else
 If Len(Request.Querystring)=0 Then
   Response.AddHeader “Location”, “http://www.lyonscom.com” & Request.ServerVariables(“URL”)
  Else
   Response.AddHeader “Location”, “http://www.lyonscom.com” & Request.ServerVariables(“URL”) & “?” & Request.Querystring
  End If
 End If
 Response.End
End If
‘—————————————–

Note, the Response.Status line will tell the search engine crawler to remove the duplicate content and only index the conent at the new location.

“Save As” Dialog Box for Streaming File

To download files using a “Save As” dialog box try this code:

        Select Case FileExt
            Case “gif” ContentType = “image/gif”
            Case “tif” ContentType = “image/tiff”
            Case “jpg” ContentType = “image/jpeg”
            Case “pdf” ContentType = “application/pdf”
            Case “avi” ContentType = “video/x-msvideo”
            Case “mp3” ContentType = “audio/mpeg”
            Case “mpg” ContentType = “video/mpeg”
            Case “wav” ContentType = “audio/wav”
            Case “rar” ContentType = “application/x-rar-compressed”
            Case “zip” ContentType = “application/x-zip-compressed”
            Case “exe” ContentType = “application/x-msdownload”
            Case Else
             ‘ FilePath = “”
             ContentType = “application/x-msdownload”
        End Select

        Response.Clear
        Response.Buffer = False
        Dim adoStream
        Set adoStream = Server.CreateObject(“ADODB.Stream”)
        adoStream.Open()
        adoStream.Type = 1
        adoStream.LoadFromFile(FilePath + “/” + FileName)
        Response.AddHeader “Content-Disposition”, “attachment;filename=” & Right(FileName,Len(FileName)-InStr(FileName,”/”))
        Response.ContentType = ContentType
        Response.BinaryWrite(adoStream.Read())
        adoStream.Close
        Set adoStream = Nothing
        Response.End

The ASP buffering limit is about 4 megs. Files larger than that will give an error like this:

   “Response Buffer to exceed its configured limit”

To stream out files larger than 4 Meg you can increase the “AspBufferingLimit” value. Edit the MetaBase.xml file in the C:\WINDOWS\system32\inetsrv folder. The default value is 4194304. Check the “Enable Direct Metabase Edits” check box before editing this file and you will not need to stop and start the WWW service.

Permanent Redirect

Instead of doing a Response.Redirect to point to a page in a new location (I.e. the page has been renamed), do a permanent redirect. Using this code tells the search engines to no longer index the old page but to index the new page.

   Response.Status = “301 Moved Permanently”
   Response.AddHeader “Location”, “new-page-name.asp”