2010
05.02

A handy Windows 32 File name validator, recently I have been working with media files being saved from a Flex app and creating directories on windows machines for export for some Avid use, Windows has some funny constrains for file names, some examples include using the words [COM/n, CLOCK, CON], these are all included in the follwoing custom Validator with the usual ‘[$, *, etc]‘, hope it is useful to someone out in the wild.

The basic RegExp of the validator

internal function validateWin32( target:String ):Boolean
		{
			//Check name is not blank
			if( target.length < 1 ) {
 
				return false;
			}
			// Leading, trailing space, dot
			if (target.charAt( 0 ) == "." || target.charAt( 0 ) == " " ||
				target.charAt( target.length ) == "." || target.charAt( target.length ) == " ") {
				return false;
			}
			// Multiple spaces
			if ( matchPart( target, "\\s\\s+" ) ) {
 
				return false;
			}
			// Explicitly illegal Windows characters
			if ( matchPart( target, "[$*\"/\\\\\\[\\]:;|=,]" ) ) {
 
				return false;
			}
			// Control characters
			if ( matchPart( target, "[\\x00-\\x1f]" ) ) {
 
				return false;
			}
			// Reserved names. DOS compatibility means that AUX.txt
			// is just as bad as AUX.
			if ( matchPart( target, "^(?i:CON|PRN|AUX|CLOCK\$|NUL|COM\\d|LPT\\d)(?:\\..*)?$" ) ) {
 
				return false;
			}
			return true;
		}

The Flash plugin is required to view this object.

View Source

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Blogplay

3 comments so far

Add Your Comment
  1. Nice information, I really appreciate the way you presented.Thanks for sharing..

    Like or Dislike: Thumb up 1 Thumb down 0

  2. matchPart? What is that?

    Like or Dislike: Thumb up 0 Thumb down 0

  3. you can view the source to see the method, it is simply a regEx verify method

    private function matchPart( target:String, patternString:String ):Boolean
    {
    var pattern:RegExp = new RegExp( patternString );

    return pattern.exec(target);
    }

    Like or Dislike: Thumb up 0 Thumb down 0