ASP and FileSystemObjects

2009 June 14
by Adam Estrada

Everyone has a script that they go back and refer to over and over again… While I was at ERDAS, I created this particular script to write the entire Imagine Developers’ Toolkit API to file so that I could compile it as a single CHM. I had the default.asp page scheduled to run on the server every night to ensure that the documentation was as up to date as possible and at the time, that was a pretty high-tech way of accomplishing this task.

I initially used Dreamweaver and classic ASP to do this because Dreamwaver makes it VERY easy to connect to a database and pull data to your web page. In my humble opinion, easy is always better! You’ll notice in the script below that I used FileSystemObject to create my html files on the server. I tend to use FSOs a lot with classic ASP because it’s really freaking easy (again with the easy) to create and manipulate files on the server! Anway, here is my ole faithful asp script so please expect to see numerous variations of it on this site…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!--#include file="Connections/DBExport.asp" -->
<%
Dim rs
Dim rs_numRows
 
Set rs = Server.CreateObject("ADODB.Recordset")
rs.ActiveConnection = MM_DBExport_STRING
rs.Source = "SELECT * FROM dbo.Functions"
rs.CursorType = 0
rs.CursorLocation = 2
rs.LockType = 1
rs.Open()
 
%>
<%
Dim types
Dim types_numRows
 
Set types = Server.CreateObject("ADODB.Recordset")
types.ActiveConnection = MM_DBExport_STRING
types.Source = "SELECT * FROM dbo.DataTypes"
types.CursorType = 0
types.CursorLocation = 2
types.LockType = 1
types.Open()
 
%>
<%
   Server.ScriptTimeout = 10000 
 
   'Set file I/O constants...
   Const ForReading   = 1
   Const ForWriting   = 2
   Const ForAppending = 8
 
   rs.MoveFirst
		Do Until rs.EOF = True   
   'Map to the Requested HTML File...
   filename = rs("FunctionName") & ".html"
   indexfilename = "index.html"
   argname = (rs("FunctionName"))
   datatypes = (types("DataTypeName"))
 
   path = Server.MapPath(".") & "\Functions\" & filename
   indexpath = Server.MapPath(".") & "\" & indexfilename
   datatypename = Server.MapPath(".") & "\DataTypes\" & datatypes
 
   'Call the funtion that will write the content...
   'CreateIndex(indexpath)
   CreateFile(path)
   'CreateType(datatypename)
   response.Write (rs("FunctionName")) + "</BR>"
   rs.MoveNext   
   Loop
%>
<html xmlns="http://www.w3.org/1999/xhtml">
<HEAD>
<title>DB Export</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</HEAD>
<BODY>
<table width="100%" border="0">
  <tr> 
    <td width="30%">&nbsp;</td>
    <td width="70%">&nbsp;</td>
  </tr>
</table>
<% sub CreateIndex(indexpath)
    dim fs, file
    set fs = CreateObject("Scripting.FileSystemObject")
 
       Response.Write("Creating " & indexfilename & ".</BR>")
       set file = fs.CreateTextFile(indexpath)         
       Response.Write("Writing data to " & indexfilename & "..." & "</BR></BR>")
	   Response.Write("<HR></BR>")
       file.WriteLine("<HTML>")
	   file.WriteLine("<TITLE>Table Of Contents</TITLE>" )
       file.WriteLine("<BODY>")
	   ' Write Out the Actual Syntax for Each Function
		Dim TOC
		Dim TOC_numRows
 
		Set TOC = Server.CreateObject("ADODB.Recordset")
		TOC.ActiveConnection = MM_DBExport_STRING
		TOC.Source = "SELECT * FROM dbo.Functions ORDER BY FunctionName ASC "
		TOC.CursorType = 0
		TOC.CursorLocation = 2
		TOC.LockType = 1
		TOC.Open()
		Response.Write( "Please Scroll to the Bottom of the Page..</BR>" )
		   Do While NOT TOC.EOF
			  ' If (trim(argname) = trim(rs("FunctionName")) ) then
						file.WriteLine("<TABLE WIDTH=600 BORDER=0 CELLPADDING=0 CELLSPACING=0>" )
						file.WriteLine("<TR BGCOLOR=#E6E6E6>" )
						file.WriteLine("<TD WIDTH=66 HEIGHT=18></TD>" )
						file.WriteLine("<A HREF=Functions/" & (TOC.Fields.Item("FunctioNname").Value) & ".html>" & (TOC.Fields.Item("FunctioNname").Value))
						file.WriteLine("</TD>" )
						file.WriteLine("</TR>" )
						file.WriteLine("</TABLE>" )
						'else
						'Do Nothing!
						'end if						
						Response.Write(TOC.Fields.Item("FunctioNname").Value) & ".html" + "<BR>"
					TOC.MoveNext					
				Loop 
 
	   file.WriteLine("</BODY>")
	   file.WriteLine("</HTML>")
       file.Close()
	   				TOC.Close()
				Set TOC = Nothing	  
		Response.Write( "<A HREF=Index.html>Click here for the Index Page...</A></BR>" )
 end sub
