home *** CD-ROM | disk | FTP | other *** search
/ PC User 2001 August / APC_Aug2001_CD2.iso / features / web_dev / files / mwjpp516.exe / %MAINDIR% / Tools / Misc / replaceInFiles.script < prev    next >
Encoding:
Text File  |  2001-06-19  |  2.0 KB  |  76 lines

  1. !!script
  2. // Copyright ⌐ 1997-1999 - Modelworks Software
  3. // @Modified build 287 cm19990410 - added support for the new Replace In Files dialog
  4.  
  5. /**
  6. @Tool: replaceInFiles~replaces a findWhat expression with a replaceWith 
  7. value in all files included in the selected directory. All changes made to 
  8. documents are listed in the output panel. 
  9. @EndTool: 
  10. @Summary: replaceInFiles~does a search and replace in a selected directory 
  11. */
  12.  
  13. var gOutput = getOutput();
  14.  
  15. function DoCommand() 
  16. {    
  17.     if (chooseReplaceInFilesParameters("Replace in files"))
  18.     {   
  19.         saveAll();
  20.  
  21.         var findParameters = getGlobal("FindParameters", null);
  22.  
  23.         gOutput.clear();
  24.         gOutput.writeLine("Searching for: '" + 
  25.             findParameters.findWhat + 
  26.             "', Replacing with: '" + findParameters.replaceWith + "'");
  27.  
  28.         var files = getDirectoryFiles(findParameters.searchPath, findParameters.fileTypes, 
  29.             findParameters.searchSubFolders);
  30.         if (files)
  31.         {
  32.             var findData = null;
  33.             var position = files.getHeadPosition();
  34.             while (position.valid)
  35.             {
  36.                 var file = files.getNext(position);
  37.                 var editor = file.open(false);
  38.                 if (editor)
  39.                 {
  40.                     gOutput.writeLine("Searching in: " + file.path);
  41.                     
  42.                     var lineIndex = -1;
  43.                     var fileName = editor.path;
  44.                     
  45.                     var findData = editor.findFirst(findParameters.findWhat, 0, 0, true, findParameters.caseSensitive, 
  46.                             findParameters.regularExpression, findParameters.replaceWith);
  47.                     
  48.                     while (findData && findData.found)
  49.                     {
  50.                         editor.replace(findData.getReplaceValue(), findData);
  51.                         
  52.                         var range = findData.range;
  53.                         
  54.                         // Only print out one message per line
  55.                         if (lineIndex != range.startLineIndex)
  56.                         {
  57.                             lineIndex = range.startLineIndex;
  58.                             gOutput.writeMessage(fileName, lineIndex, editor.copy(lineIndex));
  59.                         }
  60.                         
  61.                         editor.findNext(findData);
  62.                     }
  63.                     if (!editor.visible)
  64.                     {
  65.                         editor.close();
  66.                     }
  67.                 }
  68.             }
  69.         }
  70.         gOutput.writeLine("Replace in directory completed");
  71.     }
  72. }
  73.  
  74. !!/script
  75.  
  76.