Save each line into a file and name as the line number

Help with writing and running scripts

Save each line into a file and name as the line number

Postby mystrique » Wed Apr 09, 2008 4:46 am

Hi,

It been sooo long since the last time I visit this forum.

I have a text file consisting of few hundreds line of text. I need to save each line to a new file and name it based on the line number.

Pls advise.

Thanks,
User avatar
mystrique
Basic User
Basic User
 
Posts: 12
Joined: Mon Sep 05, 2005 11:00 pm

Re: Save each line into a file and name as the line number

Postby Mofi » Thu Apr 10, 2008 2:04 am

Do you use UE v13+ because with a script this would be easier than with macros because scripts support variables?
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4049
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Save each line into a file and name as the line number

Postby mystrique » Tue Apr 15, 2008 6:10 am

Whoa, too bad because I am still trapped with the dinasours (still using v11)

Can you explain how to do it? May be good reason for me to upgrade :)

Thanks.
User avatar
mystrique
Basic User
Basic User
 
Posts: 12
Joined: Mon Sep 05, 2005 11:00 pm

Re: Save each line into a file and name as the line number

Postby Mofi » Wed Apr 16, 2008 3:08 am

Here is the script. It is not perfect, but better as most developers would write it.

Code: Select all
// Set working environment required for this script.
UltraEdit.insertMode();
UltraEdit.columnModeOff();
UltraEdit.activeDocument.hexOff();
UltraEdit.ueReOn();
UltraEdit.selectClipboard(9);

var ActFileIndex = getActiveDocumentIndex();
if (ActFileIndex >= 0) SaveLinesIntoFiles(ActFileIndex);  // If any file is open!

UltraEdit.clearClipboard();
UltraEdit.selectClipboard(0);

// End of script!


// Main function which saves every not empty line into a file with line number as file name.
function SaveLinesIntoFiles(SourceFileIndex) {
   var LineNumber = 1;
   // Check if current file is empty file because nothing to do on an empty file.
   UltraEdit.document[SourceFileIndex].bottom();
   var ColumnNumber = UltraEdit.document[SourceFileIndex].currentColumnNum;
   if (typeof(UltraEdit.activeDocumentIdx) == "undefined") ColumnNumber++;
   if ((UltraEdit.document[SourceFileIndex].currentLineNum < 2) && (ColumnNumber < 2)) return;
   // Make sure last line of file has a line termination.
   if (UltraEdit.document[SourceFileIndex].isColNumGt(1)) {
      UltraEdit.document[SourceFileIndex].insertLine();
   }
   UltraEdit.document[SourceFileIndex].top();
   while (!UltraEdit.document[SourceFileIndex].isEof()) {
      // Copy only lines with characters, not empty lines.
      UltraEdit.document[SourceFileIndex].startSelect();
      UltraEdit.document[SourceFileIndex].key("END");
      if (UltraEdit.document[SourceFileIndex].isSel()) {
         UltraEdit.document[SourceFileIndex].copy();
         UltraEdit.newFile();
         UltraEdit.activeDocument.paste();
         UltraEdit.activeDocument.insertLine();
         UltraEdit.saveAs("F:\\Temp\\"+LineNumber+".txt");
         UltraEdit.closeFile(UltraEdit.activeDocument.path,0);
      }
      UltraEdit.document[SourceFileIndex].endSelect();
      UltraEdit.document[SourceFileIndex].gotoLine(++LineNumber,1);
   }
   /* Following line is only useful if more than 1 file is open, the active
      file on script start was not the most right one and config setting
      >Move to nearest left tab after current tab is closed< is enabled. */
   UltraEdit.document[SourceFileIndex].setActive();
   UltraEdit.document[SourceFileIndex].top();
}


/* Find the tab index of the active document.
   Copied from forum topic: Get Active Document's Index
   http://www.ultraedit.com/forums/viewtopic.php?t=4571 */
function getActiveDocumentIndex() {
   var tabindex = -1; /* start value */

   for (i = 0; i < UltraEdit.document.length; i++)
   {
      if (UltraEdit.activeDocument.path==UltraEdit.document[i].path) {
         tabindex = i;
         break;
      }
   }
   return tabindex;
}
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4049
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna


Return to Scripts