Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This script file can be executed with Alt+Shift+R after opening it in
- // UltraEdit or UEStudio to use the function and get a list of files.
- // === GetListOfFiles ========================================================
- /* Script Name: GetListOfFiles
- Creation Date: 2008-04-16
- Last Modified: 2018-11-14
- Copyright: Copyright (c) 2018 by Mofi
- Original: https://www.ultraedit.com/resources/scripts/GetListOfFiles.js
- http://forums.ultraedit.com/viewtopic.php?f=52&t=5442
- The function GetListOfFiles creates in an edit window a list of files
- according to the arguments passed to the function. It uses the command
- Find in Files with advanced option Results to edit window for
- creating the file list.
- The return value of the function is true if a file list could be created
- with at least one file name, otherwise false is returned by the function.
- The new file with the file list is the active file and the caret is at top
- of the file on success finding at least one file matching the criteria.
- The function requires UltraEdit for Windows >= v13.10 or UEStudio >= v6.30.
- Unicode file names are supported by UltraEdit for Windows >= v24.00 and
- UEStudio >= v17.00. For former versions don't use this function on file names
- containing a character not included in system code page for non-Unicode aware
- applications and set value of variable bNoUnicode in the function below
- to value true to get ANSI encoded file names.
- The function expects 1 number, 2 strings and 1 boolean as input parameters:
- 1) nFileList - is the number which specifies the kind of file list
- to be created by the function.
- The possible values are:
- 0 ... get list of files in a specified directory.
- 1 ... get list of open files.
- 2 ... get list of favorite files.
- 3 ... get list of project files.
- 4 ... get list of solution files (only supported by UEStudio).
- Value 0 is used by default if this parameter is not specified
- at all, or is not of type number, or contains a wrong number.
- The other three function parameters are evaluated only if 0 is used
- for the first function parameter and should be omitted on using 1-4.
- 2) sDirectory - is the initial directory for the search.
- The current working directory ".\" is used by the function if this
- string is empty and a search in a directory is set by first parameter.
- This parameter is ignored for all other file list types.
- The working directory is by default the directory active on start
- of UltraEdit/UEStudio. In the properties of a shortcut the working
- directory can be specified in the edit field named "Start in".
- 3) sFileType - contains the file type specification like "*.txt" or
- "ga*.htm;et*.htm".
- This string is only used for the search in a specified directory.
- "*" is used by the function if this parameter is not correct specified.
- 4) bSubDirs - is a boolean parameter which determines to find files only
- in the specified directory with value false, or also in subdirectories
- with value true.
- This boolean is only used for the search in a specified directory.
- The default value is false if the parameter is not correct specified.
- NOTE:
- An existing edit window from a previous Find in Files run is not closed
- and therefore the results of the search for the file names are appended
- to the existing content of the file. This makes it possible to run this
- function multiple times with different values for the parameters to get
- one large file list.
- ATTENTION:
- The first two strings in the function code below must be adapted to used
- version of UE/UES if a localized version of UltraEdit or UEStudio is used
- because otherwise the function does not work as designed. And the function
- uses also a non-regular expression search and sets all find parameters as
- needed for this search.
- A debug message is shown by the function on an error condition if a global
- variable named g_nDebugMessage exists and has the value 1 for debugging
- to output window or 2 for debugging with message boxes. No debug messages
- are displayed if there is no global variable with name g_nDebugMessage with
- a valid value.
- This function is copyright protected by Mofi for free usage by UE/UES
- users. The author cannot be made responsible for any damage caused by
- this function. You use it at your own risk.
- HINT:
- The list can be loaded into an array of file names with following code:
- UltraEdit.activeDocument.selectAll();
- var asFileNames = UltraEdit.activeDocument.selection.split("\r\n");
- // Last element of array is usually an empty string because all file
- // names in the list are terminated usually with a DOS line termination.
- // This empty string is removed from the array. The number of file names
- // is then equal the number of array elements (length property).
- if (asFileNames[asFileNames.length-1] == "") asFileNames.pop();
- // The results file with the list of file names is no longer needed
- // when using now the file names in the array asFileNames.
- UltraEdit.closeFile(UltraEdit.activeDocument.path,2);
- The main script can use next a for loop to open a found file, modify
- the opened file, save and close it, and continue with next file until
- all files have been modified. Extra code is needed to make the already
- opened file active instead of opening it if it is possible that some of
- the files in the list are already opened. For details see on forum topic
- http://forums.ultraedit.com/viewtopic.php?f=52&t=4596#p26710 the hint on
- common mistake "Opening a file already open from within script does not
- result in reloading file content and making the file active". */
- function GetListOfFiles (nFileList, sDirectory, sFileType, bSubDirs)
- {
- /* The summary info line at bottom of a Find in Files result depends
- on application language as also the name of the results file.
- Please adapt the following two strings to your version of UE/UES.
- Please click on ribbon tab "Advanced" or in menu "Advanced" on
- "Settings" or "Configuration", expand tree item "Search" and select
- item "Find output format" or "Set Find Output Format". The displayed
- configuration dialog contains the "Find summary" definition at bottom
- with the format string to assign below to variable sSummaryInfo.
- Even better is executing once the command "Find in Files" with an
- empty search string with advanced (gearwheel) option "Results to
- edit window" checked. The "Find in Files" floating window or docked
- view can be opened by
- a) pressing default hotkey Ctrl+Shift+F;
- b) clicking on ribbon tab "Home" in second group "Search" on
- last item "Find/replace in files" on using ribbon mode;
- c) clicking in menu "Home" on the menu item "Find/replace in files"
- on using toolbar/menu mode with contemporary menus;
- d) clicking in menu "Search" on menu item "Find/replace in files"
- respectively "Find in Files" on using toolbar/menu mode with
- traditional menus or UE < v23.00 respectively UES < v16.00.
- After running "Find in Files" with an empty search string listing
- with results written to an edit window with at least one file name
- in results window , look on file tab of results window and last
- line in results file and adapt the two strings below accordingly.
- It is also possible to just open GetListOfFiles.js and execute it with
- Alt+Shift+R to see how the find results look like in used version of
- UE/UES on having entered the parameters correct during script execution.
- Please note that variable sSummaryInfo must be initialized with
- an empty string on having disabled the "Find summary" completely. */
- // var sSummaryInfo = "Search complete, found";
- var sSummaryInfo = "";
- var sResultsDocTitle = "** Find Results ** "; // Note the space at end!
- // The default strings for German UltraEdit are:
- // var sSummaryInfo = "Suche abgeschlossen, ";
- // var sResultsDocTitle = "** Suchergebnisse ** ";
- /* Modify the value of this variable to true on using UltraEdit
- for Windows < v24.00 or UEStudio < v17.00 which do not support
- correct loading Unicode strings from Unicode files into memory. */
- var bNoUnicode = false;
- /* Determine the type of output for debug messages from the global
- variable g_nDebugMessage: 1 ... debug to output window, 2 ... debug
- to message dialog, all others ... no debug messages. A debug message
- is displayed with a popup message box like on using the value 2 if
- a global number variable g_nDebugMessage does not exist at all. */
- var nOutputType = (typeof(g_nDebugMessage) == "number") ? g_nDebugMessage : 2;
- if ((nOutputType < 1) || (nOutputType > 2)) nOutputType = 0;
- if (typeof(nFileList) != "number" || nFileList < 0 || nFileList > 4) nFileList = 0;
- if (!nFileList) // Search in a specified directory?
- {
- // Use current working directory if no directory is specified.
- if ((typeof(sDirectory) != "string") || (!sDirectory.length)) sDirectory = ".\\";
- // Append a backslash if it is missing at end of the directory string.
- else if (sDirectory[sDirectory.length-1] != "\\") sDirectory += "\\";
- // Search for all files if no file type is specified.
- if ((typeof(sFileType) != "string") || (!sFileType.length)) sFileType = "*";
- if (typeof(bSubDirs) != "boolean") bSubDirs = false;
- }
- else
- {
- sDirectory = ""; // For the list of open, favorite, project
- sFileType = ""; // or solution files the other 3 parameters
- bSubDirs = false; // have always the same default values.
- }
- // Remember current regular expression engine.
- var nRegexEngine = UltraEdit.regexMode;
- /* A regular expression engine must be defined or the find
- for the last line in the Unicode results could fail. */
- UltraEdit.ueReOn();
- /* Run a Find in Files with an empty search string to get the
- list of files stored in the specified directory in an edit
- window and delete the last line with the summary info. */
- UltraEdit.frInFiles.directoryStart=sDirectory;
- UltraEdit.frInFiles.filesToSearch=nFileList;
- UltraEdit.frInFiles.matchCase=false;
- UltraEdit.frInFiles.matchWord=false;
- UltraEdit.frInFiles.regExp=false;
- UltraEdit.frInFiles.searchInFilesTypes=sFileType;
- UltraEdit.frInFiles.searchSubs=bSubDirs;
- UltraEdit.frInFiles.unicodeSearch=false;
- UltraEdit.frInFiles.useOutputWindow=false;
- if (typeof(UltraEdit.frInFiles.openMatchingFiles) == "boolean")
- {
- UltraEdit.frInFiles.openMatchingFiles=false;
- }
- UltraEdit.frInFiles.find("");
- /* The results of the search above are appended if the Find in Files
- results window was already opened before running Find in Files. But
- the results file does not get automatically the focus as it does if
- there was no results file open from a previous search. Therefore care
- must be taken that the file with the Find in Files results is the
- active document after the search to continue on correct file. */
- var bListCreated = false;
- if (UltraEdit.activeDocument.path == sResultsDocTitle) bListCreated = true;
- else
- {
- for (var nDocIndex = 0; nDocIndex < UltraEdit.document.length; nDocIndex++)
- {
- if (UltraEdit.document[nDocIndex].path == sResultsDocTitle)
- {
- UltraEdit.document[nDocIndex].setActive();
- bListCreated = true;
- break;
- }
- }
- }
- if (bListCreated && sSummaryInfo.length)
- {
- // Search for the summary info at bottom of the results.
- UltraEdit.activeDocument.findReplace.searchDown=false;
- UltraEdit.activeDocument.findReplace.matchCase=true;
- UltraEdit.activeDocument.findReplace.matchWord=false;
- UltraEdit.activeDocument.findReplace.regExp=false;
- UltraEdit.activeDocument.findReplace.find(sSummaryInfo);
- bListCreated = UltraEdit.activeDocument.isFound();
- }
- UltraEdit.activeDocument.findReplace.searchDown=true;
- switch (nRegexEngine) // Restore original regular expression engine.
- {
- case 1: UltraEdit.unixReOn(); break;
- case 2: UltraEdit.perlReOn(); break;
- default: UltraEdit.ueReOn(); break;
- }
- /* Check next if the Find above was successful on finding last line in
- the active document which should contain the Find in Files results. */
- if (!bListCreated)
- {
- if (nOutputType == 2)
- {
- UltraEdit.messageBox('There is a problem with command frInFiles or the strings of the two script variables "sSummaryInfo" and "sResultsDocTitle" are not adapted to used version of UltraEdit/UEStudio!',"GetListOfFiles Error");
- }
- else if (nOutputType == 1)
- {
- if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
- UltraEdit.outputWindow.write('GetListOfFiles: There is a problem with command frInFiles or the strings of the two script variables');
- UltraEdit.outputWindow.write(' "sSummaryInfo" and "sResultsDocTitle" are not adapted to used version of UltraEdit/UEStudio!');
- }
- return false;
- }
- // Delete last line in results file if line with summary info was found.
- if (sSummaryInfo.length) UltraEdit.activeDocument.deleteLine();
- UltraEdit.activeDocument.top();
- /* Next convert the file into an ASCII/ANSI file for better handling of
- the file names by UltraEdit for Windows < v24.00 and UEStudio < v17.00.
- Unicode file names are not supported by these versions of UE/UES. There
- is no need for this conversion if the file with the results is already
- an ASCII file from a previous execution of function GetListOfFiles. */
- if (bNoUnicode)
- {
- UltraEdit.activeDocument.key("RIGHT ARROW");
- if (UltraEdit.activeDocument.currentPos > 1) UltraEdit.activeDocument.unicodeToASCII();
- else UltraEdit.activeDocument.top();
- }
- // No files were found if top of file is also end of file.
- if (UltraEdit.activeDocument.isEof())
- {
- if (nOutputType > 0)
- {
- var sMessage;
- switch (nFileList)
- {
- case 0: sMessage = "No file " + sFileType + " was found in directory" +
- ((nOutputType == 2) ? "\n\n" : " ") + sDirectory; break;
- case 1: sMessage = "There are no opened files."; break;
- case 2: sMessage = "There are no favorite files."; break;
- case 3: sMessage = "There are no project files or no project is opened."; break;
- case 4: sMessage = "There are no solution files or no solution is opened."; break;
- default: sMessage = ""; break;
- }
- if (nOutputType == 1)
- {
- if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
- UltraEdit.outputWindow.write("GetListOfFiles: "+sMessage);
- }
- else UltraEdit.messageBox(sMessage,"GetListOfFiles Error");
- }
- UltraEdit.closeFile(UltraEdit.activeDocument.path,2);
- return false;
- }
- return true;
- } // End of function GetListOfFiles
- // === GetListOfFiles demonstration ==========================================
- // Code to demonstrate the usage of the function GetListOfFiles.
- var g_nDebugMessage=2; // Enable debug messages with message boxes.
- var nSearchIn=0; // Stores users choice where to search.
- var sFolder=""; // Stores the search root folder specified by the user.
- var sFiles=""; // Stores the search files/types specified by the user.
- var bSubFolders=false; // Stores the users choice for searching in subdirectories.
- var bNewOpened=false; // Is set to true if a new file had to be opened.
- UltraEdit.outputWindow.showStatus=false;
- UltraEdit.outputWindow.clear();
- if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
- /* The scripting commands getValue and getString do not open message boxes
- for user input and return always 0 respectively null on using UltraEdit
- for Windows < v22.20.0.37 and UEStudio < v15.30.0.12 and no file is
- opened at the moment. As workaround open a new file and later close
- that file without saving before using function GetListOfFiles.
- The condition below as well as definition of variable bNewOpened above
- and the last but one line in this script file can be removed on using
- UltraEdit for Windows >= v22.20.0.37 and UEStudio >= v15.30.0.12. */
- if (UltraEdit.document.length < 1)
- {
- UltraEdit.newFile();
- bNewOpened = true;
- }
- nSearchIn = UltraEdit.getValue("Search in a directory (= 0) or in special file group (= 1)?",1);
- if (nSearchIn)
- {
- nSearchIn = UltraEdit.getValue("Group: 1 = open, 2 = favorites, 3 = project, 4 = solution",1);
- switch (nSearchIn)
- {
- case 1: UltraEdit.outputWindow.write("Get list of open files"); break;
- case 2: UltraEdit.outputWindow.write("Get list of favorite files"); break;
- case 3: UltraEdit.outputWindow.write("Get list of project files"); break;
- case 4: UltraEdit.outputWindow.write("Get list of solution files"); break;
- default: nSearchIn = 0; break;
- }
- }
- if (!nSearchIn)
- {
- UltraEdit.outputWindow.write("Get list of files: in directory");
- sFolder = UltraEdit.getString("Please enter the path to the directory:",1);
- if ((!sFolder.length) || (sFolder == ".\\"))
- {
- UltraEdit.outputWindow.write("Search directory: .\\ (= working directory)");
- }
- else
- {
- UltraEdit.outputWindow.write("Search directory: " + sFolder);
- }
- sFiles = UltraEdit.getString("Please enter the files/types to find:",1);
- if (!sFiles.length) sFiles = "*";
- UltraEdit.outputWindow.write("Find files/types: " + sFiles);
- var nSubFolders = UltraEdit.getValue("Search in subdirectories (0/1 = no/yes)?",1);
- if (nSubFolders) bSubFolders = true;
- var sUsersChoice = bSubFolders ? "true" : "false";
- UltraEdit.outputWindow.write("Search subfolders: " + sUsersChoice);
- }
- if (bNewOpened) UltraEdit.closeFile(UltraEdit.document[0].path,2);
- GetListOfFiles(nSearchIn,sFolder,sFiles,bSubFolders);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement