1 Reply Latest reply on Oct 16, 2008 6:43 PM by abeall

    Validate file name

    abeall Level 3
      How can I validate a filename as being valid before trying to write the file?

      Somehow I created a directory on WinXP which cannot be deleted. Every time I try to delete it or rename it via Windows says it can't find the file. Same results from the command prompt. I noticed that the dir name has a lot of trailing whitespace (filenames are generated from user input) which seems like it might be related. At the very least, I don't want this to happen to end users. But I also want to delete this directory if anyone has any ideas!
        • 1. Re: Validate file name
          abeall Level 3
          Okay, I finally deleted the folder by using the short name in the command prompt, successfully renaming, then deleting:
          http://www.spywareinfoforum.com/index.php?s=ab906deca00031316f403ac629a7145a&showtopic=743 00&view=findpost&p=405006

          Consider it a feature request to not let AIR make files like this!

          Here's my new filename validation function. Please advise if it's sufficient:

          public static const ILLEGAL_FILENAME_CHARACTERS:Array = ['/', '\\', ':', '*', '?', '"', '<', '>', '|', '%','\n','\r'];
          public static function makeValidFilename(filename:String):String{
          for each(var i:String in ILLEGAL_FILENAME_CHARACTERS)
          filename = filename.split(i).join('');
          return trim(filename);
          }
          public static function trim(str:String):String{
          while(str.charAt(0)==' ')
          str = str.substr(1);
          while(str.charAt(str.length-1)==' ')
          str = str.substr(0,-1);
          return str;
          }