Function to get list of files into an edit window

Function to get list of files into an edit window

6,675585
Grand MasterGrand Master
6,675585

    Oct 24, 2007#1

    Hello script writers!

    Often a script should be executed on a list of files and therefore it would be helpful to have a function which creates such a file list. Then one file after the other can be opened, modified, saved and closed until all files in the list have been processed. Here is the function to get such a file list.

    If you want to report mistakes or have suggestions for further enhancements post a message here.

    The script file GetListOfFiles.js with the code can be viewed or downloaded from the Macros & Scripts page. The script file was last updated on 2018-06-24.

    Important!
    Users of UltraEdit / UEStudio not using the English version have to adapt two strings at the beginning of the function to their localized version of UE/UES. See the comments at top of the script or above the two string variables sSummaryInfo and sResultsDocTitle for details.
    And users of UltraEdit for Windows < v24.00 or UEStudio < v17.00 should change the value of variable bNoUnicode to value true at top of function.

    find_results_window.png (812Bytes)
    Screenshot of the file tab of the document with the Find in Files results.

    set_find_output_format.png (4.03KiB)
    Screenshot of the dialog to configure output format of Find in Files command.

    The attached image GetListOfFiles_Configuration.png shows even more detailed how the two string variables sSummaryInfo and sResultsDocTitle inside function GetListOfFiles must be configured by the user according to language of used UltraEdit/UEStudio and user's configuration for find summary. This image shows four typical configurations:
    1. English UltraEdit is used with default configuration for find summary.
    2. English UltraEdit is used with find summary not used at all.
    3. English UltraEdit is used with beginning of find summary text modified by the user in configuration.
    4. German UltraEdit is used with default configuration for find summary.
    The line and block comments can be removed from function GetListOfFiles by running a replace all (from top of file) searching with Perl regular expression for ^ *//.+[\r\n]+|^ */\*[\s\S]+?\*/[\r\n]+| +//.+$ and using an empty replace string. The first part in this OR expression with three arguments matches entire lines containing only a line comment, the second part matches block comments, and third part matches line comments right to code. Removal of the comments makes the usage of this function in other scripts more efficient because of JavaScript interpreter has to interpret less characters and lines.
    GetListOfFiles_Configuration.png (295.09KiB)
    Examples for sSummaryInfo and sResultsDocTitle depending on language of UE/UES and user's find summary configuration.

      Feb 11, 2009#2

      The script file with the function GetListOfFiles was updated on 2009-02-10.

      I changed slightly all variable names by adding a prefix letter for the type of the variable. Advantages of using a type prefix letter:
      • The type of a variable is always visible which is a great help.
      • No problem anymore with variable names similar common words used in comments or strings, especially for searching/replacing such variables. For example sDirectory as variable name is much better than just Directory because word Directory can also exist in comments and strings.
      • It is easy to search for all string, number or boolean variables if using a type prefix letter (always lowercase) and the variable name starts with an uppercase character. For example the case sensitive regular expression search string s[A-Z][A-Za-z]+ with option Match Whole Word Only finds all string variables in the script file.
      • Using type prefix letters makes the selection of an existing variable of type string, number or boolean easier in the auto-complete dialog.
      Additionally the global variable used is now named g_nDebugMessage. g_ as additional prefix defines that this variable is a global variable not defined inside the function. Variables with a non standard type don't have a prefix, but have a very special name.

      The standard prefixes I use are:

      an ... array of numbers (doesn't exist in GetListOfFiles.js)
      as ... array of strings (doesn't exist in GetListOfFiles.js)
      b ... boolean
      n ... number
      s ... string


      Further I fixed a wrong type verification of input variable nFileList at top of the function resulting in running the function always with value 0 for the first input parameter (= find files in a specified directory or directory tree).

      The next small mistake was that there was only 1 error message if no file could be found. This error message was correct only for running the function to get a list of files in a specified directory or directory tree. I added now 4 additional error messages for that case depending on the input parameter nFileList.

      Last I added a code for demonstrating the usage of the function. So it is now possible to simply run the script which asks the user which file list shall be created and the optional parameters depending on the first choice for the type of the file list.
      Best regards from an UC/UE/UES for Windows user from Austria

      11
      Basic UserBasic User
      11

        Jun 01, 2009#3

        Mofi,

        I just downloaded and used your GetListOfFiles.js script. It rocks! I don't know why I haven't downloaded it before.

        Thank you for this script and all the other help you give in the forums! 8)
        Two rules of development:
        1) Computers work for people. People do not work for computers.
        2) Maintainability is all that matters.

        6,675585
        Grand MasterGrand Master
        6,675585

          Oct 22, 2012#4

          I updated the function GetListOfFiles on 2012-10-16. It is possible with this update to use this function with just modifying the line

          Code: Select all

            var sSummaryInfo = "Search complete, found";
          to

          Code: Select all

            var sSummaryInfo = "";
          for configurations with Find summary setting not being checked in configuration. This setting is checked by default.

            Run a script on all files of a directory tree

            Mar 01, 2013#5

            Very often a script should be executed on all files of a directory or an entire directory tree.

            This can be easily achieved with using function GetListOfFiles and a few additional code lines wrapped around the main script.

            Code: Select all

            // Include the function GetListOfFiles() here without the code demonstrating the usage.
            
            // Replace the initial directory path in the function parameter list with
            // real path of the directory containing the files to modify by the script.
            if (GetListOfFiles(0,"C:\\Directory\\Path\\","*.*",true))
            {
               // If there was no error and files are found in the directory, the
               // function made the search results output with the file names active.
               // Select all lines in the file and load the file names into an array.
               UltraEdit.activeDocument.selectAll();
               var sLineTerm = "\r\n";   // Default line terminator type is DOS.
               var nLineTermPos = UltraEdit.activeDocument.selection.search(/\r\n|\n|\r/);
               if (nLineTermPos >= 0)    // Any line terminator found?
               {
                  // The list file is a Unix file if first character found is a line-feed.
                  if (UltraEdit.activeDocument.selection[nLineTermPos] == '\n') sLineTerm = "\n";
                  // The list file is a Mac file if first character found is a carriage
                  // return and the next character is not a line-feed as in a DOS file.
                  else if (UltraEdit.activeDocument.selection[nLineTermPos+1] != '\n') sLineTerm = "\r";
               }
               var asFileNames = UltraEdit.activeDocument.selection.split(sLineTerm);
            
               // The list is not needed anymore and therefore the results window is closed.
               UltraEdit.closeFile(UltraEdit.activeDocument.path,2);
               asFileNames.pop();  // Remove empty string at end of the list.
               
               // Now open one file after the other, process it, save and close the file.   
               // No file in the list should be opened already on script start! Why?
               // http://forums.ultraedit.com/viewtopic.php?f=52&t=4596#p26710
               // contains the answer on point 7 and the solution to enhance
               // this script further if this is necessary in some cases.
               for (var nFileIndex = 0; nFileIndex < asFileNames.length; nFileIndex++)
               {
                  UltraEdit.open(asFileNames[nFileIndex]);
            
                  // Your script code should be included here.
            
                  // Save and close the processed file.
                  UltraEdit.closeFile(UltraEdit.activeDocument.path,1);
               }
            }

              Jul 09, 2018#6

              The function GetListOfFiles was updated on 2018-06-24. Lots of comments were updated and improved for hopefully easier reading by non-native English speaking users.

              But there are also following two small functional improvements:
              1. Full qualified file names with one or more Unicode characters are handled correct by UltraEdit for Windows ≥ v24.00 or UEStudio ≥ v17.00 on reading them from a Unicode encoded file using scripting command UltraEdit.activeDocument.selection. For that reason the function does no longer convert the UTF-16 LE encoded results file with the file names from Unicode to ANSI which means file names or file paths can contain also characters not available in system code page for non Unicode aware applications.
                Users of UltraEdit for Windows < v24.00 or UEStudio < v17.00 should modify the value of variable bNoUnicode from false to true at top of the function for converting the results file with all the full qualified file names to ANSI using the system code page as used by UE/UES by default to handle file names/paths containing non ASCII characters as good as possible.
              2. A code optimization was applied on code which displays debug messages with a message box or outputs them to output window.

                Nov 14, 2018#7

                Some small improvements have been made on code of function GetListOfFiles and the demonstration code with no functional effect on execution on 2018-11-14.
                Best regards from an UC/UE/UES for Windows user from Austria