How to run a script on all files in a specified folder

How to run a script on all files in a specified folder

1

    Feb 17, 2012#1

    Hello I've got this script working

    Code: Select all

    // Retour en haut du document
    var abc = "test";
    var def = "toto";
    UltraEdit.activeDocument.top();
    UltraEdit.activeDocument.findReplace.replace(abc, def);
    UltraEdit.activeDocument.top();
    var abc = "toto";
    var def = "tutu";
    UltraEdit.activeDocument.findReplace.replace(abc, def);
    But now I would like to know how I can launch this script for all files with a specific file extension in a specified folder?

    Thank you so much

    6,683583
    Grand MasterGrand Master
    6,683583

      Feb 18, 2012#2

      I suppose, your script is different to what you have posted because this script does not really make sense.

      There is the Replace in Files command to replace a string in many files in a directory or even directory tree. This command is also available as scripting command if this replace in files must be done in more or less regular intervals and not only once.

      If the files contain the string to replace more than once and just the first occurrence should be replaced in every file, it could be nevertheless possible to use a tagged Perl regular expression replace to replace just first occurrence, if the files are small, i.h. < 32 KB.

      But if you really need to run a script code on all *.php files within a folder, you need my script function GetListOfFiles copied into your script and additionally following script code:

      Code: Select all

      if (GetListOfFiles(0,"C:\\Temp\\","*.php",false))
      {
         // Select the list of file names and load them into an array of strings.
         UltraEdit.activeDocument.selectAll();
         var asFiles = UltraEdit.activeDocument.selection.split("\r\n");
         // Remove the last string from the array which is an empty string
         // because of the line termination at end of the file names list.
         asFiles.pop();
         // Close the Find in Files results file without saving it.
         UltraEdit.closeFile(UltraEdit.activeDocument.path,2);
      
         // Open one file after the other for the modification.
         for (var nFile = 0; nFile < asFiles.length; nFile++)
         {
            // Open the next file to modify.
            UltraEdit.open(asFiles[nFile]);
      
            // Insert here your script code to modify current file.
      
            // Close the file with saving the modification.
            UltraEdit.closeFile(UltraEdit.activeDocument.path,1);
         }
      }
      There are other methods, but this is the best one if the script has to do it several times. The path of the directory must be adapted in the first line of this code.

      Attention: None of the *.php files in the specified directory should be already opened in UltraEdit on execution of this script because in this case open command does nothing and the current file is modified, saved and closed. I don't have added the code for checking if a file to open is already opened in UltraEdit which would make this script slower.

      And don't forget to read the comments of the function GetListOfFiles which must be embedded, especially when not using English version of UltraEdit because in this case 2 string variable values must be adapted to your localized version of UltraEdit in code of GetListOfFiles.