When Written: Sept 2006
Humans being what they are often want to do irresponsible things like take holidays! Often this means that a key person is unavailable to update web pages when this needs to be done. Long gone are the days of companies being able to afford extra staff to cover such occurrences. In these days of lean and flexible companies, there is often a need of a software solution to cover such contingencies.
This was just the case with a client the other day. The web site in question was publishing a weekly quiz with the answers being published the next week. This meant that someone had to be around on the day that the users were expecting to see the latest set of questions or answers, to upload the relevant pdf document, and add a link. Wouldn’t it be much better to be able to upload the pdfs in advance and for the web site to know when to show a link? Obviously you could store the information of when to start and when to stop showing the link in a database, but this would require an extra step to the uploading task and the chance that the database information may not match the files uploaded or removed.
How much better would it be for the web site to find this information from the file name of the pdf? If we managed to do this, all the web site owner would have to do would be make sure the file was named according to a convention that would be decided on and then upload the file to the right folder, and err that’s it! The web server would automatically do the rest. This way a batch of files could be uploaded well in advance and the web master would not have to worry about them. To achieve this functionality we need the system to know the date to display the link, the date to remove the link and a description for the link. So always opting for something obvious that others can remember, we opted for a file name layout of:
Where the first ddmmyyyy is the date to display the link and the second is the day the link should be removed, whilst the xxxxxxxx is the link description. Pretty logical I thought. For the web server to extract this information and to act on it we obviously need to read the file name, this is done by accessing the file system object. I have written this sort of code many times in ASP and shown how to do it in previous articles. This time, however, I wanted to write the code in ASP .NET 2 and guess what? The method for accessing the file system has changed, granted this enables a greater access to the filesystem and is for the better, but it still means a different way of addressing the problem. So I though it might be of interest to share my experiences with you.
With previous versions of ASP you would create a filesystem object and then reference the 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 would need to do something like:
myFileName = f1.name,
Things have changed with ASP .NET 2 and 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 the equivalent to createobject(“scripting.filesystemobject”)in ASP ver 3
Dim dirInfo as New DirectoryInfo(Server.MapPath(FilePath))
As there are more than one file in the folder we need to look at each file object in the collection which ends with .pdf
For each FName in dirinfo.getfiles(“*.pdf”)
We now need to exctract the file name from the object and convert it to a string type variable so that we can use it in the HTML on our web page
MyFileName = ctype(FName.name,string)
The rest of the code will depend on how you want things to display on your web page but if for example you had a label on the page you could do MyLable.text = MyFileName to display the file name. It is often little differences like this that can trip you up when moveing from ASP3 to ASP .NET and it can be surprising just how long it can take to get what you at first though would be simple code to work, just because your old way of doing things no longer works. The fact that an object type was returned when asking for a file name caused me some head scratching but thank goodness that there is decent debugging in Visual Web Developer 2005.
Article by: Mark Newton
Published in: Mark Newton