AIR file search and replace


/ Published in: ActionScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. // FileStream object used to open the file
  2. var _fs : FileStream;
  3. // RegExp to search for the characters tag
  4. var _pickRegExp : RegExp = /<characters>(.*)<\/characters>/g;
  5. // Array used to store the strings found
  6. var tempArray : Array = [];
  7.  
  8. // loop through all the files in the Array
  9. for each (var $file : File in $fileArr)
  10. {
  11. // close the FileStream if it exists
  12. if (_fs != null) _fs.close();
  13.  
  14. _fs = new FileStream();
  15.  
  16. // open the file and read it's data, then close it again
  17. _fs.open($file, FileMode.READ);
  18. var data : String = _fs.readUTFBytes(_fs.bytesAvailable);
  19. _fs.close();
  20.  
  21. // search through the data using the RegExp
  22. var result : Object = _pickRegExp.exec(data);
  23.  
  24. // as long as there are results found the RegExp keeps searching
  25. while (result != null)
  26. {
  27. tempArray.push(result[1]);
  28. result = _pickRegExp.exec(data);
  29. }
  30. }
  31. }
  32.  
  33. // returns a clean Array, cleared of all duplicates
  34. return clearDuplicates(tempArray);
  35.  
  36. // function used to clear all duplicates from the array using the Dictionary Class (http://livedocs.adobe.com/Flash/9.0/ActionScriptLangRefV3/Flash/utils/Dictionary.html)
  37. function clearDuplicates($arr : Array) : Array
  38. {
  39. var dict : Dictionary = new Dictionary(true);
  40. var output : Array = new Array();
  41. var item : *;
  42. var total : int = $arr.length;
  43. var pointer : int = 0;
  44. for (pointer; pointer < total ; pointer++)
  45. {
  46. item = $arr[pointer];
  47. if (dict[item] == undefined)
  48. {
  49. output.push(item);
  50. dict[item] = true;
  51. }
  52. }
  53.  
  54. output.sort();
  55.  
  56. return output;
  57. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.