%>
<% sub CreateFile(path)
 
    dim fs, file
    set fs = CreateObject("Scripting.FileSystemObject")
 
       'Response.Write("Creating " & filename & ".</BR>")
       set file = fs.CreateTextFile(path)         
       'Response.Write("Writing data to " & (rs.Fields.Item("FunctionName").Value) & "..." & "</BR></BR>")
	   'Response.Write("<HR></BR>")
       file.WriteLine("<HTML>")
	   file.WriteLine("<TITLE>"  & (rs.Fields.Item("FunctionName").Value) & "</TITLE>" )
       file.WriteLine("<BODY>")
	   file.WriteLine("<TABLE WIDTH=100% BORDER=0 CELLPADDING=0 CELLSPACING=0>" )
	   file.WriteLine("<TR>")
       file.WriteLine("<TD WIDTH=66><STRONG><FONT COLOR=RED SIZE=" & "4>" & (rs.Fields.Item("FunctionName").Value) & "</BR></BR></FONT></STRONG></TD>" )
	   file.WriteLine("<TD ALIGN=RIGHT VALIGN=TOP><A HREF=../Index.html>Go Back</A></TD>")
	   file.WriteLine("</TR>")
	   file.WriteLine("</TABLE>")
	   file.WriteLine("<HR>")
       file.WriteLine("<STRONG>Summary: " & "</STRONG>" & "</BR>" )
	   file.WriteLine("&nbsp;&nbsp;" & (rs.Fields.Item("Summary").Value) & "</BR></BR>" )
	   file.WriteLine("<STRONG>Header File: " & "</STRONG>" & "</BR>" )
       file.WriteLine("&nbsp;&nbsp;" & (rs.Fields.Item("Header").Value) & "</BR></BR>" )
	   file.WriteLine("<STRONG>Library Name:" & "</STRONG>" & "</BR>" )
       file.WriteLine("&nbsp;&nbsp;" &  (rs.Fields.Item("LibName").Value) & "</BR></BR>" )
 
		' Write Out the Actual Syntax for Each Function
		Dim rs2
		Dim rs2_numRows
 
		Set rs2 = Server.CreateObject("ADODB.Recordset")
		rs2.ActiveConnection = MM_DBExport_STRING
		rs2.Source = "SELECT * FROM dbo.FunctionArguments WHERE FunctionName = '" &  rs("functionName") & "' ORDER BY ID, SortOrder ASC  "
		rs2.CursorType = 0
		rs2.CursorLocation = 2
		rs2.LockType = 1
		rs2.Open()
 
						file.WriteLine("<TABLE WIDTH=600 BORDER=0 CELLPADDING=0 CELLSPACING=0>" )
						file.WriteLine("<TR BGCOLOR=#E6E6E6>" )
						file.WriteLine("<TD WIDTH=66></TD>" )
						file.WriteLine((rs.Fields.Item("ReturnType").Value) & (rs.Fields.Item("ReturnPointerType").Value) & "&nbsp;&nbsp;&nbsp;&nbsp;" &(rs.Fields.Item("FunctionName").Value) & "&nbsp;&nbsp;(" )
						file.WriteLine("</TD>" )
						file.WriteLine("</TR>" )
						file.WriteLine("</TABLE>" )
 
			   Do While NOT rs2.EOF
			   If (trim(argname) = trim(rs("FunctionName")) ) then
						file.WriteLine("<TABLE WIDTH=600 BORDER=0 CELLPADDING=0 CELLSPACING=0>" )
						file.WriteLine("<TR BGCOLOR=#E6E6E6>" )
						file.WriteLine("<TD WIDTH=66></TD>" )
						file.WriteLine("&nbsp;&nbsp;&nbsp;&nbsp;" & (rs2.Fields.Item("ArgumentType").Value) & (rs2.Fields.Item("ArgumentPointerType").Value) & "&nbsp;&nbsp;&nbsp;&nbsp;" & (rs2.Fields.Item("ArgumentName").Value) & "," )
						file.WriteLine("</TD>" )
						file.WriteLine("</TR>" )
						file.WriteLine("</TABLE>" )
						else
						'Do Nothing!
						end if
 
					rs2.MoveNext
				Loop
				rs2.Close()
				Set rs2 = Nothing	  
 
						file.WriteLine("<TABLE WIDTH=600 HEIGHT=18 BORDER=0 CELLPADDING=0 CELLSPACING=0>" )
						file.WriteLine("<TR BGCOLOR=#E6E6E6>" )
						file.WriteLine("&nbsp;&nbsp;)")
						file.WriteLine("</TR>" )
						file.WriteLine("</TABLE>" )
 
		'Write out the Function Argument
		Dim rs3
		Dim rs3_numRows
 
		Set rs3 = Server.CreateObject("ADODB.Recordset")
		rs3.ActiveConnection = MM_DBExport_STRING
		rs3.Source = "SELECT * FROM dbo.FunctionArguments WHERE FunctionName = '" &  rs("functionName") & "' ORDER BY SortOrder ASC"
		rs3.CursorType = 0
		rs3.CursorLocation = 2
		rs3.LockType = 1
		rs3.Open()
 
			   Do While NOT rs3.EOF
			   If (trim(argname) = trim(rs("FunctionName")) ) then
						file.WriteLine("<STRONG>" & (rs3.Fields.Item("ArgumentName").Value) & "</STRONG>" & "</BR>" )
						file.WriteLine("<I>&nbsp;&nbsp;&nbsp;&nbsp;" & (rs3.Fields.Item("Description").Value) & "</I></BR>" )
						else
						'Do Nothing!
						end if
					rs3.MoveNext
				Loop
			rs3.Close()
			Set rs3 = Nothing	  
 
       file.WriteLine("</BR><STRONG>Description: " & "</STRONG>" & "</BR>" )
	   file.WriteLine("&nbsp;&nbsp;" & (rs.Fields.Item("Description").Value) & "</BR></BR>" )
       file.WriteLine("<STRONG>Notes: " & "</STRONG>" & "</BR>" )
	   file.WriteLine("&nbsp;&nbsp;" & (rs.Fields.Item("Notes").Value) & "</BR></BR>" )
	   file.WriteLine("<STRONG>Return Description: " & "</STRONG>" & "</BR>" )
   	   file.WriteLine("&nbsp;&nbsp;" & (rs.Fields.Item("ReturnDescription").Value) & "</BR></BR>" )
	   file.WriteLine("</BODY>")
	   file.WriteLine("</HTML>")
       file.Close()
 
	   ' Now create the Table of Contents!
 
 end sub
  'CreateIndex(indexpath)
%>
</BODY>
</HTML>
<%
rs.Close()
Set rs = Nothing
%>

The output from this script can be viewed here

Bookmark and Share
No comments yet

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS

Spam protection by WP Captcha-Free