The first script posted by me on 2012-03-27 should not be used as it is horrible slow on splitting up a very large file into several files also with large blocks. The performance of UltraEdit for Windows prior v18.20.0.1017 on writing with scripting command write a large block into a file was really horrible. The command paste as used by me in the enhanced version posted on 2014-01-23 (and slightly modified by me today) is the much better version.
The script below is based on the script posted by me on 2014-01-23 and works also with UltraEdit for Windows v15.10.0.1018.
The script was tested by me on an ASCII text file with 28,207,453 lines and a total file size of 1,455,402,312 bytes (1.36 GiB). The script split up the huge file into files with 1,048,576 lines per file without copying first line into each file producing 27 files with a file size between 48,730,850 bytes (46.47 MiB - last file) and 54,104,617 bytes (51.60 MiB - largest file). UltraEdit for Windows v15.10.0.1018 running on my 12 years old notebook with just 2 GiB RAM and with a slow HDD took 15 minutes to complete the task with no window update while running the script. I surfed in world wide web while UltraEdit executed the script to use the time. So be patient on running the script on your huge file. The non-responsive user interface (UltraEdit application window) does not mean that UltraEdit stopped working or ran into an endless loop.
The script below is based on the script posted by me on 2014-01-23 and works also with UltraEdit for Windows v15.10.0.1018.
Code: Select all
// First file (most left on file tabs bar) must be the file to split.
if (UltraEdit.document.length > 0) { // Is any file opened?
var bCopyFirstLine = false; // Copy first line into every file?
// NO = false, YES = true.
var nLinesPerFile = 1048576; // Number of lines per file without the
// first line on copying it to all files.
var nFileCount = 0; // Counts the files created by this script.
var nNextColumnNum = 1; // Column number for next block selection.
var nNextLineNum = nLinesPerFile + 1; // Line number for next block selection.
/* UE for Windows < v16.00 and UES < v10.00 return the current column
number with 0 for first column in a line instead of 1 as needed to
move caret to first column in a line. This difference is taken into
account with this version dependent column offset being 1 or 0. */
var nColumnOffset = (typeof(UltraEdit.activeDocumentIdx) == "undefined") ? 1 : 0;
// Define the environment for the script.
UltraEdit.insertMode();
if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
UltraEdit.document[0].hexOff();
// Move caret to end of the file and get its line and column number.
UltraEdit.document[0].bottom();
var nLastLineNum = UltraEdit.document[0].currentLineNum;
var nLastColumNum = UltraEdit.document[0].currentColumnNum + nColumnOffset;
// Move caret to top of the file.
UltraEdit.document[0].top();
// Do not copy first line if the first file contains just one line.
if ((nLastLineNum < 2) || ((nLastLineNum == 2) && (nLastColumNum == 1))) bCopyFirstLine = false;
if (bCopyFirstLine) {
// Code to copy first line of file into user clipboard 8 for pasting
// it later into every file created on splitting the large file.
UltraEdit.selectClipboard(8);
UltraEdit.document[0].selectLine();
UltraEdit.document[0].copy();
UltraEdit.document[0].endSelect();
UltraEdit.document[0].gotoLine(2,1);
nNextLineNum++;
}
UltraEdit.selectClipboard(9);
// Quick and dirty solution to get file name without extension
// and the file extension. Does not work for all file names.
var nLastPoint = UltraEdit.document[0].path.lastIndexOf('.');
if (nLastPoint < 0) nLastPoint = UltraEdit.document[0].path.length;
var sFileName = UltraEdit.document[0].path.substr(0,nLastPoint) + '_';
var sFileExt = UltraEdit.document[0].path.substr(nLastPoint);
// If the first file is an empty file, do not create a new file.
var bExitLoop = ((nLastLineNum == 1) && (nLastColumNum == 1)) ? true : false;
while (!bExitLoop) {
// Determine if next to select the defined number of lines or just
// the remaining text up to end of the file and do the selection.
if (nNextLineNum > nLastLineNum)
{
nNextLineNum = nLastLineNum;
nNextColumnNum = nLastColumNum;
bExitLoop = true;
}
UltraEdit.document[0].gotoLineSelect(nNextLineNum,nNextColumnNum);
// Copy the selected block to user clipboard 9.
UltraEdit.document[0].copy();
UltraEdit.newFile();
if (bCopyFirstLine) {
// Paste the first line first into the new file.
UltraEdit.selectClipboard(8);
UltraEdit.activeDocument.paste();
UltraEdit.selectClipboard(9);
}
// Paste the file copied block into the new file.
UltraEdit.activeDocument.paste();
nFileCount++;
UltraEdit.saveAs(sFileName + nFileCount + sFileExt);
UltraEdit.closeFile(UltraEdit.activeDocument.path,2);
// Solution for UltraEdit for Windows < v17.20 with no support for
// the function cancelSelect() tested only with UE v15.10.0.1018.
UltraEdit.document[0].endSelect();
UltraEdit.document[0].gotoLine(nNextLineNum,1);
// Break the loop if the caret is now at end of file.
if ((nNextLineNum == nLastLineNum) && (nNextColumnNum == nLastColumNum)) break;
// Increment the line number for expected end of next block.
nNextLineNum += nLinesPerFile;
}
// Move file the caret back to top of the file. That would
// not be really necessary, but is done usually very fast.
UltraEdit.document[0].top();
// Clear the used user clipboards and reselect system clipboard.
UltraEdit.clearClipboard();
if (bCopyFirstLine) {
UltraEdit.selectClipboard(8);
UltraEdit.clearClipboard();
}
UltraEdit.selectClipboard(0);
UltraEdit.messageBox(nFileCount + " files created.");
}
Best regards from an UC/UE/UES for Windows user from Austria
Blarneystone likes this post