Here is a customized script which I think fulfills all your requirements.
The strings assigned to the variables
g_sListFile and
g_sFolderDefault must be defined by you. The variables
g_sFolderReplace and
sLogFileName can be defined with a non-empty string to avoid the prompt for the base folder path respectively automatically save and close the log file.
Code: Select all
// Define fixed strings used globally.
var g_asFindReplaceList; // Array of find and replace strings.
var g_sLogFileContents;
var g_sListFile = "D:\\redaction\\redactionlist.csv";
var g_sFolderDefault = "D:\\Base\\Folder\\";
// Assign g_sFolderDefault or a different folder path instead of
// an empty string to avoid the prompt for the base folder path.
var g_sFolderReplace = ""; // Path to folder with files to modify.
// Define fixed strings used only in main code.
var sSeparator = "\t";
// Specify here a valid log file name with valid
// full path for an automatic save and close.
var sLogFileName = "";
// === GetFindReplaceList ====================================================
// This function reads the list of find and replace strings from CSV file.
// It prompts the user also for the folder path if not defined at top.
function GetFindReplaceList()
{
var nColumnNumber;
var nLineNumber = 0;
var nListFile = -1;
// Is any file opened?
if (UltraEdit.document.length > 0)
{
// Search in list of opened files for the list file.
// Compare the full qualified file names case-insensitive.
var sListFile = g_sListFile.toLowerCase();
for (var nDocIndex = 0; nDocIndex < UltraEdit.document.length; nDocIndex++)
{
if (UltraEdit.document[nDocIndex].path.length != sListFile.length) continue;
if (UltraEdit.document[nDocIndex].path.toLowerCase() != sListFile) continue;
nLineNumber = UltraEdit.document[nDocIndex].currentLineNum;
nColumnNumber = UltraEdit.document[nDocIndex].currentColumnNum;
// This line is just for UltraEdit < v16.00 and UES < v10.00.
if (typeof(UltraEdit.activeDocumentIdx) == "undefined") nColumnNumber++;
nListFile = nDocIndex;
break;
}
}
// Is the list file not already opened?
if (nListFile < 0)
{
nListFile = UltraEdit.document.length;
UltraEdit.open(g_sListFile);
// Is the list file still not opened?
if (nListFile == UltraEdit.document.length)
{
g_asFindReplaceList = [];
UltraEdit.outputWindow.write("ERROR: Failed to open file: " + g_sListFile);
UltraEdit.outputWindow.showWindow(true);
return;
}
}
// Get all lines in the list file as an array of strings
// independent on the type of line endings (DOS/Unix/Mac).
UltraEdit.document[nListFile].selectAll();
if (UltraEdit.document[nListFile].isSel())
{
g_asFindReplaceList = UltraEdit.document[nListFile].selection.replace(/\r\n|\r/g,"\n").split("\n");
}
else
{
g_asFindReplaceList = [];
UltraEdit.outputWindow.write("ERROR: There is nothing in file: " + g_sListFile);
UltraEdit.outputWindow.showWindow(true);
}
// Prompt the user with at least one file opened now
// for the folder path if not already defined at top.
if (g_asFindReplaceList.length && !g_sFolderReplace.length)
{
g_sFolderReplace = UltraEdit.getString("The default base folder path is:\n"+g_sFolderDefault+"\nPlease enter base folder path:",1);
if (g_sFolderReplace.length)
{
// Replace all / by \ in user input folder path.
g_sFolderReplace = g_sFolderReplace.replace(/\x2F/g,"\\");
}
else
{
g_sFolderReplace = g_sFolderDefault;
}
}
if (nLineNumber)
{
// Restore initial caret position in already opened list file.
UltraEdit.document[nListFile].gotoLine(nLineNumber,nColumnNumber);
}
else
{
// Close the list file just opened before.
UltraEdit.closeFile(UltraEdit.document[nListFile].path,2);
}
}
// === GetFilesList ==========================================================
// This function gets the list of files on which the replaces are done.
function GetFilesList()
{
// The folder path must end with a backslash.
if (g_sFolderReplace[g_sFolderReplace.length-1] != "\\")
{
g_sFolderReplace += "\\";
}
// Run a Find in Files with an empty search string to get the
// list of files in specified directory and all its subdirectories.
UltraEdit.outputWindow.clear();
UltraEdit.frInFiles.useOutputWindow=true;
UltraEdit.frInFiles.filesToSearch=0;
UltraEdit.frInFiles.searchInFilesTypes="*";
UltraEdit.frInFiles.directoryStart=g_sFolderReplace;
UltraEdit.frInFiles.ignoreHiddenSubs=false;
UltraEdit.frInFiles.displayLinesDoNotMatch=false;
UltraEdit.frInFiles.openMatchingFiles=false;
UltraEdit.frInFiles.reverseSearch=false;
UltraEdit.frInFiles.useEncoding=false;
UltraEdit.frInFiles.searchSubs=true;
UltraEdit.frInFiles.matchCase=false;
UltraEdit.frInFiles.matchWord=false;
UltraEdit.frInFiles.regExp=true;
UltraEdit.frInFiles.find("");
// Copy the files list written to output window to the
// user clipboard 9 of UltraEdit and clear output window.
UltraEdit.selectClipboard(9);
UltraEdit.outputWindow.copy();
UltraEdit.outputWindow.clear();
// Get the files list from user clipboard 9 without the
// summary on last line. The string must be adapted on
// using not English version of UltraEdit or UEStudio.
var sFilesList = UltraEdit.clipboardContent.replace(/Search complete[\s\S]+$/,"");
// If there was nothing output than the summary by command
// Find in Files, the folder does not exist or there is no file.
if (!sFilesList.length)
{
g_asFindReplaceList = [];
UltraEdit.clearClipboard();
UltraEdit.selectClipboard(0);
UltraEdit.outputWindow.write("ERROR: No file found in folder: " + g_sFolderReplace);
UltraEdit.outputWindow.showWindow(true);
}
else
{
g_sLogFileContents = "Files processed:\n\n" + sFilesList;
}
}
// === Main ==================================================================
// Define environment for this script.
UltraEdit.insertMode();
if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
GetFindReplaceList();
if (g_asFindReplaceList.length) GetFilesList();
if (g_asFindReplaceList.length)
{
// Define the parameters for Replace in Files which were
// not already defined for Find in Files in GetFilesList.
UltraEdit.frInFiles.logChanges=true;
UltraEdit.frInFiles.preserveCase=false;
// Declare the variables used in the loop below.
var nSeparator; // Character index position of first separator occurrence
var sFind; // String to find by the command Replace in Files
var sFindReplace; // Next line from list file with next find and replace
var sReplace; // Replace string to use by the command Replace in Files
for (var nFindReplace = 0; nFindReplace < g_asFindReplaceList.length; nFindReplace++)
{
sFindReplace = g_asFindReplaceList[nFindReplace];
// Ignore empty lines in list file. Empty lines can be used in
// list file to create blocks to structure somehow the list.
if (!sFindReplace.length) continue;
nSeparator = sFindReplace.indexOf(sSeparator);
// Is there a separator not at beginning of the line?
if (nSeparator > 0)
{
sFind = sFindReplace.substr(0,nSeparator);
// The replace string can be an empty string!
sReplace = sFindReplace.substr(nSeparator+1);
}
// Is there no separator in the line?
else if (nSeparator < 0)
{
sFind = sFindReplace;
// The replace string is an empty string.
sReplace = "";
}
else continue; // Ignore lines starting with the separator. Such
// lines can be used for comments in list file.
// Run the case-insensitive Replace in Files.
UltraEdit.frInFiles.replace(sFind,sReplace);
// Append the find and replace strings to log file contents.
g_sLogFileContents += '\nReplace "' + sFind + '" by "' + sReplace + '":\n';
// Get the log of command Replace in Files and append it, too.
UltraEdit.outputWindow.copy();
UltraEdit.outputWindow.clear();
g_sLogFileContents += UltraEdit.clipboardContent;
}
// Is there anything to write into the log file?
if (g_sLogFileContents.length)
{
// Copy the log file contents to user clipboard 9.
UltraEdit.clipboardContent = g_sLogFileContents;
// Create a new file, make sure it is a file with DOS/Windows
// line endings and paste the log file contents into the file
// with an automatic conversion of the line endings to DOS
// if not disabled in configuration of UltraEdit / UEStudio.
UltraEdit.newFile();
UltraEdit.activeDocument.unixMacToDos();
UltraEdit.activeDocument.paste();
// Is there a log file name defined at top of the script.
if (sLogFileName.length)
{
// Save the new file with that file name.
UltraEdit.saveAs(sLogFileName);
// Was the file saving really successful?
if (sLogFileName.toLowerCase() == UltraEdit.activeDocument.path.toLowerCase())
{
// Close the log file and inform the user in output
// window about successful saving of log file.
UltraEdit.closeFile(UltraEdit.activeDocument.path,2);
UltraEdit.outputWindow.write("Replace results written to: " + UltraEdit.activeDocument.path);
UltraEdit.outputWindow.showWindow(true);
}
} // Otherwise the new file remains open for user verification.
}
// Clear the user clipboard 9 and select Windows clipboard.
UltraEdit.clearClipboard();
UltraEdit.selectClipboard(0);
}
The CSV file can contain empty lines which are ignored by the code, for example to structure the find and replace strings a little bit in blocks.
The CSV file can have also lines with first character being a horizontal tab which are also ignored making it possible to comment a line in CSV file.
Please note that case-insensitive non-regular expression replaces are executed which means the symbols of special character summary on help page about
Find command or
Replace command can be also used in the find and replaces strings stored in the tab-separated values file with the exception of
^c and
^s which make no sense in this case.