Advertisement
Gabarito

Get List Of Files

Dec 21st, 2023
1,983
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This script file can be executed with Alt+Shift+R after opening it in
  2. // UltraEdit or UEStudio to use the function and get a list of files.
  3.  
  4. // === GetListOfFiles ========================================================
  5.  
  6. /* Script Name:   GetListOfFiles
  7.    Creation Date: 2008-04-16
  8.    Last Modified: 2018-11-14
  9.    Copyright:     Copyright (c) 2018 by Mofi
  10.    Original:      https://www.ultraedit.com/resources/scripts/GetListOfFiles.js
  11.                   http://forums.ultraedit.com/viewtopic.php?f=52&t=5442
  12.  
  13. The function   GetListOfFiles   creates in an edit window a list of files
  14. according to the arguments passed to the function. It uses the command
  15. Find in Files   with advanced option   Results to edit window   for
  16. creating the file list.
  17.  
  18. The return value of the function is   true   if a file list could be created
  19. with at least one file name, otherwise   false   is returned by the function.
  20.  
  21. The new file with the file list is the active file and the caret is at top
  22. of the file on success finding at least one file matching the criteria.
  23.  
  24. The function requires UltraEdit for Windows >= v13.10 or UEStudio >= v6.30.
  25.  
  26. Unicode file names are supported by UltraEdit for Windows >= v24.00 and
  27. UEStudio >= v17.00. For former versions don't use this function on file names
  28. containing a character not included in system code page for non-Unicode aware
  29. applications and set value of variable   bNoUnicode   in the function below
  30. to value   true   to get ANSI encoded file names.
  31.  
  32. The function expects 1 number, 2 strings and 1 boolean as input parameters:
  33.  
  34.   1) nFileList - is the number which specifies the kind of file list
  35.                  to be created by the function.
  36.  
  37.      The possible values are:
  38.  
  39.         0 ... get list of files in a specified directory.
  40.         1 ... get list of open files.
  41.         2 ... get list of favorite files.
  42.         3 ... get list of project files.
  43.         4 ... get list of solution files (only supported by UEStudio).
  44.  
  45.      Value 0 is used by default if this parameter is not specified
  46.      at all, or is not of type number, or contains a wrong number.
  47.  
  48.      The other three function parameters are evaluated only if 0 is used
  49.      for the first function parameter and should be omitted on using 1-4.
  50.  
  51.   2) sDirectory - is the initial directory for the search.
  52.  
  53.      The current working directory ".\" is used by the function if this
  54.      string is empty and a search in a directory is set by first parameter.
  55.  
  56.      This parameter is ignored for all other file list types.
  57.  
  58.      The working directory is by default the directory active on start
  59.      of UltraEdit/UEStudio. In the properties of a shortcut the working
  60.      directory can be specified in the edit field named "Start in".
  61.  
  62.   3) sFileType - contains the file type specification like "*.txt" or
  63.      "ga*.htm;et*.htm".
  64.  
  65.      This string is only used for the search in a specified directory.
  66.  
  67.      "*" is used by the function if this parameter is not correct specified.
  68.  
  69.   4) bSubDirs - is a boolean parameter which determines to find files only
  70.      in the specified directory with value false, or also in subdirectories
  71.      with value true.
  72.  
  73.      This boolean is only used for the search in a specified directory.
  74.  
  75.      The default value is false if the parameter is not correct specified.
  76.  
  77. NOTE:
  78.  
  79. An existing edit window from a previous Find in Files run is not closed
  80. and therefore the results of the search for the file names are appended
  81. to the existing content of the file. This makes it possible to run this
  82. function multiple times with different values for the parameters to get
  83. one large file list.
  84.  
  85. ATTENTION:
  86.  
  87. The first two strings in the function code below must be adapted to used
  88. version of UE/UES if a localized version of UltraEdit or UEStudio is used
  89. because otherwise the function does not work as designed. And the function
  90. uses also a non-regular expression search and sets all find parameters as
  91. needed for this search.
  92.  
  93. A debug message is shown by the function on an error condition if a global
  94. variable named   g_nDebugMessage   exists and has the value 1 for debugging
  95. to output window or 2 for debugging with message boxes. No debug messages
  96. are displayed if there is no global variable with name g_nDebugMessage with
  97. a valid value.
  98.  
  99. This function is copyright protected by Mofi for free usage by UE/UES
  100. users. The author cannot be made responsible for any damage caused by
  101. this function. You use it at your own risk.
  102.  
  103. HINT:
  104.  
  105. The list can be loaded into an array of file names with following code:
  106.  
  107. UltraEdit.activeDocument.selectAll();
  108. var asFileNames = UltraEdit.activeDocument.selection.split("\r\n");
  109. // Last element of array is usually an empty string because all file
  110. // names in the list are terminated usually with a DOS line termination.
  111. // This empty string is removed from the array. The number of file names
  112. // is then equal the number of array elements (length property).
  113. if (asFileNames[asFileNames.length-1] == "") asFileNames.pop();
  114. // The results file with the list of file names is no longer needed
  115. // when using now the file names in the array asFileNames.
  116. UltraEdit.closeFile(UltraEdit.activeDocument.path,2);
  117.  
  118. The main script can use next a for loop to open a found file, modify
  119. the opened file, save and close it, and continue with next file until
  120. all files have been modified. Extra code is needed to make the already
  121. opened file active instead of opening it if it is possible that some of
  122. the files in the list are already opened. For details see on forum topic
  123. http://forums.ultraedit.com/viewtopic.php?f=52&t=4596#p26710 the hint on
  124. common mistake "Opening a file already open from within script does not
  125. result in reloading file content and making the file active". */
  126.  
  127.  
  128. function GetListOfFiles (nFileList, sDirectory, sFileType, bSubDirs)
  129. {
  130.    /* The summary info line at bottom of a Find in Files result depends
  131.       on application language as also the name of the results file.
  132.       Please adapt the following two strings to your version of UE/UES.
  133.  
  134.       Please click on ribbon tab "Advanced" or in menu "Advanced" on
  135.       "Settings" or "Configuration", expand tree item "Search" and select
  136.       item "Find output format" or "Set Find Output Format". The displayed
  137.       configuration dialog contains the "Find summary" definition at bottom
  138.       with the format string to assign below to variable sSummaryInfo.
  139.  
  140.       Even better is executing once the command "Find in Files" with an
  141.       empty search string with advanced (gearwheel) option "Results to
  142.       edit window" checked. The "Find in Files" floating window or docked
  143.       view can be opened by
  144.  
  145.       a) pressing default hotkey Ctrl+Shift+F;
  146.       b) clicking on ribbon tab "Home" in second group "Search" on
  147.          last item "Find/replace in files" on using ribbon mode;
  148.       c) clicking in menu "Home" on the menu item "Find/replace in files"
  149.          on using toolbar/menu mode with contemporary menus;
  150.       d) clicking in menu "Search" on menu item "Find/replace in files"
  151.          respectively "Find in Files" on using toolbar/menu mode with
  152.          traditional menus or UE < v23.00 respectively UES < v16.00.
  153.  
  154.       After running "Find in Files" with an empty search string listing
  155.       with results written to an edit window with at least one file name
  156.       in results window , look on file tab of results window and last
  157.       line in results file and adapt the two strings below accordingly.
  158.  
  159.       It is also possible to just open GetListOfFiles.js and execute it with
  160.       Alt+Shift+R to see how the find results look like in used version of
  161.       UE/UES on having entered the parameters correct during script execution.
  162.  
  163.       Please note that variable sSummaryInfo must be initialized with
  164.       an empty string on having disabled the "Find summary" completely. */
  165.  
  166. //   var sSummaryInfo = "Search complete, found";
  167.    var sSummaryInfo = "";
  168.    var sResultsDocTitle = "** Find Results ** ";  // Note the space at end!
  169.  
  170.    // The default strings for German UltraEdit are:
  171.    // var sSummaryInfo = "Suche abgeschlossen, ";
  172.    // var sResultsDocTitle = "** Suchergebnisse ** ";
  173.  
  174.    /* Modify the value of this variable to true on using UltraEdit
  175.       for Windows < v24.00 or UEStudio < v17.00 which do not support
  176.       correct loading Unicode strings from Unicode files into memory. */
  177.    var bNoUnicode = false;
  178.  
  179.  
  180.    /* Determine the type of output for debug messages from the global
  181.       variable g_nDebugMessage: 1 ... debug to output window, 2 ... debug
  182.       to message dialog, all others ... no debug messages. A debug message
  183.       is displayed with a popup message box like on using the value 2 if
  184.       a global number variable g_nDebugMessage does not exist at all. */
  185.    var nOutputType = (typeof(g_nDebugMessage) == "number") ? g_nDebugMessage : 2;
  186.    if ((nOutputType < 1) || (nOutputType > 2)) nOutputType = 0;
  187.  
  188.    if (typeof(nFileList) != "number" || nFileList < 0 || nFileList > 4) nFileList = 0;
  189.  
  190.    if (!nFileList)      // Search in a specified directory?
  191.    {
  192.       // Use current working directory if no directory is specified.
  193.       if ((typeof(sDirectory) != "string") || (!sDirectory.length)) sDirectory = ".\\";
  194.       // Append a backslash if it is missing at end of the directory string.
  195.       else if (sDirectory[sDirectory.length-1] != "\\") sDirectory += "\\";
  196.       // Search for all files if no file type is specified.
  197.       if ((typeof(sFileType) != "string") || (!sFileType.length)) sFileType = "*";
  198.       if (typeof(bSubDirs) != "boolean") bSubDirs = false;
  199.    }
  200.    else
  201.    {
  202.       sDirectory = "";    // For the list of open, favorite, project
  203.       sFileType = "";     // or solution files the other 3 parameters
  204.       bSubDirs = false;   // have always the same default values.
  205.    }
  206.  
  207.    // Remember current regular expression engine.
  208.    var nRegexEngine = UltraEdit.regexMode;
  209.    /* A regular expression engine must be defined or the find
  210.       for the last line in the Unicode results could fail. */
  211.    UltraEdit.ueReOn();
  212.  
  213.    /* Run a Find in Files with an empty search string to get the
  214.       list of files stored in the specified directory in an edit
  215.       window and delete the last line with the summary info. */
  216.    UltraEdit.frInFiles.directoryStart=sDirectory;
  217.    UltraEdit.frInFiles.filesToSearch=nFileList;
  218.    UltraEdit.frInFiles.matchCase=false;
  219.    UltraEdit.frInFiles.matchWord=false;
  220.    UltraEdit.frInFiles.regExp=false;
  221.    UltraEdit.frInFiles.searchInFilesTypes=sFileType;
  222.    UltraEdit.frInFiles.searchSubs=bSubDirs;
  223.    UltraEdit.frInFiles.unicodeSearch=false;
  224.    UltraEdit.frInFiles.useOutputWindow=false;
  225.    if (typeof(UltraEdit.frInFiles.openMatchingFiles) == "boolean")
  226.    {
  227.       UltraEdit.frInFiles.openMatchingFiles=false;
  228.    }
  229.    UltraEdit.frInFiles.find("");
  230.  
  231.    /* The results of the search above are appended if the Find in Files
  232.       results window was already opened before running Find in Files. But
  233.       the results file does not get automatically the focus as it does if
  234.       there was no results file open from a previous search. Therefore care
  235.       must be taken that the file with the Find in Files results is the
  236.       active document after the search to continue on correct file. */
  237.    var bListCreated = false;
  238.    if (UltraEdit.activeDocument.path == sResultsDocTitle) bListCreated = true;
  239.    else
  240.    {
  241.       for (var nDocIndex = 0; nDocIndex < UltraEdit.document.length; nDocIndex++)
  242.       {
  243.          if (UltraEdit.document[nDocIndex].path == sResultsDocTitle)
  244.          {
  245.             UltraEdit.document[nDocIndex].setActive();
  246.             bListCreated = true;
  247.             break;
  248.          }
  249.       }
  250.    }
  251.    if (bListCreated && sSummaryInfo.length)
  252.    {
  253.       // Search for the summary info at bottom of the results.
  254.       UltraEdit.activeDocument.findReplace.searchDown=false;
  255.       UltraEdit.activeDocument.findReplace.matchCase=true;
  256.       UltraEdit.activeDocument.findReplace.matchWord=false;
  257.       UltraEdit.activeDocument.findReplace.regExp=false;
  258.       UltraEdit.activeDocument.findReplace.find(sSummaryInfo);
  259.       bListCreated = UltraEdit.activeDocument.isFound();
  260.    }
  261.    UltraEdit.activeDocument.findReplace.searchDown=true;
  262.    switch (nRegexEngine)     // Restore original regular expression engine.
  263.    {
  264.       case 1:  UltraEdit.unixReOn(); break;
  265.       case 2:  UltraEdit.perlReOn(); break;
  266.       default: UltraEdit.ueReOn();   break;
  267.    }
  268.    /* Check next if the Find above was successful on finding last line in
  269.       the active document which should contain the Find in Files results. */
  270.    if (!bListCreated)
  271.    {
  272.       if (nOutputType == 2)
  273.       {
  274.          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");
  275.       }
  276.       else if (nOutputType == 1)
  277.       {
  278.          if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
  279.          UltraEdit.outputWindow.write('GetListOfFiles: There is a problem with command frInFiles or the strings of the two script variables');
  280.          UltraEdit.outputWindow.write('                "sSummaryInfo" and "sResultsDocTitle" are not adapted to used version of UltraEdit/UEStudio!');
  281.       }
  282.       return false;
  283.    }
  284.  
  285.    // Delete last line in results file if line with summary info was found.
  286.    if (sSummaryInfo.length) UltraEdit.activeDocument.deleteLine();
  287.    UltraEdit.activeDocument.top();
  288.  
  289.    /* Next convert the file into an ASCII/ANSI file for better handling of
  290.       the file names by UltraEdit for Windows < v24.00 and UEStudio < v17.00.
  291.       Unicode file names are not supported by these versions of UE/UES. There
  292.       is no need for this conversion if the file with the results is already
  293.       an ASCII file from a previous execution of function GetListOfFiles. */
  294.    if (bNoUnicode)
  295.    {
  296.       UltraEdit.activeDocument.key("RIGHT ARROW");
  297.       if (UltraEdit.activeDocument.currentPos > 1) UltraEdit.activeDocument.unicodeToASCII();
  298.       else UltraEdit.activeDocument.top();
  299.    }
  300.  
  301.    // No files were found if top of file is also end of file.
  302.    if (UltraEdit.activeDocument.isEof())
  303.    {
  304.       if (nOutputType > 0)
  305.       {
  306.          var sMessage;
  307.          switch (nFileList)
  308.          {
  309.             case 0:  sMessage = "No file " + sFileType + " was found in directory" +
  310.                                 ((nOutputType == 2) ? "\n\n" : " ") + sDirectory; break;
  311.             case 1:  sMessage = "There are no opened files."; break;
  312.             case 2:  sMessage = "There are no favorite files."; break;
  313.             case 3:  sMessage = "There are no project files or no project is opened."; break;
  314.             case 4:  sMessage = "There are no solution files or no solution is opened."; break;
  315.             default: sMessage = ""; break;
  316.          }
  317.          if (nOutputType == 1)
  318.          {
  319.             if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
  320.             UltraEdit.outputWindow.write("GetListOfFiles: "+sMessage);
  321.          }
  322.          else UltraEdit.messageBox(sMessage,"GetListOfFiles Error");
  323.       }
  324.       UltraEdit.closeFile(UltraEdit.activeDocument.path,2);
  325.       return false;
  326.    }
  327.    return true;
  328. }  // End of function GetListOfFiles
  329.  
  330.  
  331. // === GetListOfFiles demonstration ==========================================
  332.  
  333. // Code to demonstrate the usage of the function GetListOfFiles.
  334.  
  335. var g_nDebugMessage=2;  // Enable debug messages with message boxes.
  336. var nSearchIn=0;        // Stores users choice where to search.
  337. var sFolder="";         // Stores the search root folder specified by the user.
  338. var sFiles="";          // Stores the search files/types specified by the user.
  339. var bSubFolders=false;  // Stores the users choice for searching in subdirectories.
  340. var bNewOpened=false;   // Is set to true if a new file had to be opened.
  341.  
  342. UltraEdit.outputWindow.showStatus=false;
  343. UltraEdit.outputWindow.clear();
  344. if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
  345.  
  346. /* The scripting commands getValue and getString do not open message boxes
  347.    for user input and return always 0 respectively null on using UltraEdit
  348.    for Windows < v22.20.0.37 and UEStudio < v15.30.0.12 and no file is
  349.    opened at the moment. As workaround open a new file and later close
  350.    that file without saving before using function GetListOfFiles.
  351.    The condition below as well as definition of variable bNewOpened above
  352.    and the last but one line in this script file can be removed on using
  353.    UltraEdit for Windows >= v22.20.0.37 and UEStudio >= v15.30.0.12. */
  354. if (UltraEdit.document.length < 1)
  355. {
  356.    UltraEdit.newFile();
  357.    bNewOpened = true;
  358. }
  359.  
  360. nSearchIn = UltraEdit.getValue("Search in a directory (= 0) or in special file group (= 1)?",1);
  361. if (nSearchIn)
  362. {
  363.    nSearchIn = UltraEdit.getValue("Group: 1 = open, 2 = favorites, 3 = project, 4 = solution",1);
  364.    switch (nSearchIn)
  365.    {
  366.       case 1: UltraEdit.outputWindow.write("Get list of open files");     break;
  367.       case 2: UltraEdit.outputWindow.write("Get list of favorite files"); break;
  368.       case 3: UltraEdit.outputWindow.write("Get list of project files");  break;
  369.       case 4: UltraEdit.outputWindow.write("Get list of solution files"); break;
  370.       default: nSearchIn = 0; break;
  371.    }
  372. }
  373. if (!nSearchIn)
  374. {
  375.    UltraEdit.outputWindow.write("Get list of files: in directory");
  376.    sFolder = UltraEdit.getString("Please enter the path to the directory:",1);
  377.    if ((!sFolder.length) || (sFolder == ".\\"))
  378.    {
  379.       UltraEdit.outputWindow.write("Search directory:  .\\ (= working directory)");
  380.    }
  381.    else
  382.    {
  383.       UltraEdit.outputWindow.write("Search directory:  " + sFolder);
  384.    }
  385.    sFiles = UltraEdit.getString("Please enter the files/types to find:",1);
  386.    if (!sFiles.length) sFiles = "*";
  387.    UltraEdit.outputWindow.write("Find files/types:  " + sFiles);
  388.    var nSubFolders = UltraEdit.getValue("Search in subdirectories (0/1 = no/yes)?",1);
  389.    if (nSubFolders) bSubFolders = true;
  390.    var sUsersChoice = bSubFolders ? "true" : "false";
  391.    UltraEdit.outputWindow.write("Search subfolders: " + sUsersChoice);
  392. }
  393. if (bNewOpened) UltraEdit.closeFile(UltraEdit.document[0].path,2);
  394. GetListOfFiles(nSearchIn,sFolder,sFiles,bSubFolders);
  395.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement