Close all files that match a filter/mask

Close all files that match a filter/mask

23
Basic UserBasic User
23

    Aug 16, 2010#1

    I'd like a script that prompts me for a file mask and then closes all the files currently open in the editor that match that mask.

    E.g. "*.txt" would close all the .txt files open in the editor.

    Thanks!

    6,603548
    Grand MasterGrand Master
    6,603548

      Aug 17, 2010#2

      Do you need that script only for closing files with a specified file extension or do you want to be able to use it also with general masks?

      Just closing all files with a specified file extension is simple. But when the script should support general masks like C:\Temp\*\Test_??_*.c* it is quite more difficult to code this script, whoever will do that for you.

      Please be more detailed on what you want. Post a list of file names and below some masks you want to use and which files should be closed by these masks from the list. The better you define the requirements for the script the faster you can expect a script really working for you and the less time we need to code that script.
      Best regards from an UC/UE/UES for Windows user from Austria

      23
      Basic UserBasic User
      23

        Aug 17, 2010#3

        Just simple ones like *.txt or *.asp

        6,603548
        Grand MasterGrand Master
        6,603548

          Aug 17, 2010#4

          Here is a quick written script which closes all files with the same extension as entered by the user. You have to insert in the script file additionally the source code of function GetFileExt.

          Code: Select all

          var nFileCount = UltraEdit.document.length;
          // Is at least 1 file open?
          if (nFileCount > 0) {
             // Let the user enter the file extension string.
             var sExtension = UltraEdit.getString("Enter file extension",1);
             // The user should enter just the file extension, for example
             // just TXT. In case there is something left the file extension
             // like "*." or just "." remove that part of the entered string.
             var nPointIndex = sExtension.lastIndexOf('.');
             if (nPointIndex >= 0) sExtension = sExtension.substr(++nPointIndex);
             // Normally it makes sense to run the file close loop only when the
             // remaining string is not empty now.
             if (sExtension.length) {
                // Convert the file extension to lower case.
                sExtension = sExtension.toLowerCase();
                // Run the file closing loop from last to first. Running it in reverse
                // direction is important whenever a loop is executed on a dynamic
                // list which removes items from the list. Because removing an item
                // from the list results in moving everything with a higher index
                // down in the list, removing items from first to last would need to
                // permanently check the modified list count property and the index
                // can be increased only when an item is not removed. Running the
                // loop in reverse order from last to first avoids all that problems
                // and the loop is executed much faster.
                var nFileIndex = nFileCount;
                do {
                   var sActFileExtension = GetFileExt(--nFileIndex);
                   if (sExtension == sActFileExtension.toLowerCase()) {
                      UltraEdit.closeFile(UltraEdit.document[nFileIndex].path,0);
                   }
                }
                while (nFileIndex);
             }
          }
          A similar solution with using isExt() instead of GetFileExt():

          Code: Select all

          var nFileCount = UltraEdit.document.length;
          // Is at least 1 file open?
          if (nFileCount > 0) {
             // Let the user enter the file extension string.
             var sExtension = UltraEdit.getString("Enter file extension",1);
             // The user should enter just the file extension, for example
             // just TXT. In case there is something left the file extension
             // like "*." or just "." remove that part of the entered string.
             var nPointIndex = sExtension.lastIndexOf('.');
             if (nPointIndex >= 0) sExtension = sExtension.substr(++nPointIndex);
             // Normally it makes sense to run the file close loop only when the
             // remaining string is not empty now.
             if (sExtension.length) {
                // Run the file closing loop from last to first. Running it in reverse
                // direction is important whenever a loop is executed on a dynamic
                // list which removes items from the list. Because removing an item
                // from the list results in moving everything with a higher index
                // down in the list, removing items from first to last would need to
                // permanently check the modified list count property and the index
                // can be increased only when an item is not removed. Running the
                // loop in reverse order from last to first avoids all that problems
                // and the loop is executed much faster.
                var nFileIndex = nFileCount;
                do
                   if (UltraEdit.document[--nFileIndex].isExt(sExtension)) {
                      UltraEdit.closeFile(UltraEdit.document[nFileIndex].path,0);
                   }
                }
                while (nFileIndex);
             }
          }
          Best regards from an UC/UE/UES for Windows user from Austria