Adding filename to beginning of lines

Adding filename to beginning of lines

2
NewbieNewbie
2

    Sep 26, 2011#1

    Hi,

    I am very new to UltraEdit, but am liking what I see. Is there an easy way to add the filename (no path neccesary) to the beginning of each line within a file, across many files? I am using ultraedit ver 16.

    Thanks

    4

      Sep 27, 2011#2

      Here is a short script that shows some of the functionality you're after on the current open file and leaves that file unchanged and opens a new file with the file name added to each line. I know there are other examples of scripts going thru multiple files (Mofi) and something like this would need to be integrated into those examples.

      Code: Select all

      var   doc      = UltraEdit.activeDocument,
            lineTerm = doc.lineTerminator === 1 ? '\n' : doc.lineTerminator === 2 ? '\r' : '\r\n',
            fLines = [],
            fName  = '';
      
      doc.selectAll();
      fLines   = doc.selection.split(lineTerm);
      doc.bottom();
      doc.top();
      
      fName = doc.path.replace(/.+\\/,'');
      UltraEdit.newFile();
      for (var i = 0; i < fLines.length;i++) {
      	  doc.write(fName + ' ' + fLines[i] + lineTerm);
      }
      edit:
      I was unaware of Mofi's function for file name ....another awesome contribution to UE users.




      Doug

      6,603548
      Grand MasterGrand Master
      6,603548

        Sep 27, 2011#3

        Code: Select all

        if (UltraEdit.document.length > 0) {
        
           UltraEdit.insertMode();
           if (typeof(UltraEdit.columnModeOn) == "function") UltraEdit.columnModeOn();
           else if (typeof(UltraEdit.activeDocument.columnModeOn) == "function") UltraEdit.activeDocument.columnModeOn();
        
           for (var nDocIndex = 0; nDocIndex < UltraEdit.document.length; nDocIndex++) {
        
              // Ignore files opened in hex edit mode.
              if (UltraEdit.document[nDocIndex].isHexModeOn()) continue;
              // Get name of the file with a simplified method. Better method working
              // also for FTP loaded files and for UNIX file paths offers the function
              // GetNameOfFile() in https://www.ultraedit.com/resources/scripts/FileNameFunctions.js
              var nLastBackslashPos = UltraEdit.document[nDocIndex].path.lastIndexOf('\\');
              // Ignore not saved new files.
              if (nLastBackslashPos < 0) continue;
              var sFileName = UltraEdit.document[nDocIndex].path.substr(++nLastBackslashPos);
              UltraEdit.document[nDocIndex].top();
              // Insert in column mode the file name at beginning of every line.
              UltraEdit.document[nDocIndex].columnInsert(sFileName + ": ");
           }
           if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
           else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
        }

        2
        NewbieNewbie
        2

          Sep 27, 2011#4

          Thanks guys, this was exactly what I was looking for!