Demo-FileSystemObject

FileSystemObject in Action:

Current path = ...\cse686\code\AspScripts
number of files = 13

Fully qualified path names:
C:\inetpub\wwwroot\sites\ecs\faculty\fawcett\handouts\CSE686\code\AspScripts\AspScripts.sln
C:\inetpub\wwwroot\sites\ecs\faculty\fawcett\handouts\CSE686\code\AspScripts\AspScripts.suo
C:\inetpub\wwwroot\sites\ecs\faculty\fawcett\handouts\CSE686\code\AspScripts\AspScripts.zip
C:\inetpub\wwwroot\sites\ecs\faculty\fawcett\handouts\CSE686\code\AspScripts\CoverSheet-DemoFileSystemObject.doc
C:\inetpub\wwwroot\sites\ecs\faculty\fawcett\handouts\CSE686\code\AspScripts\CoverSheet-DemoResponse.doc
C:\inetpub\wwwroot\sites\ecs\faculty\fawcett\handouts\CSE686\code\AspScripts\CoverSheet-ShowFile.doc
C:\inetpub\wwwroot\sites\ecs\faculty\fawcett\handouts\CSE686\code\AspScripts\Demo-FileSystemObject.asp
C:\inetpub\wwwroot\sites\ecs\faculty\fawcett\handouts\CSE686\code\AspScripts\Demo-Response.asp
C:\inetpub\wwwroot\sites\ecs\faculty\fawcett\handouts\CSE686\code\AspScripts\DemoResponse.asp
C:\inetpub\wwwroot\sites\ecs\faculty\fawcett\handouts\CSE686\code\AspScripts\ScreenShot-DemoFileSystemObject.doc
C:\inetpub\wwwroot\sites\ecs\faculty\fawcett\handouts\CSE686\code\AspScripts\ScreenShot-DemoResponse.doc
C:\inetpub\wwwroot\sites\ecs\faculty\fawcett\handouts\CSE686\code\AspScripts\ScreenShot-ShowFile.doc
C:\inetpub\wwwroot\sites\ecs\faculty\fawcett\handouts\CSE686\code\AspScripts\ShowFile.asp

Just file names:
AspScripts.sln
AspScripts.suo
AspScripts.zip
CoverSheet-DemoFileSystemObject.doc
CoverSheet-DemoResponse.doc
CoverSheet-ShowFile.doc
Demo-FileSystemObject.asp
Demo-Response.asp
DemoResponse.asp
ScreenShot-DemoFileSystemObject.doc
ScreenShot-DemoResponse.doc
ScreenShot-ShowFile.doc
ShowFile.asp

Text of last asp file is:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@LANGUAGE="JavaScript"%>
<!--
***********************************************************************
* ShowFile.asp - Use select as listbox, pick file and display
*
* Jim Fawcett, CSE686 - Internet Programming, Summer 2009
***********************************************************************
-->
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Show File</title>
<style type="text/css">
body { font-family:Tahoma; }
div.list { background-color:#F0F0F0; margin:1em; padding:0.5em; }
div.submitter { text-align:center; }
h1.title { text-align:center; color:Maroon; }
#ListBox { width:100%; }
#FileText { border:solid 2px black; margin:1em; padding:0.5em; }
</style>
<% /* script section here acts like constructor when page is built */

var FSO = Server.CreateObject("Scripting.FileSystemObject");
var listBox = null;
Response.CharSet="windows-1252";

if(Request.TotalBytes > 0) { /* this is postback */
listBox = Request.Form("ListBox"); /* gets selection */
}
else { /* initial get request */
listBox = null;
}
/*----< insert filename in select listbox >------------------*/

function addToList(name) {
Response.Write("<option>" + name + "</option>");
}
/*----< collect names of all files in virtual directory >----*/

function fileList() {
var Path = Server.MapPath(".");
var Folder = FSO.GetFolder(Path);
return Folder.Files;
}
/*----< show file contents on page below listbox >-----------*/

function displayFileText(file) {
if(listBox == null)
return;

var file = FSO.OpenTextFile(file);
Response.Write("<pre>");
while(!file.AtEndOfStream)
{
var string = file.ReadLine();

/* replace markup with entity characters */
string = string.replace(new RegExp('<','g'),'<');
string = string.replace(new RegExp('>','g'),'>');
Response.Write(string + "<br />");
}
Response.Write("</pre><p />");
file.Close();
}
%>
<script type="text/javascript" language="javascript">

/* note: runs on client */
/*----< submit form from JavaScript event >------------------*/

function submit() {
document.forms[0].submit(); // same as clicking submit
}
</script>
</head>
<body>
<h1 class="title">Display Virtual Directory File Contents</h1>

<form id="Form" name="Form" action="ShowFile.asp" method="post">

<!-- listbox -->
<div class="list">
<select id="ListBox" name="ListBox" size="12" runat="server" ondblclick="submit()" width="100%">
<% /* add select options, e.g., entries in listbox */
var files = fileList();
for(var enumer = new Enumerator(files); !enumer.atEnd(); enumer.moveNext())
{
var string = new String(enumer.item());

/* strip off path prefix */
var pos = string.lastIndexOf("\\") + 1;
string = string.substr(pos);

addToList(string);
}
%>
</select>
</div>

<!-- submit button -->
<div class="submitter">
<input type="submit" value="submit" />
</div>

<!-- area to display files -->
<div id="FileText">
<%
if(listBox != null) {
var path = Server.MapPath(".");
var file = path + "\\" + listBox;
Response.Write(file);
displayFileText(file);
}
else
Response.Write("no selection");
%>
</div>
</form>
</body>
</html>