Posted By

stanosmith on 06/15/09


Tagged

parse file type extension


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

Pingpongbal
iamgowan


Determine File Extension


 / Published in: ActionScript 3
 

Works great for parsing links in XML documents and arrays.

  1. // Use an XML object or an Array. This array is just an example
  2. var _arrLinks:Array = ["testfiletype.txt", "http://www.domain.com/", "something.fdz", "another-file.rtf", "www.somedomain.com", "domain.com"];
  3. var _fileExt:String;
  4. var _strTmp:String;
  5. for (var i:uint = 0; i < _arrLinks.length; i++)
  6. {
  7. _strTmp = _arrLinks[i];
  8. _fileExt = _strTmp.substr(0, 4);
  9.  
  10. if (_fileExt == "www.") _fileExt = "www";
  11. else if (_fileExt != "http") _fileExt = _strTmp.substr(_strTmp.lastIndexOf('.') + 1, _strTmp.length);
  12.  
  13. switch (_fileExt)
  14. {
  15. case "swf":
  16. trace("This is a SWF file: " + _strTmp);
  17. break;
  18. case "txt":
  19. trace("This is a TXT file: " + _strTmp);
  20. break;
  21. case "http":
  22. trace("This is an HTTP link: " + _strTmp);
  23. break;
  24. case "www":
  25. trace("This is a WWW link, convert to an HTTP link: http://" + _strTmp);
  26. break;
  27. case "com":
  28. trace("This is a dot com link, convert to an HTTP link: http://" + _strTmp);
  29. break;
  30. default:
  31. trace("Unrecognized link or file extension. Please add (" + _fileExt + ") to the switch statment or change your filename.");
  32. break;
  33. }
  34. }

Report this snippet  

You need to login to post a comment.