Adding filename to beginning of lines

Help with writing and running scripts

Adding filename to beginning of lines

Postby willt » Mon Sep 26, 2011 11:51 am

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
willt
Newbie
 
Posts: 2
Joined: Mon Sep 26, 2011 11:30 am

Re: Adding filename to beginning of lines

Postby fan.of.dilbert » Mon Sep 26, 2011 10:57 pm

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
User avatar
fan.of.dilbert
Newbie
 
Posts: 4
Joined: Sat Aug 08, 2009 2:28 pm
Location: USA

Re: Adding filename to beginning of lines

Postby Mofi » Tue Sep 27, 2011 12:55 am

Code: Select all
if (UltraEdit.document.length > 0) {

   UltraEdit.insertMode();
   UltraEdit.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 http://www.ultraedit.com/files/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 + ": ");
   }
   UltraEdit.columnModeOff();
}
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4058
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Adding filename to beginning of lines

Postby willt » Tue Sep 27, 2011 11:02 am

thanks guys this was exactly what i was looking for!
willt
Newbie
 
Posts: 2
Joined: Mon Sep 26, 2011 11:30 am


Return to Scripts