Humans, being what they are, sometimes want to do irresponsible things like take a holiday, and this often means a key person is unavailable to update web pages when it needs to be done. In these days of lean and flexible companies, few can afford to hire extra staff to cover such contingencies, so there’s often a need for a software system to cover for such absences.

This was just the case with a client of mine the other day: the website in question was publishing a weekly quiz, whose answers were published the next week. This meant someone had to be around on the day users were expecting to see the latest set of questions or answers, to upload the relevant PDF document to the site and add a link to it. Wouldn’t it be better to load all these PDFs in advance and for the site to know when to display the appropriate link?
You could store the start and stop dates for showing each link in a database, but this would add an extra step to the uploading task, and create the possibility of mismatches between database information and files uploaded or removed. Better still then for the site to discover date information from the filename of the PDF, then all the site owner has to do is name each file according to a certain convention, upload it to the right folder and, er, that’s it, the web server will automatically do the rest. Batches of files could be uploaded well in advance, and the webmaster need worry no further.
To achieve this functionality, the system needs to know when to start displaying a link, when to remove the link and a description for that link. We opted for an obvious filename convention that’s easy to remember; namely:
ddmmyyyy1ddmmyyyy2xxxxxxx.pdf
where ddmmyyyy1 is the day to display the link, ddmmyyyy2 is the day to remove it, and xxxxxxxx is the link description. For the web server to extract this information and act on it, we need to read the filename by accessing the filesystem object. I’ve shown you how to write this sort of code in ASP many times in previous columns, but this time I wanted to write it in ASP.NET 2 and, guess what, the method for accessing the file system has changed. Granted the change is for the better and enables greater access, but it requires a different way of addressing the problem that might be of interest to you.
In previous versions of ASP, you’d create a filesystemobject and reference those bits that you want with something like:
set fs = createobject(“scripting.filesystemobject”)
set dc = fs.drives
set mydrive = fs.getdrive(startdrive)
set myroot = fs.getfolder(startfolder)
set sf = myroot.subfolders
Then to examine the files, you’d need to do something like:
myFileName = f1.name
In ASP.NET 2, much more is possible with the filesystem:
Sub Page_Load(sender as Object, e as EventArgs)
Dim FName as System.IO.FileInfo
Dim FileTitle as string
Dim FilePath as string
Dim MyFileName as string
This next line is equivalent to createobject(“scripting.filesystemobject”)in ASP 3:
Dim dirInfo as New DirectoryInfo(Server.MapPath(FilePath))
As there’s more than one file in the folder, we need to look at each file object in the collection of type .PDF, thus:
For each FName in dirinfo.getfiles(“*.pdf”)
Now we need to extract the filename from the object and convert it to a string type variable so we can insert it into the HTML on our web page:
MyFileName = ctype(FName.name,string)
The rest of the code will depend on how you want things to look on your web page, but if, for example, you had a label on the page you could do MyLabel.text = MyFileName to display the filename.
Disclaimer: Some pages on this site may include an affiliate link. This does not effect our editorial in any way.