Thanks again Mofi,
But unfortunately I'm not using UltraEdit v22.20 or v26.10, I'm using UEStudio v12.20.
By the way I want to try replace in files with loop by following pattern replace:
Find:
Code: Select all
(?s)(<(div)( class="(?(4)[^"]*?|(?|(sec[A-Z])))")>((?>(?:(?!<\2\b)(?!</\2\b).)++|(?1))*+)</\2>)
Replace:
and try to create a script UEStudio v12.20 like:
Code: Select all
var sDirectory = "";
var sFileExtension = "";
sDirectory = UltraEdit.getString("Enter directory path of files to modify:",1);
//sDirectory = sDirectory.replace(/(\/)/g,"\1\1");
sFileExtension = UltraEdit.getString("Enter file name pattern (extension) like *.htm:",1);
if (sFileExtension[0] != '*') sFileExtension = '*' + sFileExtension;
if (sFileExtension[1] != '.') sFileExtension = "*." + sFileExtension.substr(1);
var file_names = get_array_of_files(sDirectory, sFileExtension);
var no_of_files = file_names.length;
// ***** for backup *****
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();
// ***** main update *****
UltraEdit.perlReOn();
UltraEdit.frInFiles.regExp = true;
UltraEdit.frInFiles.matchCase = false;
UltraEdit.frInFiles.filesToSearch = 0;
UltraEdit.frInFiles.searchSubs = false;
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;
for (var i = 0; i < no_of_files; i++) {
//UltraEdit.messageBox(file_names[i]);
UltraEdit.frInFiles.replace('(?s)(<(div)( class="(?(4)[^"]*?|(?|(sec[A-Z])))")>((?>(?:(?!<\\2\\b)(?!</\\2\\b).)++|(?1))*+)</\\2>)',"<section\\3>\\5</section>");
}
function get_array_of_files(dir_path, ext) {
GetListOfFiles(0, dir_path, ext, true);
UltraEdit.activeDocument.top();
UltraEdit.activeDocument.selectAll();
var file_names = UltraEdit.activeDocument.selection.split("\r\n");
UltraEdit.closeFile(UltraEdit.activeDocument.path, 2);
return file_names;
}
function GetListOfFiles (nFileList, sDirectory, sFileType, bSubDirs)
{
/* The summary info line at end of a Search - Find in Files result is
language depended as also the name of the results document. Adapt
the following 2 strings to your version of UE/UES. Take a look
in Configuration/Preferences on "Search - Set Find Output Format"
on "Find Summary" definition, or better execute once the command
"Find in Files" from menu "Search" and adapt the two strings below
accordingly. You can also just open GetListOfFiles.js and execute
it with command "Run Active Script" from menu "Scripting" to see
how the find results look like in your version of UE/UES when you
enter during script execution the parameters correct.
Please note that with disabling the "Find Summary" completely you
have to initialize variable sSummaryInfo with an empty string. */
var sSummaryInfo = "Search complete, found";
var sResultsDocTitle = "** Find Results ** "; // Note the space at end!
// For German UltraEdit the default strings are:
// var sSummaryInfo = "Suche abgeschlossen, ";
// var sResultsDocTitle = "** Suchergebnisse ** ";
/* Determine the type of output for debug messages from the global
variable g_nDebugMessage: 1 ... debug to output window, 2 ... debug
to message dialog, all others ... no debug messages. If the global
variable g_nDebugMessage does not exist, display the debug message
as popup message in a dialog box (value 2). */
var nOutputType = (typeof(g_nDebugMessage) == "number") ? g_nDebugMessage : 2;
if (typeof(nFileList) != "number" || nFileList < 0 || nFileList > 4) nFileList = 0;
if (nFileList == 0) // Search in a specified directory?
{
// If no directory specified, use current working directory.
if (typeof(sDirectory) != "string" || sDirectory == "" ) sDirectory = ".\\";
// Append a backslash if it is missing at end of the directory string.
else if (sDirectory[sDirectory.length-1] != "\\") sDirectory += "\\";
// Search for all files if no file type is specified.
if (typeof(sFileType) != "string" || sFileType == "") sFileType = "*";
if (typeof(bSubDirs) != "boolean") bSubDirs = false;
}
else
{
sDirectory = ""; // For the list of open, favorite, project
sFileType = ""; // or solution files the other 3 parameters
bSubDirs = false; // have always the same default values.
}
// Remember current regular expression engine.
var nRegexEngine = UltraEdit.regexMode;
/* A regular expression engine must be defined or the find
for the last line in the Unicode results could fail. */
UltraEdit.ueReOn();
/* Run a Find In Files with an empty search string to get the
list of files stored in the specified directory in an edit
window and delete the last line with the summary info. */
UltraEdit.frInFiles.directoryStart=sDirectory;
UltraEdit.frInFiles.filesToSearch=nFileList;
UltraEdit.frInFiles.matchCase=false;
UltraEdit.frInFiles.matchWord=false;
UltraEdit.frInFiles.regExp=false;
UltraEdit.frInFiles.searchInFilesTypes=sFileType;
UltraEdit.frInFiles.searchSubs=bSubDirs;
UltraEdit.frInFiles.unicodeSearch=false;
UltraEdit.frInFiles.useOutputWindow=false;
if (typeof(UltraEdit.frInFiles.openMatchingFiles) == "boolean")
{
UltraEdit.frInFiles.openMatchingFiles=false;
}
UltraEdit.frInFiles.find("");
/* If the Find In Files results window was open already the results
of the search above are appended, but the results document does
not get automatically the focus as it does if there was no results
document open from a previous search. Therefore care must be taken
that the document with the Find In Files results is the active
document after the search to continue on correct file. */
var bListCreated = false;
if (UltraEdit.activeDocument.path == sResultsDocTitle) bListCreated = true;
else
{
for (var nDocIndex = 0; nDocIndex < UltraEdit.document.length; nDocIndex++)
{
if (UltraEdit.document[nDocIndex].path == sResultsDocTitle)
{
UltraEdit.document[nDocIndex].setActive();
bListCreated = true;
break;
}
}
}
if (bListCreated == true && sSummaryInfo.length)
{
// Search for the summary info at bottom of the results.
UltraEdit.activeDocument.findReplace.searchDown=false;
UltraEdit.activeDocument.findReplace.matchCase=true;
UltraEdit.activeDocument.findReplace.matchWord=false;
UltraEdit.activeDocument.findReplace.regExp=false;
UltraEdit.activeDocument.findReplace.find(sSummaryInfo);
bListCreated = UltraEdit.activeDocument.isFound();
}
UltraEdit.activeDocument.findReplace.searchDown=true;
switch (nRegexEngine) // Restore original regular expression engine.
{
case 1: UltraEdit.unixReOn(); break;
case 2: UltraEdit.perlReOn(); break;
default: UltraEdit.ueReOn(); break;
}
/* Check now if the Find above has had success finding the last line in
the active document which should contain the Find In Files results. */
if (bListCreated == false)
{
if (nOutputType == 2)
{
UltraEdit.messageBox("There is a problem with frInFiles command or the strings of the script variables\n\"sSummaryInfo\" or \"sResultsDocTitle\" are not adapted to your version of UE/UES!","GetListOfFiles Error");
}
else if (nOutputType == 1)
{
if (UltraEdit.outputWindow.visible == false) UltraEdit.outputWindow.showWindow(true);
UltraEdit.outputWindow.write("GetListOfFiles: There is a problem with frInFiles command or the strings of the script variables");
UltraEdit.outputWindow.write(" \"sSummaryInfo\" or \"sResultsDocTitle\" are not adapted to your version of UE/UES!");
}
return false;
}
/* If last line with summary info found, delete this line. Next convert
the file into an ASCII text file for better handling of the file
names. Unicode file names are not supported by this script function.
If the file with the results is already an ASCII file from a
previous execution, there is no need for the conversion. */
if (sSummaryInfo.length) UltraEdit.activeDocument.deleteLine();
UltraEdit.activeDocument.top();
UltraEdit.activeDocument.key("RIGHT ARROW");
if (UltraEdit.activeDocument.currentPos > 1) UltraEdit.activeDocument.unicodeToASCII();
else UltraEdit.activeDocument.top();
// If top of file is also end of file, no files were found.
if (UltraEdit.activeDocument.isEof())
{
if (nOutputType == 2)
{
var sMessage = "";
switch (nFileList)
{
case 0: sMessage = "No file "+sFileType+" was found in directory\n\n"+sDirectory; break;
case 1: sMessage = "There are no opened files."; break;
case 2: sMessage = "There are no favorite files."; break;
case 3: sMessage = "There are no project files or no project is opened."; break;
case 4: sMessage = "There are no solution files or no solution is opened."; break;
}
UltraEdit.messageBox(sMessage,"GetListOfFiles Error");
}
else if (nOutputType == 1)
{
var sMessage = "";
switch (nFileList)
{
case 0: sMessage = "No file "+sFileType+" was found in directory "+sDirectory; break;
case 1: sMessage = "There are no opened files."; break;
case 2: sMessage = "There are no favorite files."; break;
case 3: sMessage = "There are no project files or no project is opened."; break;
case 4: sMessage = "There are no solution files or no solution is opened."; break;
}
if (UltraEdit.outputWindow.visible == false) UltraEdit.outputWindow.showWindow(true);
UltraEdit.outputWindow.write("GetListOfFiles: "+sMessage);
}
UltraEdit.closeFile(UltraEdit.activeDocument.path,2);
return false;
}
return true;
} // End of function GetListOfFiles
But loop replace not work properly.
Please kindly solve this script.
Thanks Samir