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 < so HTML displayed, not interpreted
s = s.replace ( /</g, '<' );
// replace newline with HTML equivalent
s = s.replace ( /\n/g, '<br>' );
// replace 2 spaces with non-breaking spaces
s = s.replace ( / /g, ' ' );
// replace tabs with 3 spaces
s = s.replace ( /\t/g, ' ' );
// 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!