CoverYourASP --> Displaying the contents of files on the server

Free membership

Join in the fun! Sign in
Member Services

Site navigation
Download the entire site!
Search my articles
Free Magazines
Browse the directory

Send me feedback
Buy my boxer shorts

Recommend this page
Printer-friendly page

Resources I recommend
Link to my site
Advertising slashed!
About your privacy
Legal stuff
Site statistics
87 active users
2111 visitors today
1882 pages today
(only part of today)
Tools I use

CoverYourASP
Copyright © 1999-2016 James Shaw.
All rights reserved.

ASP.NET Blog
RSS submissions
E-commerce

Now open source with SourceForge!

Quite often I need to show you the contents of a file on my server. Obviously being a ASP source site I may need to do this more than most sites, but it's a useful tutorial nonetheless.

Firstly, it demonstrates the use of the FileSystemObject, and secondly it shows how powerful regular expressions can be. I learnt all I know about regular expressions from Microsoft's online documentation and a lot of experimentation!

I created a function, ShowFile( ), which I keep in the utils/ShowFile.asp Server Side Include. Let's look at that function:

function ShowFile ( oFSO, sFile, bPassHTML, bShowName, bLiveLinks )
{
   var ForReading = 1;
   // var ForWriting = 2;
   // var ForAppending = 8;

   // open asp file for reading
   var fFile = oFSO.OpenTextFile ( Server.MapPath( sFile ), ForReading );

   // read entire file contents into variable
   var s = fFile.ReadAll ( );

   // close the file ASAP to free resources
   fFile.Close ( );

I pass in a FileSystemObject (oFSO, you'll see how I call it in a minute), a filename (sFile), and whether to display any embedded HTML or allow the browser to act upon it (bPassHTML). The next parameter, bShowName, defines whether the filename is displayed, and bLiveLinks specifies if it should convert any http://... strings into live hyperlinks.

The first thing I do is to open the file and read in the entire contents of the file into the variable s.

I then close the file. This is important! Remember to always close as soon as possible to conserve precious server resources. This applies to files, objects, database connections - anything and everything you create or open...

if ( !bPassHTML )
{
   // replace & with & so HTML displayed, not interpreted
   s = s.replace ( /&/g, '&' );

   // replace < with &lt; so HTML displayed, not interpreted
   s = s.replace ( /</g, '&lt;' );

   // replace newline with HTML equivalent
   s = s.replace ( /\n/g, '<br>' );

   // replace 2 spaces with non-breaking spaces
   s = s.replace ( /  /g, '&nbsp;&nbsp;' );

   // replace tabs with 3 spaces
   s = s.replace ( /\t/g, '&nbsp;&nbsp;&nbsp;' );

   // find hyperlinks
   if ( bLiveLinks )
      s = s.replace ( /(http\:\S*)/gi, '<a href="$1" target="CYAExternal">$1</a>' );

Now comes the fun part. If the HTML is to be displayed to you, i.e. you want to see <b>hello</b>, not just hello, I find and replace certain characters in the buffer.

The order that you do these replacements is important. The latest one I added recently was to make any hyperlinks in the buffer "live" - very cool when displaying my newsletter archive that contains many "http:...".

   // change font color for source code
   s = '<font color="black">' + s + '</font>';

   // show filename if wanted
   if ( bShowName )
      s = '<h4>' + sFile + '</h4>' + s;
}

Next, a <font> tag is wrapped around the buffer to display all the text in black, and the filename added to the top if needed.

   // output results
   Response.Write ( s );
}

All that's left then is to output the buffer to your browser. Below is an example of how to call the function - see how the FilesystemObject is opened once, and the object passed into ShowFile( ). This is so that you can create the object once and use it to display many files.

// create handle to FileSystemObject
var oFSO = Server.CreateObject ( 'Scripting.FileSystemObject' );

ShowSource ( oFSO, 'global.asa', true, false );

Remember, you can download all of my source code, including this handy SSI and many others!

Featured sponsor
My favorite resources


I share my content

Supporting ASPRSS

Do you need a quick and easy way to link to my articles? All the information you need is published with ASPRSS...


New Proposal Kit Professional 5.1
Brand yourself as a top professional: create quotes and amazing proposals and get many legal documents free!

The latter saved me 3 times the purchase price on the first day I owned it!


Qualify for Free Trade Magazines

Free subscriptions to industry leading publications for those who qualify!


See my source code
wherever you see this icon...

You can also download the entire site source code for FREE!