User to user discussion and support for UltraEdit, UEStudio, UltraCompare, and other IDM applications.

Help with writing and running scripts
2 posts Page 1 of 1
Hi friends

How to split a single file into multiple several files by script?

I want a script which run with file path and split file in multiple files.

At first put file path next the beginning tag in file like <hr class="sigil_marker"/>.
File split by <hr class="sigil_marker"/> beginning tag with appropriate closing tag, which open adobe <hr class="sigil_marker"/> and those open tag insert next split file.

I have uploaded an example file and the result files after splitting the example file. Both files were deleted later.
Here is the script for this task.

Code: Select all
function CreateChapterFile ()
{
   UltraEdit.newFile();
   UltraEdit.activeDocument.unixMacToDos();
   UltraEdit.activeDocument.write('<html>\r\n<body>\r\n<div class="chapter">\r\n');
   UltraEdit.activeDocument.paste();
   UltraEdit.activeDocument.write('</div>\r\n</body>\r\n</html>\r\n');

   var sChapter = g_nChapter.toString(10);
   sChapter = sLeadingZeros.substr(0,sLeadingZeros.length-sChapter.length) + sChapter;
   var sFileNameWithPath = g_sFilePath + sChapter + "_" + g_sFileName + "-" + sChapter + ".xhtml";
   UltraEdit.saveAs(sFileNameWithPath);
   UltraEdit.closeFile(UltraEdit.activeDocument.path,2);
}

if (UltraEdit.document.length > 0)
{
   UltraEdit.insertMode();
   UltraEdit.columnModeOff();
   UltraEdit.activeDocument.top();

   var nDocIndex = UltraEdit.activeDocumentIdx;

   UltraEdit.ueReOn();
   UltraEdit.document[nDocIndex].findReplace.mode=0;
   UltraEdit.document[nDocIndex].findReplace.matchCase=true;
   UltraEdit.document[nDocIndex].findReplace.matchWord=false;
   UltraEdit.document[nDocIndex].findReplace.regExp=false;
   UltraEdit.document[nDocIndex].findReplace.searchDown=true;
   UltraEdit.document[nDocIndex].findReplace.searchInColumn=false;

   if(UltraEdit.document[nDocIndex].findReplace.find('<div class="chapter">'))
   {
      var g_nChapter  = 1;
      var g_sFilePath = UltraEdit.document[nDocIndex].path.replace(/^(.+\\).+$/,"$1");
      var g_sFileName = UltraEdit.document[nDocIndex].path.replace(/^.+\\(.+)\.xhtml$/,"$1");

      var nLastLine;
      var nFirstLine = UltraEdit.document[nDocIndex].currentLineNum + 1;
      var sLeadingZeros = "00";

      UltraEdit.selectClipboard(9);

      while(UltraEdit.document[nDocIndex].findReplace.find('<hr class="sigil_marker"/>'))
      {
         nLastLine = UltraEdit.document[nDocIndex].currentLineNum;
         UltraEdit.document[nDocIndex].gotoLine(nLastLine,1);
         UltraEdit.document[nDocIndex].gotoLineSelect(nFirstLine,1);
         UltraEdit.document[nDocIndex].copy();
         CreateChapterFile();
         nFirstLine = nLastLine + 1;
         UltraEdit.document[nDocIndex].gotoLine(nFirstLine,1);
         g_nChapter++;
      }

      if(UltraEdit.document[nDocIndex].findReplace.find("</body>"))
      {
         nLastLine = UltraEdit.document[nDocIndex].currentLineNum - 1;
         UltraEdit.document[nDocIndex].gotoLine(nLastLine,1);
         UltraEdit.document[nDocIndex].gotoLineSelect(nFirstLine,1);
         UltraEdit.document[nDocIndex].copy();
         CreateChapterFile();
      }

      UltraEdit.document[nDocIndex].top();
      UltraEdit.clearClipboard();
      UltraEdit.selectClipboard(0);
   }
}

The separator tag <hr class="sigil_marker"/> is not written into any created file.
Best regards from Austria
2 posts Page 1 of 1