Formatted Version of VBScript Code
This page contains some Badly Formatted VBScript code, and its VBScript Formatted version. The output was produced by our VBScript Formatter.
Badly Formatted VBScript Code
This code is a real program, artificially formatted badly in a way that exactly preserves its functionality to model what happens after too many programmers have hacked on it. The nesting structure is pretty bad; it would be hard to work on this without painfully reformatting it in an editor. There goes some productivity.
<!--#INCLUDE FILE="BBSAuth.inc"-->
<html>
<head>
<!--#INCLUDE FILE="CommonRoutines.inc"-->
<!--#INCLUDE FILE="BBSRoutines.inc"-->
<!--#INCLUDE FILE="dbConnect_BBS.inc"-->
<!--#INCLUDE FILE="CheckSecurity.inc"-->
<title>Upload</title>
<!--#INCLUDE FILE="header.inc"-->
</head>
<body>
<!--#INCLUDE FILE="Upload.inc"-->
<%
'testing response.write "<table border='1'>"
'testing For Each key in Request.ServerVariables
'testing response.write "<tr><td>" & key & "</td><td>" & request.ServerVariables(key) & "</td></tr>"
'testing next
'testing Response.Write "</table>"
'testing response.write "<table border='1'>"
'testing For Each key in Request.Form
'testing response.write "<tr><td>" & key & "</td><td>" & request.Form(key) & "</td></tr>"
'testing next
'testing Response.Write "</table>"
SpaceAllotted = MakeNumber(sec_Topics.Fields("Upload Space Per Member")) * 1024
'-- ----------------------------------------------- --
'-- First make sure that this topic has a directory --
'-- ----------------------------------------------- --
Dim DestinationPath
WebPath = "http://" & Request.ServerVariables("HTTP_HOST") & AppServerRoot() & "/Uploads/" & TopicID
WebPath = WebPath & "/"& BBSProfileID
DestinationPath = AppFileRoot() & "\Uploads\" & TopicID
Set WorkFileObject = server.createobject("Scripting.FileSystemObject")
On Error Resume Next
Set NewFolder = WorkFileObject.CreateFolder(DestinationPath)
Set NewFolder = Nothing
DestinationPath = DestinationPath & "\" & BBSProfileID
Set NewFolder = WorkFileObject.CreateFolder(DestinationPath)
Set NewFolder = Nothing
On Error Goto 0
%>
<h2 align="center"><%= sec_Topics.Fields("Topic Title") & IIf(sec_Topics.Fields("Topic Locked"), " <strong><em>* LOCKED *</em></strong>","") %></h2>
<% If Not IsNull(sec_Topics.Fields("Description")) Then %>
<h4 align="center"><%= MemoWrite(sec_Topics.Fields("Description")) %></h4>
<% End If %>
<h3 align="center">Document Upload</h3>
<center>
<table border="0">
<tr><td valign="bottom" align="center"><a href='BBSBoard.asp?TopicID=<%= TopicID %>'>Return To Index</a></td>
</tr>
</table>
</center>
<%
Dim ResultHTML
Server.ScriptTimeout = 500
If Request.ServerVariables("REQUEST_METHOD") = "POST" Then 'Request method must be "POST" for get the fields
If LCase(Left(Request.ServerVariables("HTTP_Content_Type"),19)) = "multipart/form-data" Then
UploadSizeLimit = 10000000 ' 10 Mb
'************************************************* Main Upload - start
Dim Upload
UploadFile1 = ""
UploadFile2 = ""
'Gets uploaded fields
On Error GoTo 0
GetUpload()
'************************************************* Main Upload - end
ElseIf Request.Form("B1") = "Delete" Then
For Each key in Request.Form
If UCase(Left(key, 6)) = "DELETE" Then
Response.Write "Deleting " & Request.Form(Key) & "<br>"
On Error Resume Next
WorkFileObject. DeleteFile (DestinationPath & "\" & Request.Form(Key))
On Error GoTo 0
End If
Next
End If
End If
'-- -------------------------------------------- --
'-- Do quick survey of files and remove bad ones --
'-- -------------------------------------------- --
SQLStmt = "Select * From [tblAllowedUploads] "
SQLStmt = SQLStmt & "Where [TopicID] " & SQLNumberFormat("=", TopicID)
Set rs_AllowedUpload = Server.CreateObject("ADODB.Recordset")
rs_AllowedUpload.Open SQLStmt, BBS_conn, 3, 3
Set WorkFileFolder = WorkFileObject.GetFolder(DestinationPath)
Set WorkFileList = WorkFileFolder.Files
FileCtr = 0
TotFileSize = 0
For Each WrkFileName In WorkFileList
If rs_AllowedUpload.Eof Then
'-- No files allowed (yes it's stupid, but possible) --
Response.write "Invalid file " & WrkFileName.Name & " deleted<br>"
WorkFileObject.DeleteFile(DestinationPath & "\" & WrkFileName.Name)
Else
If rs_AllowedUpload.Fields("Allow Upload Of") = "ALL" Then
'Do Nothing - All files allowed
FileCtr = FileCtr + 1
TotFileSize = TotFileSize + WrkFileName.Size
Else
GoodFile = False
Do While Not rs_AllowedUpload.Eof And _
Not GoodFile
If Right(LCase(WrkFileName.Name),Len(rs_AllowedUpload.Fields("Allow Upload Of"))) = LCase(rs_AllowedUpload.Fields("Allow Upload Of")) Then
GoodFile = True
Else
rs_AllowedUpload.MoveNext
End If
Loop
rs_AllowedUpload.MoveFirst
If Goodfile Then
'-- Do Nothing
FileCtr = FileCtr + 1
TotFileSize = TotFileSize + WrkFileName.Size
Else
Response.write "Invalid file " & WrkFileName.Name & " deleted<br>"
WorkFileObject.DeleteFile(DestinationPath & "\" & WrkFileName.Name)
End If
End If
End If
Next
rs_AllowedUpload.Close
Set rs_AllowedUpload = Nothing
Randomize
If SpaceAllotted <= TotFileSize Then
Response.Write "<h2 align='center'>Your Maximum space has been used</h2>"
If (SpaceAllotted * 1.10 ) <= TotFileSize Then
'-- Remove most recent upload --
On Error Resume Next
If UploadFile1 <> "" Then
Response.Write "<h3 align='center'>Removing Uploaded File " & UploadFile1 & "</h3>"
WorkFileObject.DeleteFile(DestinationPath & "\" & UploadFile1)
End If
If UploadFile2 <> "" Then
Response.Write "<h3 align='center'>Removing Uploaded File " & UploadFile2 & "</h3>"
WorkFileObject.DeleteFile(DestinationPath & "\" & UploadFile2)
End If
End If
End If
'-- Get the final file list and sort it --
FileCtr = 0
TotFileSize=0
For Each WrkFileName In WorkFileList
FileCtr = FileCtr + 1
TotFileSize = TotFileSize + WrkFileName.Size
Next
If FileCtr > 0 Then
FileCtr = 0
For Each WrkFileName In WorkFileList
FileCtr = FileCtr + 1
Next
If FileCtr > 0 Then
ReDim FileList(FileCtr)
ReDim FileSize(FileCtr)
ReDim srtFileList(FileCtr)
ReDim srtFileSize(FileCtr)
ICtr = 1
FileList(0) = "ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ"
For Each WrkFileName In WorkFileList
FileList(ICtr) = LCase(WrkFileName.Name)
FileSize(ICtr) = WrkFileName.Size
ICtr = ICtr + 1
Next
'-- ----------------- --
'-- Now sort the list --
'-- ----------------- --
For ICtr = 1 To FileCtr
LowCtr = 0
For ICt2 = 1 To FileCtr
If FileList(ICt2) < FileList(LowCtr) Then
LowCtr = ICt2
End If
Next
If LowCtr > 0 Then
srtFileList(ICtr) = FileList(LowCtr)
srtFileSize(ICtr) = FileSize(LowCtr)
FileList(LowCtr) = FileList(0)
Else
srtFileList(ICtr) = ""
End If
Next
End If
End If
If SpaceAllotted > TotFileSize Then
TopicSpaceUsed = 0
Set TopicFileObject = server.createobject("Scripting.FileSystemObject")
If TopicFileObject.FolderExists(AppFilePath() & "\Uploads\" & TopicID & "\") Then
Set TopicFile = TopicFileObject.GetFolder(AppFilePath() & "\Uploads\" & TopicID & "\")
TopicSpaceUsed = TopicFile.Size
Set TopicFile = Nothing
End If
Set TopicFileObject = Nothing
If (TopicSpaceUsed/1024) > sec_Topics.Fields("Upload Space Per Topic") Then
Response.Write "<h2 align='center'>The Maximum space for this BBS has been used - Contact " & MemoWrite(sec_Topics.Fields("UserEMail")) & "</h2>"
Else %>
<form method="post" action="<%= ShortScriptName %>.ASP?TopicID=<%= TopicID %>" ENCTYPE="multipart/form-data">
<div align="center"><center>
<table border="1">
<tr BGColor="Silver">
<td nowrap></td>
<td Align="Right"><input type="submit" Name="Action" value="Upload New Documents"></td>
</tr>
<tr>
<td nowrap>File 1</td>
<td><input type="file" name="File1" size="70"></td>
</tr>
<tr>
<td nowrap>File 2</td>
<td><input type="file" name="File2" size="70"></td>
</tr>
</table>
</center></div>
</form>
<% End If
End If
Set WorkFileList = Nothing
Set WorkFileFolder = Nothing
Set WorkFileObject = Nothing %>
<center>
<form action='<%= ShortScriptName %>.asp?TopicID=<%= TopicID %>' method='POST'>
<table border='1'>
<tr>
<td colspan='3' align='center' bgcolor='#C0C0C0' style='font-weight: bold; font-size: larger'>Uploaded<br>Files</td>
</tr>
<tr>
<td align='left' valign='top' bgcolor='#C0C0C0'>File Name</td>
<td align='center' valign='top' bgcolor='#C0C0C0'>Delete</td>
<td align='right' valign='top' bgcolor='#C0C0C0'>Size</td>
</tr>
<% For PCtr = 1 To FileCtr %>
<tr>
<td align='left' valign='top'>
<a href='<%= WebPath & "/" & srtFileList(PCtr) %>'
target='<%= "New" & NumberToString(Rnd() * 1000,4,0) %>'>
<%= WebPath & "/" & srtFileList(PCtr) %></a>
</td>
<td align='center' valign='top'>
<input type='Checkbox' name='Delete<%= NumberToString(PCtr,4,0) %>' value='<%= MemoView(srtFileList(PCtr)) %>'>
</td>
<td align='right' valign='top'><%= NumberToString(srtFileSize(PCtr) / 1024,"Float",2) %> k</td>
</tr>
<% Next %>
<tr>
<td align='left' valign='top' colspan='2'><strong>Total</strong></td>
<td align='right' valign='top'><strong><%= NumberToString(TotFileSize / 1024,"Float",2) %> k</strong></td>
</tr>
<tr>
<td align='left' valign='top' colspan='2'><strong>Maximum</strong></td>
<td align='right' valign='top'><strong><%= NumberToString(MakeNumber(sec_Topics.Fields("Upload Space Per Member")),"Float",2) %> k</strong></td>
</tr>
<tr>
<td align='left' valign='top' colspan='2'><strong>Available</strong></td>
<td align='right' valign='top'><strong><%= NumberToString((SpaceAllotted - TotFileSize) / 1024,"Float",2) %> k</strong></td>
</tr>
</table>
<input type='submit' name='B1' value='Delete' ONCLICK="return Confirm_Delete()">
</form>
<%
SecurityClose()
BBS_Conn.Close
Set BBS_Conn = Nothing
%>
<!--#INCLUDE FILE="footer.inc"-->
</body>
</html>
VBScript Formatted Version
This is the result of using SD's VBScriptFormatter tool on the sample badly formatted VBScript, using just the default settings. You can see that the formatter reindented according to the the language structure. Especially note the indentation of the ASP code inside THEN blocks. The block structure is now clearly visible.
<!--#INCLUDE FILE="BBSAuth.inc"-->
<html>
<head>
<!--#INCLUDE FILE="CommonRoutines.inc"-->
<!--#INCLUDE FILE="BBSRoutines.inc"-->
<!--#INCLUDE FILE="dbConnect_BBS.inc"-->
<!--#INCLUDE FILE="CheckSecurity.inc"-->
<title>Upload</title>
<!--#INCLUDE FILE="header.inc"-->
</head>
<body>
<!--#INCLUDE FILE="Upload.inc"-->
<%
'testing response.write "<table border='1'>"
'testing For Each key in Request.ServerVariables
'testing response.write "<tr><td>" & key & "</td><td>" & request.ServerVariables(key) & "</td></tr>"
'testing next
'testing Response.Write "</table>"
'testing response.write "<table border='1'>"
'testing For Each key in Request.Form
'testing response.write "<tr><td>" & key & "</td><td>" & request.Form(key) & "</td></tr>"
'testing next
'testing Response.Write "</table>"
SpaceAllotted = MakeNumber(sec_Topics.Fields("Upload Space Per Member"))*1024
'-- ----------------------------------------------- --
'-- First make sure that this topic has a directory --
'-- ----------------------------------------------- --
Dim DestinationPath
WebPath = "http://"&Request.ServerVariables("HTTP_HOST")&AppServerRoot()&"/Uploads/"&TopicID
WebPath = WebPath&"/"&BBSProfileID
DestinationPath = AppFileRoot()&"\Uploads\"&TopicID
Set WorkFileObject = server.createobject("Scripting.FileSystemObject")
On Error Resume Next
Set NewFolder = WorkFileObject.CreateFolder(DestinationPath)
Set NewFolder = Nothing
DestinationPath = DestinationPath&"\"&BBSProfileID
Set NewFolder = WorkFileObject.CreateFolder(DestinationPath)
Set NewFolder = Nothing
On Error GoTo 0
%> <h2 align="center"><%= sec_Topics.Fields("Topic Title")&IIf(sec_Topics.Fields("Topic Locked")," <strong><em>* LOCKED *</em></strong>","")
%> </h2>
<%
If Not IsNull(sec_Topics.Fields("Description")) Then
%> <h4 align="center"><%= MemoWrite(sec_Topics.Fields("Description"))
%> </h4>
<%
End If
%> <h3 align="center">Document Upload</h3>
<center>
<table border="0">
<tr><td valign="bottom" align="center"><a href='BBSBoard.asp?TopicID=<%= TopicID
%> '>Return To Index</a></td>
</tr>
</table>
</center>
<%
Dim ResultHTML
Server.ScriptTimeout = 500
If Request.ServerVariables("REQUEST_METHOD") = "POST" Then 'Request method must be "POST" for get the fields
If LCase(Left(Request.ServerVariables("HTTP_Content_Type"),19)) = "multipart/form-data" Then
UploadSizeLimit = 10000000 ' 10 Mb
'************************************************* Main Upload - start
Dim Upload
UploadFile1 = ""
UploadFile2 = ""
'Gets uploaded fields
On Error GoTo 0
GetUpload()
'************************************************* Main Upload - end
ElseIf Request.Form("B1") = "Delete" Then
For Each key In Request.Form
If UCase(Left(key,6)) = "DELETE" Then
Response.Write "Deleting "&Request.Form(Key)&"<br>"
On Error Resume Next
WorkFileObject.DeleteFile(DestinationPath&"\"&Request.Form(Key))
On Error GoTo 0
End If
Next
End If
End If
'-- -------------------------------------------- --
'-- Do quick survey of files and remove bad ones --
'-- -------------------------------------------- --
SQLStmt = "Select * From [tblAllowedUploads] "
SQLStmt = SQLStmt&"Where [TopicID] "&SQLNumberFormat("=",TopicID)
Set rs_AllowedUpload = Server.CreateObject("ADODB.Recordset")
rs_AllowedUpload.Open SQLStmt,BBS_conn,3,3
Set WorkFileFolder = WorkFileObject.GetFolder(DestinationPath)
Set WorkFileList = WorkFileFolder.Files
FileCtr = 0
TotFileSize = 0
For Each WrkFileName In WorkFileList
If rs_AllowedUpload.Eof Then
'-- No files allowed (yes it's stupid, but possible) --
Response.write "Invalid file "&WrkFileName.Name&" deleted<br>"
WorkFileObject.DeleteFile(DestinationPath&"\"&WrkFileName.Name)
Else
If rs_AllowedUpload.Fields("Allow Upload Of") = "ALL" Then
'Do Nothing - All files allowed
FileCtr = FileCtr+1
TotFileSize = TotFileSize+WrkFileName.Size
Else
GoodFile = False
Do While Not rs_AllowedUpload.Eof And Not GoodFile
If Right(LCase(WrkFileName.Name),Len(rs_AllowedUpload.Fields("Allow Upload Of"))) = LCase(rs_AllowedUpload.Fields("Allow Upload Of")) Then
GoodFile = True
Else
rs_AllowedUpload.MoveNext
End If
Loop
rs_AllowedUpload.MoveFirst
If Goodfile Then
'-- Do Nothing
FileCtr = FileCtr+1
TotFileSize = TotFileSize+WrkFileName.Size
Else
Response.write "Invalid file "&WrkFileName.Name&" deleted<br>"
WorkFileObject.DeleteFile(DestinationPath&"\"&WrkFileName.Name)
End If
End If
End If
Next
rs_AllowedUpload.Close
Set rs_AllowedUpload = Nothing
Randomize
If SpaceAllotted <= TotFileSize Then
Response.Write "<h2 align='center'>Your Maximum space has been used</h2>"
If(SpaceAllotted*1.10) <= TotFileSize Then
'-- Remove most recent upload --
On Error Resume Next
If UploadFile1 <> "" Then
Response.Write "<h3 align='center'>Removing Uploaded File "&UploadFile1&"</h3>"
WorkFileObject.DeleteFile(DestinationPath&"\"&UploadFile1)
End If
If UploadFile2 <> "" Then
Response.Write "<h3 align='center'>Removing Uploaded File "&UploadFile2&"</h3>"
WorkFileObject.DeleteFile(DestinationPath&"\"&UploadFile2)
End If
End If
End If
'-- Get the final file list and sort it --
FileCtr = 0
TotFileSize = 0
For Each WrkFileName In WorkFileList
FileCtr = FileCtr+1
TotFileSize = TotFileSize+WrkFileName.Size
Next
If FileCtr > 0 Then
FileCtr = 0
For Each WrkFileName In WorkFileList
FileCtr = FileCtr+1
Next
If FileCtr > 0 Then
ReDim FileList(FileCtr)
ReDim FileSize(FileCtr)
ReDim srtFileList(FileCtr)
ReDim srtFileSize(FileCtr)
ICtr = 1
FileList(0) = "\u00ff\u00ff\u00ff\u00ff\u00ff\u00ff\u00ff\u00ff\u00ff\u00ff\u00ff\u00ff\u00ff\u00ff\u00ff\u00ff\u00ff\u00ff\u00ff\u00ff\u00ff\u00ff"
For Each WrkFileName In WorkFileList
FileList(ICtr) = LCase(WrkFileName.Name)
FileSize(ICtr) = WrkFileName.Size
ICtr = ICtr+1
Next
'-- ----------------- --
'-- Now sort the list --
'-- ----------------- --
For ICtr = 1 To FileCtr
LowCtr = 0
For ICt2 = 1 To FileCtr
If FileList(ICt2) < FileList(LowCtr) Then
LowCtr = ICt2
End If
Next
If LowCtr > 0 Then
srtFileList(ICtr) = FileList(LowCtr)
srtFileSize(ICtr) = FileSize(LowCtr)
FileList(LowCtr) = FileList(0)
Else
srtFileList(ICtr) = ""
End If
Next
End If
End If
If SpaceAllotted > TotFileSize Then
TopicSpaceUsed = 0
Set TopicFileObject = server.createobject("Scripting.FileSystemObject")
If TopicFileObject.FolderExists(AppFilePath()&"\Uploads\"&TopicID&"\") Then
Set TopicFile = TopicFileObject.GetFolder(AppFilePath()&"\Uploads\"&TopicID&"\")
TopicSpaceUsed = TopicFile.Size
Set TopicFile = Nothing
End If
Set TopicFileObject = Nothing
If(TopicSpaceUsed/1024) > sec_Topics.Fields("Upload Space Per Topic") Then
Response.Write "<h2 align='center'>The Maximum space for this BBS has been used - Contact "&MemoWrite(sec_Topics.Fields("UserEMail"))&"</h2>"
Else
%>
<form method="post" action="<%= ShortScriptName
%> .ASP?TopicID=<%= TopicID
%> " ENCTYPE="multipart/form-data">
<div align="center"><center>
<table border="1">
<tr BGColor="Silver">
<td nowrap></td>
<td Align="Right"><input type="submit" Name="Action" value="Upload New Documents"></td>
</tr>
<tr>
<td nowrap>File 1</td>
<td><input type="file" name="File1" size="70"></td>
</tr>
<tr>
<td nowrap>File 2</td>
<td><input type="file" name="File2" size="70"></td>
</tr>
</table>
</center></div>
</form>
<%
End If
End If
Set WorkFileList = Nothing
Set WorkFileFolder = Nothing
Set WorkFileObject = Nothing
%> <center>
<form action='<%= ShortScriptName
%> .asp?TopicID=<%= TopicID
%> ' method='POST'>
<table border='1'>
<tr>
<td colspan='3' align='center' bgcolor='#C0C0C0' style='font-weight: bold; font-size: larger'>Uploaded<br>Files</td>
</tr>
<tr>
<td align='left' valign='top' bgcolor='#C0C0C0'>File Name</td>
<td align='center' valign='top' bgcolor='#C0C0C0'>Delete</td>
<td align='right' valign='top' bgcolor='#C0C0C0'>Size</td>
</tr>
<%
For PCtr = 1 To FileCtr
%> <tr>
<td align='left' valign='top'>
<a href='<%= WebPath&"/"&srtFileList(PCtr)
%> '
target='<%= "New"&NumberToString(Rnd()*1000,4,0)
%> '>
<%= WebPath&"/"&srtFileList(PCtr)
%> </a>
</td>
<td align='center' valign='top'>
<input type='Checkbox' name='Delete<%= NumberToString(PCtr,4,0)
%> ' value='<%= MemoView(srtFileList(PCtr))
%> '>
</td>
<td align='right' valign='top'><%= NumberToString(srtFileSize(PCtr)/1024,"Float",2)
%> k</td>
</tr>
<%
Next
%> <tr>
<td align='left' valign='top' colspan='2'><strong>Total</strong></td>
<td align='right' valign='top'><strong><%= NumberToString(TotFileSize/1024,"Float",2)
%> k</strong></td>
</tr>
<tr>
<td align='left' valign='top' colspan='2'><strong>Maximum</strong></td>
<td align='right' valign='top'><strong><%= NumberToString(MakeNumber(sec_Topics.Fields("Upload Space Per Member")),"Float",2)
%> k</strong></td>
</tr>
<tr>
<td align='left' valign='top' colspan='2'><strong>Available</strong></td>
<td align='right' valign='top'><strong><%= NumberToString((SpaceAllotted-TotFileSize)/1024,"Float",2)
%> k</strong></td>
</tr>
</table>
<input type='submit' name='B1' value='Delete' ONCLICK="return Confirm_Delete()">
</form>
<%
SecurityClose()
BBS_Conn.Close
Set BBS_Conn = Nothing
%>
<!--#INCLUDE FILE="footer.inc"-->
</body>
</html>
For more information: Info@semanticdesigns.com
Copyright 1995-2010 Semantic Designs, Incorporated
DMS and "Design Maintenance System" are registered trademarks of Semantic Designs, Inc.
The SD logo and "Semantic Designs" are registered service marks of Semantic Designs, Inc.
CloneDR, PARLANSE, JOVIAL2C, Thicket, Smart Differencer are trademarks of Semantic Designs, Inc.
The OMG logo is a registered trademark of the Object Management Group, Inc. in the United States and other countries.
To view our Privacy Policy, click here
Comments or problems: Webmaster@semanticdesigns.com
