h1

Links for 2006-07-18

July 18th, 2006

Tag:

h1

Links for 2006-05-25

May 25th, 2006

Tag:

h1

Links for 2006-05-24

May 24th, 2006

Tag:

h1

Links for 2006-05-23

May 23rd, 2006

Tag:

h1

Sanitize File Names Using VBScript

May 18th, 2006

We use an ASP based form to enable our customers to submit files created as part of a registration process. During the submission, the file is sent as an email attachment to our support staff, and a copy is written to a file server as a backup. The application uses a standard Win32 Save dialog that suggests a default filename. In nearly every case, the file is submitted by the customer without changing the default name. If the email process fails, retrieving the backup copy is difficult at best.

I modified the form to create a unique filename using values from certain form elements. Since these values can, and often do, use characters that are invalid as part of a filename, I needed a way to “sanitize” the name during the submission process. I wrote the following VBScript code to replace these characters before attempting to save the file.


   Function SanitizeFilename(byVal strFilename, byVal strReplChar)

      Set objRegExp = New RegExp ' Create new RegExp object

      ' Define regex pattern and set Global replacement property
      objRegExp.Pattern = "[\x00-\x1f\x22\\\/:\*\?<>\|]"
      objRegExp.Global = True

      ' Check strReplChar parameter for invalid length or character,
      ' default to underscore
      If Len(strReplChar) = 1 Then 
         strReplChar = objRegExp.Replace(strReplChar, "_")
      Else 
         strReplChar = "_"
      End If

      ' Return clean filename
      SanitizeFilename = objRegExp.Replace(strFilename, strReplChar)

      Set objRegExp = Nothing
   End Function

This function accepts two arguments, strFilename which receives the string created from the form values, and strReplChar which receives the character used for replacement. The function uses a regular expression pattern to match the invalid characters (control characters through 31, double-quotes, and a handful of others). There is a check to make sure that the strReplChar argument is valid, but not much else. The RegExp Object member Replace does the heavy lifting, before the function returns the modified string.

The regex pattern replaces invalid characters for Windows NTFS (generally), but could easily be altered for other OS file systems. You could also use a more restrictive regular expression like:


      [^\w\x20\&%'`\-\@{}~!#\(\)&_\^\+,\.=\[\]]

Then again, that may be a bit much. Wikipedia has a good filename reference that includes a fairly comprehensive list of reserved words and allowable character sets. As always, if you need a good regex reference, try Regular-Expressions.info.

UPDATE: The logic in my if/then statement to check the length and validity of the strReplChar was backwards. The example has been corrected.

Tags: , ,

h1

Links for 2006-05-10

May 10th, 2006

Tag:

h1

Internet Closed for Cleaning?

May 5th, 2006

According to a message sent from the Department of Homeland Security dated March 29, 2006, and posted here, the Internet recently had to be closed for cleaning. According to the email,

...the internet will be shut down for cleaning for twenty-four hours from midnight on March 31 through the early hours of April 2. This cleaning is necessary to clear out the “electronic flotsam and jetsam” that has accumulated in the network.

It’s a wonder my blog is still here.

Tags: , , ,

h1

Gmail Service Outage?

May 4th, 2006

Anyone else seeing this? I’ve been getting either 404 Not Found or 503 Service Unavailable errors when trying to access my Gmail account. I first noticed this about 1:50pm PDT (GMT -8), but suspect it’s been going on a bit longer. What’s up?

Funny, I was just thinking I need to switch to Gmail exclusively. Hmmm.

UPDATE: As quick as I saved this post, Gmail came back online (2:38pm PDT). It wasn’t just me, posts in the Gmail-Users Group seem to confirm the outage.

Tags: , , ,

h1

Links for 2006-05-03

May 3rd, 2006

Tag:

h1

Links for 2006-05-02

May 2nd, 2006

Tag: