With the input and output example it was clear what to do. I wrote 2 scripts.
The first one below runs a Perl regular expression Replace in Files on all *.edi files in C:\Temp\ to remove all transactions from the files not having the
RECVPARTNER number which is
- predefined in script at top, or
- determined from a selection in active file on script execution starting with RECVPARTNER and having 1 or more digits, or
- is entered by the user of the script.
You can of course define a different directory for the script and a different file type specification.
How many transactions have been removed from which files is written into the output window which is not automatically opened by the script.
Code: Select all
var sNumber = "";
// Must the script user enter the search string (or is it predefined)?
if (!sNumber.length)
{
if (UltraEdit.document.length > 0) // Is any file opened?
{
UltraEdit.insertMode();
if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();;
// Is there a selection in active file?
if (UltraEdit.activeDocument.isSel())
{
// Get the selected text and use it if it starts with "RECVPARTNER".
sNumber = UltraEdit.activeDocument.selection;
// Does the selected text start with string "RECVPARTNER" and
// has 1 or more digits and nothing else to end of selection?
if (sNumber.search(/^RECVPARTNER\d+$/) != 0)
{
sNumber = ""; // No, ignore selected text.
}
else
{
sNumber = sNumber.replace(/RECVPARTNER/,"");
}
}
}
}
// Is the number whether predefined nor no correct selection?
if (!sNumber.length)
{
// Ask user of script for the number.
sNumber = UltraEdit.getString("Enter RECVPARTNER number with all digits:",1);
if (sNumber.length)
{
// The number must consist of digits and no other character.
if (sNumber.search(/^\d+$/) != 0)
{
sNumber = "";
}
}
}
// Is a number now defined?
if (sNumber.length)
{
// Define the parameters for the Perl regular expression Replace in Files.
UltraEdit.perlReOn();
UltraEdit.frInFiles.filesToSearch=0;
UltraEdit.frInFiles.searchSubs=false;
UltraEdit.frInFiles.searchInFilesTypes="*.edi";
UltraEdit.frInFiles.directoryStart="C:\\Temp\\";
UltraEdit.frInFiles.ignoreHiddenSubs=false;
UltraEdit.frInFiles.unicodeSearch=false;
UltraEdit.frInFiles.preserveCase=false;
UltraEdit.frInFiles.logChanges=true;
UltraEdit.frInFiles.matchWord=false;
UltraEdit.frInFiles.matchCase=true;
UltraEdit.frInFiles.regExp=true;
// Build the entire Perl regular expression search string to delete
// all transactions except those with entered RECVPARTNER number.
var sSearchString = "^ISA\\*.+?RECVPARTNER(?!" + sNumber + ")[\\s\\S]+?IEA\\*\\d.+\\s*";
UltraEdit.frInFiles.replace(sSearchString,"");
}
I wrote a second script based on
FindStringsToNewFile.js which copies all found transactions with predefined, selected or entered
RECVPARTNER number from active file only into a new file.
It would be of course possible to extended this script to run on a list of files, see
GetListOfFiles, or all open files.
Code: Select all
var sSearchString = "";
//== OutputMessage =========================================================
// This function displays a message in a message box or write the message
// into a new file for users of UltraEdit v13.00 or UEStudio v6.20.
function OutputMessage (sMessageText)
{
if (UltraEdit.messageBox) UltraEdit.messageBox(sMessageText);
else
{
UltraEdit.newFile();
UltraEdit.activeDocument.unixMacToDos();
UltraEdit.activeDocument.unicodeToASCII();
UltraEdit.activeDocument.write(sMessageText.replace(/\n/g,"\r\n"));
UltraEdit.activeDocument.insertLine();
}
}
//== FindTransactionsToNewFile =============================================
if (UltraEdit.document.length > 0) // Is any file currently opened?
{
UltraEdit.insertMode(); // Define environment for the script.
if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
UltraEdit.activeDocument.hexOff();
// Must the script user enter the search string (or is it predefined)?
if (!sSearchString.length)
{
// Is there a selection in active file?
if (UltraEdit.activeDocument.isSel())
{
// Get the selected text and use it if it starts with "RECVPARTNER".
sSearchString = UltraEdit.activeDocument.selection;
// Does the selected text start with string "RECVPARTNER" and
// has 1 or more digits and nothing else to end of selection?
if (sSearchString.search(/^RECVPARTNER\d+$/) != 0)
{
sSearchString = ""; // No, ignore selected text.
}
}
}
if (!sSearchString.length)
{
// Otherwise ignore the selection and ask user of script for the number.
sSearchString = UltraEdit.getString("Enter RECVPARTNER number with all digits:",1);
if (sSearchString.length)
{
// The number must consist of digits and no other character.
if (sSearchString.search(/^\d+$/) == 0)
{
sSearchString = "RECVPARTNER" + sSearchString;
}
else
{
sSearchString = "";
}
}
}
if (sSearchString.length)
{
// Make sure that last line of file has a line termination.
UltraEdit.activeDocument.bottom();
if (UltraEdit.activeDocument.isColNumGt(1))
{
UltraEdit.activeDocument.insertLine();
}
// Build the entire regular expression search string.
sSearchString = "ISA\\*.+?" + sSearchString + "[\\s\\S]+?IEA\\*\\d.+\\s+";
// Search entire file with a case sensitive regular expression.
// As the script user can define also an invalid regular expression
// via wrong selection, catch this error and inform the script user
// about this mistake.
var bValidExpression = true;
try
{
var rRegSearch = new RegExp(sSearchString,"g");
}
catch (Error)
{
OutputMessage("The entered search expression is an invalid regular expression.\n\n"+Error);
bValidExpression = false;
}
if (bValidExpression)
{
// Search entire file with a not case sensitive regular expression
// search which puts all found strings into an array of strings.
UltraEdit.activeDocument.selectAll();
// An error in the line below on script execution is caused usually by
// a too large file for this script, use FindStringsToNewFileExtended.js.
var asFoundTransactions = UltraEdit.activeDocument.selection.match(rRegSearch);
UltraEdit.activeDocument.top(); // Cancel selection.
if (asFoundTransactions != null) // Was any string found?
{
UltraEdit.newFile(); // Open a new file.
// If version of UE/UES supports direct write to clipboard,
// use user clipboard 9 to paste the found strings into the
// new file as this is much faster than using write command.
if (typeof(UltraEdit.clipboardContent) == "string")
{
var nActiveClipboard = UltraEdit.clipboardIdx;
UltraEdit.selectClipboard(9);
UltraEdit.clipboardContent = asFoundTransactions.join("");
UltraEdit.activeDocument.paste();
UltraEdit.clearClipboard();
UltraEdit.selectClipboard(nActiveClipboard);
}
else UltraEdit.activeDocument.write(asFoundTransactions.join(""));
// Set caret to top of file with found strings and inform user
// about number of strings found with the entered expression.
UltraEdit.activeDocument.top();
var nCount = asFoundTransactions.length;
OutputMessage("Found "+nCount+" transaction"+(nCount==1?".":"s."));
}
else OutputMessage("No transaction found!");
}
}
else OutputMessage("No RECVPARTNER number entered!");
}
else OutputMessage("You should have a file opened when you run this script!");