ASP/.NET Web Hosting
* 3 Months FREE *
CLICK HERE!
 CoverYourASP --> Displaying the newsletter archives --> Part 2

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
314 active users
1798 visitors today
2701 pages today
how is this done?
ASP.NET Showcase
ASP.NET Graph and Chart Component with FREE developer version

ASP.NET Shopping Cart and E-commerce Control with FREE trial download

ASP.NET Web Hosting with free month and setup

$15 Domain Name Registration from .DNR.
As seen on these sites
Special thanks go to the sites that support me by publishing my articles.
See more

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

ASP.NET Blog
ASPRSS
ShawThing

Now open source with SourceForge!

Getting the list of newsletters

Luckily I already had the perfect way to get a list of files in a folder or sub-folders, as described in my earlier article about the WalkFolders( ) function.

The function works by walking through the files and folders on the server, passing what it finds into the functions that are passed into WalkFolders as parameters. Easily extensible as they say.

// create array to hold dates of newsletter
var sDates = new Array;
var nTotal;

[code snipped for brevity]

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

// search the archive folder
var oFolder = oFSO.GetFolder ( Server.MapPath ( 'Newsletters' ) );

// start walking through the files and sub-folders
WalkFolders ( oFolder, 0, DisplayArchiveFolder, DisplayArchiveName );

I first declare two global variables to store the list of newsletters found, and the total number of them. Then I create an instance of the FileSystemObject, and get a Folder object relating to the Newsletters sub-folder.

Note: Server.MapPath is used to translate a relative path to an absolute path on the server

Then WalkFolders is called, passing in the Folder object and the two functions that I will use to collect the file and folders that are found. Those functions are shown below:

// ============================================
// don't need to display archive folder
// ============================================
function DisplayArchiveFolder ( oFolder, nLevel )
{
}

// ============================================
// display file name with link to display page
// ============================================
function DisplayArchiveName ( oFile, nLevel )
{
   var sName = oFile.Name;

   // skip test files (start with _), and non-text files
   if ( sName.charAt ( 0 ) == '_' || -1 == sName.indexOf ( '.txt' ) )
      return;

   // remove extension
   sName = sName.slice ( 0, sName.indexOf ( '.' ) );

   // add date to array, for sorting later
   sDates [ nTotal++ ] = sName;

   // NOTE: in JScript 5.5 use sDates.push ( sName )
}

I don't care about any folders that might be found, so I just supply an empty function for sending folder information.

DisplayArchiveName gets passed a File object. First, as always, I ignore any files that start with '_' just in case I want to ever have any private files in the folder. I also ignore any files that don't have a 'txt' extension.

Next, I remove the file extension with slice, and add the filename to the sDates array, incrementing nTotal at the same time.

Note: As commented in the code above, you could dispense with nTotal altogether, and simply use sDates.push to add a new element to the array. Unfortunately, that's only available in JScript 5.5, which my server doesn't have yet. My development machine does, but the real one doesn't. Go figure!

Now to throw the first "spanner in the works" as we English people say. For obvious reasons, I don't want to do this every time someone goes to view my archives. The answer is of course Application variables...

Part 3: Caching the archive names...

Featured sponsor
My favorite resources

Selling products online - read how James does it now



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

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


Qualify for Free Trade Magazines

Free subscriptions to industry leading publications for those who qualify!


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!


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...