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

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

12
Basic UserBasic User
12

    Apr 09, 2008#1

    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,

    6,686585
    Grand MasterGrand Master
    6,686585

      Apr 10, 2008#2

      Do you use UE v13+ because with a script this would be easier than with macros because scripts support variables?
      Best regards from an UC/UE/UES for Windows user from Austria

      12
      Basic UserBasic User
      12

        Apr 15, 2008#3

        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.

        6,686585
        Grand MasterGrand Master
        6,686585

          Apr 16, 2008#4

          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
             https://forums.ultraedit.com/viewtopic.php?f=52&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;
          }
          Best regards from an UC/UE/UES for Windows user from Austria