Hi Mofi
I am very much thankful to you for your kind cooperation.
I was trying to update the script of by myself.
I added the two additional requirements to the script by myself:
- To create backup at the same directory with ".backup" extension and same file name before execute the replaces.
- To enlisted the summary of find replaces which are not execute in the files.
Here is the updated script:
Code: Select all
// These 3 variables can be predefined to avoid user prompts on script execution.
var sListFile = ""; // "C:\\Temp\\Test\\find_replace_list.txt";
var sDirectory = ""; // "C:\\Temp\\Test\\input\\";
var sFileExtension = ""; // "*.htm";
// The script code below works only for a list file with DOS/Windows line endings.
var asListFileData = null;
var nReplaceCount = 0;
var bOldVersion = (typeof(UltraEdit.activeDocumentIdx) == "undefined") ? true : false;
if (UltraEdit.document.length > 0) // Is any file opened?
{
// Define environment for this script.
UltraEdit.insertMode();
if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
// Get caret position in active file.
var nLine = UltraEdit.activeDocument.currentLineNum;
var nColumn = UltraEdit.activeDocument.currentColumnNum;
if (bOldVersion) nColumn++;
// Move caret to top of the active file.
UltraEdit.activeDocument.top();
UltraEdit.perlReOn();
UltraEdit.activeDocument.findReplace.mode = 0;
UltraEdit.activeDocument.findReplace.matchCase = true;
UltraEdit.activeDocument.findReplace.matchWord = false;
UltraEdit.activeDocument.findReplace.regExp = true;
UltraEdit.activeDocument.findReplace.searchDown = true;
if (typeof(UltraEdit.activeDocument.findReplace.searchInColumn) == "boolean")
{
UltraEdit.activeDocument.findReplace.searchInColumn = false;
}
// Does the active file contain at least 3 lines in required format for list file?
if(UltraEdit.activeDocument.findReplace.find("^(?:.+\\t(?:i|iu|r|ri|u|ui)?\\t.*\\r\\n){3}"))
{
// Get the lines in active file into a string array.
UltraEdit.activeDocument.selectAll();
asListFileData = UltraEdit.activeDocument.selection.split("\r\n");
}
// Restore initial caret position in active file.
UltraEdit.activeDocument.gotoLine(nLine,nColumn);
}
// Are the list file data not loaded already from active file?
if (asListFileData == null)
{
// Is a list file name not predefined in script file, let
// user of script open any file and hope it is the list file.
if (!sListFile.length)
{
var nDocCount = UltraEdit.document.length;
UltraEdit.open("");
// Has the user opened an additional file at all?
if (UltraEdit.document.length > nDocCount)
{
// Get the lines in active file into a string array.
UltraEdit.activeDocument.selectAll();
if (UltraEdit.activeDocument.isSel())
{
asListFileData = UltraEdit.activeDocument.selection.split("\r\n");
}
// Close the opened list file no longer needed.
UltraEdit.closeFile(UltraEdit.activeDocument.path,2);
}
}
else // The name of the list file with full path is predefined in script.
{
var nDocCount = UltraEdit.document.length;
var sFileNameWithPath = sListFile.toLowerCase();
for (var nDocIndex = 0; nDocIndex < nDocCount; nDocIndex++)
{
// Is the list file opened already in UltraEdit?
if (UltraEdit.document[nDocIndex].path.toLowerCase() == sFileNameWithPath)
{
// Get caret position in opened list file.
var nLine = UltraEdit.document[nDocIndex].currentLineNum;
var nColumn = UltraEdit.document[nDocIndex].currentColumnNum;
if (bOldVersion) nColumn++;
// Get the lines in opened list file into a string array.
UltraEdit.document[nDocIndex].selectAll();
if (UltraEdit.document[nDocIndex].isSel())
{
asListFileData = UltraEdit.document[nDocIndex].selection.split("\r\n");
}
UltraEdit.document[nDocIndex].gotoLine(nLine,nColumn);
break;
}
}
// Is the list file not opened already?
if (nDocIndex == nDocCount)
{
UltraEdit.open(sListFile);
// Could the file be opened successfully?
if (UltraEdit.document.length > nDocIndex)
{
// Get the lines in active file into a string array.
UltraEdit.activeDocument.selectAll();
if (UltraEdit.activeDocument.isSel())
{
asListFileData = UltraEdit.activeDocument.selection.split("\r\n");
}
UltraEdit.closeFile(UltraEdit.activeDocument.path,2);
}
}
}
}
// Are the list file data loaded from active, predefined or specified list file?
if (asListFileData != null)
{
var bCreatedNewFile = false;
// Is a directory path not predefined in script file, ask user
// of the script for the directory path of the files to modify.
while (!sDirectory.length)
{
if (UltraEdit.document.length < 1)
{
UltraEdit.newFile();
bCreatedNewFile = true;
}
sDirectory = UltraEdit.getString("Enter directory path of files to modify:",1);
}
// Make sure the directory path ends with a backslash.
if (sDirectory[sDirectory.length-1] != '\\') sDirectory += '\\';
// Is the file name pattern not predefined in script file, ask
// user of the script for the file extension/file name pattern.
while (!sFileExtension.length)
{
if (UltraEdit.document.length < 1)
{
UltraEdit.newFile();
bCreatedNewFile = true;
}
sFileExtension = UltraEdit.getString("Enter file name pattern (extension) like *.htm:",1);
}
// Define the replace in files options never modified.
UltraEdit.frInFiles.filesToSearch = 0;
UltraEdit.frInFiles.searchSubs = false;
UltraEdit.frInFiles.directoryStart = sDirectory;
UltraEdit.frInFiles.searchInFilesTypes = sFileExtension;
UltraEdit.frInFiles.openMatchingFiles = false;
UltraEdit.frInFiles.ignoreHiddenSubs = true;
UltraEdit.frInFiles.logChanges = true;
UltraEdit.frInFiles.matchWord = false;
UltraEdit.frInFiles.preserveCase = false;
UltraEdit.frInFiles.useEncoding = true;
UltraEdit.frInFiles.encoding = 65001;
// ***** new update start *****
UltraEdit.outputWindow.clear();
UltraEdit.frInFiles.useOutputWindow=true;
UltraEdit.frInFiles.find('');
UltraEdit.outputWindow.copy();
UltraEdit.newFile();
UltraEdit.activeDocument.paste();
UltraEdit.clearClipboard();
UltraEdit.selectClipboard(0);
UltraEdit.activeDocument.top();
UltraEdit.perlReOn();
UltraEdit.activeDocument.findReplace.mode=0;
UltraEdit.activeDocument.findReplace.matchCase=false;
UltraEdit.activeDocument.findReplace.matchWord=false;
UltraEdit.activeDocument.findReplace.regExp=true;
UltraEdit.activeDocument.findReplace.searchDown=true;
UltraEdit.activeDocument.findReplace.replaceAll=true;
UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
UltraEdit.activeDocument.top();
UltraEdit.activeDocument.findReplace.replace("^Search complete, found.+\\r\\n","");
UltraEdit.activeDocument.findReplace.replace("^(.+.)\\.(\\w+)$","copy \\1\.\\2 \\1\.bak");
var batFile = "c:\\temp\\bak.bat";
var toolName = "backup";
var nPath = UltraEdit.activeDocument.selection;
nPath = nPath.replace(/\\/g,"\\\\");
UltraEdit.saveAs(batFile);
UltraEdit.closeFile(UltraEdit.activeDocument.path,2);
UltraEdit.runTool(toolName);
UltraEdit.outputWindow.clear();
// ***** new update end *****
for (var nLine = 0; nLine < asListFileData.length; nLine++)
{
// Is the active line in array empty, skip the line.
if (!asListFileData[nLine].length) continue;
// Split the line up into data strings
var asData = asListFileData[nLine].split('\t');
// Skip the line on less than 2 tab characters.
if (asData.length < 3) continue;
// Skip the line on search string is an empty string.
if (!asData[0].length) continue;
// Determine the variable replace in files options for next execution.
var bApplyWorkaround = true;
var sOptions = asData[1].toLowerCase();
if (sOptions == "i")
{
UltraEdit.frInFiles.regExp = true;
UltraEdit.frInFiles.matchCase = true;
UltraEdit.perlReOn();
bApplyWorkaround = false;
}
else if ((sOptions == "iu") || (sOptions == "ui"))
{
UltraEdit.frInFiles.regExp = true;
UltraEdit.frInFiles.matchCase = true;
UltraEdit.ueReOn();
}
else if (sOptions == "u")
{
UltraEdit.frInFiles.regExp = true;
UltraEdit.frInFiles.matchCase = false;
UltraEdit.ueReOn();
}
else if (sOptions == "r")
{
UltraEdit.frInFiles.regExp = false;
UltraEdit.frInFiles.matchCase = false;
UltraEdit.ueReOn();
}
else if ((sOptions == "ir") || (sOptions == "ri"))
{
UltraEdit.frInFiles.regExp = false;
UltraEdit.frInFiles.matchCase = true;
UltraEdit.ueReOn();
}
else
{
UltraEdit.frInFiles.regExp = true;
UltraEdit.frInFiles.matchCase = false;
UltraEdit.perlReOn();
bApplyWorkaround = false;
}
// Output find and replace string in output window.
UltraEdit.outputWindow.write('Find: "' + asData[0] + '" Replace: "' + asData[2] + '"');
// Workaround for a bug in older versions of UE and UES which do
// not correct interpret ^p, ^r, ^n, ^t and ^b in replace string
// of a replace in files executed from within a script. In search
// string those special escape sequences are interpreted as expected.
if (bApplyWorkaround)
{
// Replace all ^p by carriage return + line-feed.
var sReplace = asData[2].replace(/\^p/g,"\r\n");
sReplace = sReplace.replace(/\^r/g,"\r"); // carriage return
sReplace = sReplace.replace(/\^n/g,"\n"); // line-feed
sReplace = sReplace.replace(/\^t/g,"\t"); // horizontal tab
sReplace = sReplace.replace(/\^b/g,"\f"); // form-feed
asData[2] = sReplace;
}
// Run the replace in files without evaluation of results.
UltraEdit.frInFiles.replace(asData[0],asData[2]);
nReplaceCount++;
}
// Close the new file just opened to be able to prompt user for directory
// path and file extension loaded into variables in older versions of UE.
if (bCreatedNewFile)
{
UltraEdit.closeFile(UltraEdit.activeDocument.path,2);
}
UltraEdit.clearClipboard();
UltraEdit.selectClipboard(0);
}
// Open the output window if any replace in files was executed at all.
if (nReplaceCount) UltraEdit.outputWindow.showWindow(true);
// Show a small summary output with number of replace in files executed.
var sPluralS = (nReplaceCount != 1) ? "s" : "";
UltraEdit.messageBox("Executed " + nReplaceCount + " replace" + sPluralS + " in files.");
//***** new update start *****
UltraEdit.outputWindow.copy();
UltraEdit.newFile();
UltraEdit.activeDocument.top();
UltraEdit.activeDocument.paste();
UltraEdit.clearClipboard();
UltraEdit.selectClipboard(0);
UltraEdit.activeDocument.top();
UltraEdit.insertMode();
if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
UltraEdit.perlReOn();
UltraEdit.activeDocument.findReplace.mode=0;
UltraEdit.activeDocument.findReplace.matchCase=true;
UltraEdit.activeDocument.findReplace.matchWord=false;
UltraEdit.activeDocument.findReplace.regExp=true;
UltraEdit.activeDocument.findReplace.searchDown=true;
UltraEdit.activeDocument.findReplace.replaceAll=true;
UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
UltraEdit.activeDocument.findReplace.replace("Find: \"([^\r\n]*?)\" Replace: \"([^\r\n]*?)\"\\r\\n0 items replaced in 0 files\\.","@@\\1\\t\\t\\2");
UltraEdit.activeDocument.findReplace.replace("^[^@].+\\r\\n","");
UltraEdit.activeDocument.findReplace.replace("^@+","");
//***** new update end *****
It will be my pleasure to have your kind cooperation in future.
Thanks