Script to loop through all open files and output their file names with path enclosed in double quotes into a new file

Script to loop through all open files and output their file names with path enclosed in double quotes into a new file

15
Basic UserBasic User
15

    Feb 14, 2018#1

    I have written a script to copy the full path and file name to the clipboard and put a double quote before and after it, and that works fine. I tried to extend the script to loop through all open files, quoting the file path as before, and pasting the results into a new blank document. So far, all I've accomplished is that the new blank document opens.

    Unfortunately, although I am a programmer (primarily Oracle PL/SQL), I know nothing about JavaScript, so I don't know what I'm doing wrong. Any suggestions? Would it help for me to put what I have tried?

    Thanks,
    Dan

    11327
    MasterMaster
    11327

      Feb 14, 2018#2

      Something like this?

      Code: Select all

      var n = UltraEdit.document.length
      var files = "";
      for (i=0;i<n;i++)
      {
        files = files + '"'+UltraEdit.document[i].path + '"'+";"
      }
      files = files.substr(files,files.length-1).replace(/;/g,"\r\n");
      UltraEdit.newFile();
      UltraEdit.activeDocument.write(files);
      It's impossible to lead us astray for we don't care even to choose the way.

      15
      Basic UserBasic User
      15

        Feb 14, 2018#3

        Perfect! I hadn't considered saving all the values into one variable and then just writing it all out at once. I was trying to write each file one at a time.

        Thanks!

          Feb 14, 2018#4

          After playing around with it a bit, I simplified and shortened it up a little. This version leaves a blank line at the end of the new file with all the file names listed, but that's good for my purposes.

          Code: Select all

          var n = UltraEdit.document.length
          var files = "";
          for (i=0; i<n; i++)
          {
            UltraEdit.document[i].setActive();
            files = files + '"'+UltraEdit.activeDocument.path + '"\n'
          }
          UltraEdit.newFile();
          UltraEdit.activeDocument.write(files);
          Thanks again!

          11327
          MasterMaster
          11327

            Feb 14, 2018#5

            You are welcome!

            I've slightly edited script:

            Line

            Code: Select all

            UltraEdit.document[i].setActive();
            was removed.

            Line 

            Code: Select all

            files = files + '"'+UltraEdit.activeDocument.path + '"'+";"
            changed to:  

            Code: Select all

            files = files + '"'+UltraEdit.document[i].path + '"'+";"
            It's impossible to lead us astray for we don't care even to choose the way.

            15
            Basic UserBasic User
            15

              Feb 15, 2018#6

              I incorporated those changes and made one more. I eliminated this line:

              Code: Select all

              var n = UltraEdit.document.length
              And just put the UltraEdit.document.length directly in the FOR:

              Code: Select all

              for (i=0; i < UltraEdit.document.length; i++)
              So my full script now looks like this:

              Code: Select all

              var files = "";
              for (i=0; i < UltraEdit.document.length; i++)
              {
                files = files + '"' + UltraEdit.document[i].path + '"\n'
              }
              UltraEdit.newFile();
              UltraEdit.activeDocument.write(files);

              11327
              MasterMaster
              11327

                Feb 15, 2018#7

                DGMarchant wrote:I incorporated those changes and made one more. I eliminated this line:

                Code: Select all

                var n = UltraEdit.document.length
                Hello!
                I may be wrong, but imho function call in the loop is a bad idea, because var n = UltraEdit.document.length is executing only once.
                It's impossible to lead us astray for we don't care even to choose the way.

                6,604548
                Grand MasterGrand Master
                6,604548

                  Feb 15, 2018#8

                  UltraEdit.document.length is not a function call. There is no () at end. UltraEdit is an object (a class in C++) which has the property (member variable in C++) document being an array of objects. The array has the property length which is the number of items in array.

                  If the code would be compiled to CPU instructions, it would make a very small difference on assigning the value of length accessed via two pointers to a local variable (register) and use this local variable in the loop. But in JavaScript it most likely does not make a noticeable difference.

                  BTW: Create a script with just the line var_dump(UltraEdit); and run this script and look into output window.

                    Feb 15, 2018#9

                    I suggest an enhanced version of the script. Read the comments for details.

                    Code: Select all

                    var asFileNames = [];
                    for (var nDocIndex=0; nDocIndex < UltraEdit.document.length; nDocIndex++)
                    {
                       // Is the current document window for a file with a name or at least
                       // with a file extension like .htaccess and not a new, unsaved file,
                       // or a find in files results file, or a captured command/tool ouput?
                       if ((!UltraEdit.document[nDocIndex].isName("")) || (!UltraEdit.document[nDocIndex].isExt("")))
                       {
                          // Append the name of this real file enclosed
                          // in double quotes to the array of file names.
                          asFileNames.push('"' + UltraEdit.document[nDocIndex].path + '"');
                       }
                    }
                    if (asFileNames.length)  // Contains the array of file names at least one string?
                    {
                       asFileNames.push(""); // Append an empty string to terminate also
                                             // last line witn appropriate line ending.
                       UltraEdit.newFile();  // Create a new file.
                       var sLineEnd;         // Determine line ending type of new file.
                       if (UltraEdit.activeDocument.lineTerminator < 1) sLineEnd = "\r\n";     // DOS/Windows
                       else if (UltraEdit.activeDocument.lineTerminator == 1) sLineEnd = "\n"; // Unix/Linux/Mac >= OS X
                       else sLineEnd = "\r";                                                   // Mac <= OS 9
                       UltraEdit.activeDocument.write(asFileNames.join(sLineEnd));
                    }
                    
                    Best regards from an UC/UE/UES for Windows user from Austria

                    11327
                    MasterMaster
                    11327

                      Feb 16, 2018#10

                      Thank you, Mofi, for clarification!  
                      It's impossible to lead us astray for we don't care even to choose the way.