Saturday, March 28, 2009

getFileType

Last week I showed you how to get the size of a file. This week I'm going to show you how to get the type of a file. This is useful if you want to display an icon or bit of text next to a hyperlink to indicate the file type. Some people like to use this to warn visitors when they link to a PDF file, because PDF readers can take a while to load.


ASP

  1. function getFileType(someFile)
  2.     dim fs
  3.     set fs = Server.CreateObject("Scripting.FileSystemObject")
  4.     getFileType = fs.GetExtensionName(Server.MapPath(someFile))
  5.     set fs = nothing
  6. end function

See you next week!


View ASP implementation on Snipplr

Saturday, March 21, 2009

getFileSize

When you provide a link to a file for the user to download, it's sometimes nice to provide the size of the file so that they know what they're getting themselves into.


ASP

  1. function getFileSize(someFile)
  2.     dim fs
  3.     dim file
  4.     set fs = Server.CreateObject("Scripting.FileSystemObject")
  5.     set file = fs.GetFile(Server.MapPath(someFile))
  6.     getFileSize = FormatFileSize(file.size)
  7.     set file = nothing
  8.     set fs = nothing
  9. end function

The size property returns the size in bytes, which could be a very large number. As you may have guessed, we're going to write a FormatFileSize function to wrap around it.


ASP

  1. function FormatFileSize(size)
  2.     dim units
  3.     dim factor
  4.     units = Array("B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
  5.     factor = log(size) \ 7
  6.     FormatFileSize = Round(size / (1024 ^ factor), 2) & units(factor)
  7. end function

I think it will be a very, very long time before we reach yottabytes, so this function should withstand the test of time. Next week I'll show you how to get the file type and why you might want that.


View ASP implementation on Snipplr

Saturday, March 14, 2009

Soundex

This week we're going to write a function to convert letters to their corresponding soundex codes. But wait, there's more! We'll also allow you to pass in an entire string and convert the whole thing. Here's the set up:


  1. function soundex(someString)
  2.     if len(someString) = 1 then
  3.         ' code to convert a single character
  4.     else
  5.         ' loop through the whole string and convert each character
  6.     end if
  7. end function

Inside the loop, we'll recursively call the soundex function to convert the individual character. Actually, this may not perfectly fit the definition of recursion. The only recursive aspect of it is that the function calls itself, but an entirely different execution path is being followed once inside. But that's good, because we won't have the poor performance that sometimes comes with recursion.


Once again, the full source code is a little too long to post, so you'll have to grab it from Snipplr:


Saturday, March 7, 2009

Luhn Algorithm

This week we're writing a function to verify credit card numbers. Credit cards have a check digit which is generated with the Luhn algorithm. The code is too long to post here, but as always it is posted on Snipplr and linked here for your dissemination.