Function to get list of files into an edit window

Help with writing and running scripts

Function to get list of files into an edit window

Postby Mofi » Wed Oct 24, 2007 6:41 am

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 scripts section of the Extra Downloads page.

Important!
Users of UltraEdit / UEStudio not using the English version have to adapt 2 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 2 string variables ResultsDocTitle and SummaryInfo for details.

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

set_find_output_format.png
Screenshot of the dialog to configure output format of Find in Files command.
set_find_output_format.png (4.03 KiB) Viewed 984 times
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4049
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Function to get list of files into an edit window

Postby Mofi » Wed Feb 11, 2009 11:22 am

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

I have changed slightly all variable names by adding a prefix letter for the type of the variable. Advantages of using a type prefix letter:

  1. The type of a variable is always visible which is a great help.
  2. 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.
  3. 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.
  4. 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 have 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 have added now 4 additional error messages for that case depending on the input parameter nFileList.

Last I have 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.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4049
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Function to get list of files into an edit window

Postby richard.crist » Mon Jun 01, 2009 11:57 am

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)
User avatar
richard.crist
Basic User
Basic User
 
Posts: 11
Joined: Tue Jan 13, 2009 1:33 pm
Location: Nashville, TN, United States

Re: Function to get list of files into an edit window

Postby Mofi » Mon Oct 22, 2012 11:49 am

I have 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. This setting is checked by default.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4049
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Run a script on all files of a directory tree

Postby Mofi » Fri Mar 01, 2013 12:52 pm

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 asFileNames = UltraEdit.activeDocument.selection.split("\r\n");

   // 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.   
   for (var nFileIndex = 0; nFileIndex < asFileNames.length; nFileIndex++) {

      // No file in the list should be already open on script start! Why?
      // http://www.ultraedit.com/forums/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.
      UltraEdit.open(asFileNames[nFileIndex]);

      // Your script code should be included here.

      // Save and close the processed file.
      UltraEdit.closeFile(UltraEdit.activeDocument.path,1);
   }
}
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4049
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna


Return to Scripts