Mofi, thank you very much for your attention and prompt response.
Your solution works very well and I'm grateful to see so fast result.
Unfortunately, yesterday, late hour, I tried to edit my first post to change some wrong requests, but I'd see no proper button/link to do so.
Because it was late, I decided to explain it more today. But you were faster and already brought a solution.
I forgot to explain that the
index.html files have to be moved to the parent folder and edited to find its resources (style sheets, pictures, etc.) inside the folders, accordingly.
To do that, you already wrote a script in the past.
But it get the folders already named correctly.
Now, the folders must to be renamed before apply the script.
Here you are:
Code: Select all
//var sParentFolderPath = ""; // A parent folder path can be defined here.
// Note: Each backslash must be escaped with an additional backslash.
var sParentFolderPath = "F:\\Meus documentos\\Downloads\\1";
// Is no parent folder path defined above?
if (!sParentFolderPath.length)
{
// If there is any file opened, get path of active file.
if (UltraEdit.document.length > 0) // Is any file opened?
{
sParentFolderPath = GetFilePath();
}
// Let script user enter the folder path if no file is opened
// in UltraEdit or the active file is a new, unsaved file.
while (!sParentFolderPath.length)
{
sParentFolderPath = UltraEdit.getString("Enter path of parent folder:",1);
}
}
// Append a backslash if parent folder path does not end already with a backslash.
if (sParentFolderPath[sParentFolderPath.length-1] != '\\')
{
sParentFolderPath += '\\';
}
// Get all index.html files with full path in specified directory tree.
if (GetListOfFiles(0,sParentFolderPath,"index.html",true))
{
UltraEdit.activeDocument.top();
UltraEdit.activeDocument.findReplace.mode=0;
UltraEdit.activeDocument.findReplace.matchCase=false;
UltraEdit.activeDocument.findReplace.matchWord=false;
UltraEdit.activeDocument.findReplace.regExp=false;
UltraEdit.activeDocument.findReplace.searchDown=true;
UltraEdit.activeDocument.findReplace.searchInColumn=false;
UltraEdit.activeDocument.findReplace.preserveCase=false;
UltraEdit.activeDocument.findReplace.replaceAll=true;
UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
UltraEdit.activeDocument.findReplace.replace(sParentFolderPath+"index.html\r\n","");
UltraEdit.activeDocument.selectAll();
if (UltraEdit.activeDocument.isSel())
{
var asFileNames = UltraEdit.activeDocument.selection.split("\r\n");
asFileNames.pop(); // Remove the empty string from end of array.
// Convert the find in files results file into a batch file for
// moving the modified index.html files up to parent folder.
// The new file name is inserted later on each line.
UltraEdit.activeDocument.top();
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.searchInColumn=false;
UltraEdit.activeDocument.findReplace.preserveCase=false;
UltraEdit.activeDocument.findReplace.replaceAll=true;
UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
UltraEdit.activeDocument.findReplace.replace("^(.+)$",'move /Y "\\1" "');
UltraEdit.activeDocument.top();
// Add a line to run later the batch file with less output to console.
UltraEdit.activeDocument.write("@echo off\r\n");
// Define the parameters for the replace in files executed later.
UltraEdit.perlReOn();
UltraEdit.frInFiles.regExp=true;
UltraEdit.frInFiles.filesToSearch=0;
UltraEdit.frInFiles.logChanges=true;
UltraEdit.frInFiles.matchWord=false;
UltraEdit.frInFiles.matchCase=false;
UltraEdit.frInFiles.searchSubs=false;
UltraEdit.frInFiles.directoryStart="";
UltraEdit.frInFiles.useEncoding=false;
UltraEdit.frInFiles.preserveCase=false;
UltraEdit.frInFiles.openMatchingFiles=false;
// Process each file name in the array.
for (var nFile = 0; nFile < asFileNames.length; nFile++)
{
// Get path of file without parent folder path and without \index.html at end.
var sFilePath = asFileNames[nFile].substring(sParentFolderPath.length,asFileNames[nFile].length-11);
// URL encode the file path with additionally encode also single straight quotes.
var sEncodedFilePath = encodeURI(sFilePath).replace(/\'/g,"%27");
sEncodedFilePath = sEncodedFilePath.replace(/&/g,"&");
// Run the Perl regular expression replace on this index.html file
// which inserts the file path on href="..." or href='...' or src="..."
// or src='...' or url("...") or url('...') or url("...")
// file references if those file references do not start with "#",
// or "javascript:" or contain already a reference with / in string.
UltraEdit.frInFiles.searchInFilesTypes=asFileNames[nFile];
UltraEdit.frInFiles.replace("(href=[\"']|src=[\"']|url\\([\"']|url\\(")(?!#|javascript:)((?:(?![\"']|")[^/])+(?=[\"']|"))","\\1"+sEncodedFilePath+"/\\2");
// Remove the string "_files" at end of file path for new file name
// if this string is at end of file path at all. Then insert new file
// name with path into already reformatted find in files results file.
var sNewFileName = sFilePath.replace(/_arquivos$/,"") + ".html";
UltraEdit.activeDocument.key("END");
UltraEdit.activeDocument.write(sParentFolderPath+sNewFileName+'"');
UltraEdit.activeDocument.key("DOWN ARROW");
}
// Append a line so that the batch file deletes itself on execution.
// UltraEdit.activeDocument.write('del "%~f0" & exit\r\n');
// Convert the file from ANSI to OEM to work also for folder paths
// not consisting of only ASCII characters.
UltraEdit.activeDocument.ansiToOem();
// Save the reformatted find in files results file as batch file in parent folder.
UltraEdit.saveAs(sParentFolderPath+"MoveHtmlFiles.bat");
// Run the batch file via a user tool.
UltraEdit.runTool("Run Active File");
// Close the batch file which has deleted already itself.
UltraEdit.closeFile(UltraEdit.activeDocument.path,2);
}
else
{
// Close the empty results file because of nothing to do.
UltraEdit.closeFile(UltraEdit.activeDocument.path,2);
UltraEdit.messageBox("No index.html file found in a subdirectory of "+sParentFolderPath);
}
}
function GetFilePath (CompleteFileNameOrDocIndexNumber)
{
var sFilePath = "";
var sFullFileName = "";
var nLastDirDelim = -1;
var nOutputType = (typeof(g_nDebugMessage) == "number") ? g_nDebugMessage : 0;
if (typeof(CompleteFileNameOrDocIndexNumber) == "string")
{
sFullFileName = CompleteFileNameOrDocIndexNumber;
nLastDirDelim = sFullFileName.lastIndexOf("|");
if (nLastDirDelim < 0)
{
var nLastSlash = sFullFileName.lastIndexOf("/");
var nLastBackSlash = sFullFileName.lastIndexOf("\\");
nLastDirDelim = (nLastBackSlash > nLastSlash) ? nLastBackSlash : nLastSlash;
}
if (nLastDirDelim < 0)
{
if (nOutputType == 2)
{
UltraEdit.messageBox("File path can't be determined from \""+sFullFileName+"\"!","GetFilePath Error");
}
else if (nOutputType == 1)
{
if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
UltraEdit.outputWindow.write("GetFilePath: File path can't be determined from \""+sFullFileName+"\"!");
}
return sFilePath;
}
}
else
{
if (UltraEdit.document.length < 1)
{
if (nOutputType == 2)
{
UltraEdit.messageBox("No document is open currently!","GetFilePath Error");
}
else if (nOutputType == 1)
{
if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
UltraEdit.outputWindow.write("GetFilePath: No document is open currently!");
}
return sFilePath;
}
var nDocumentNumber = -1;
if (typeof(CompleteFileNameOrDocIndexNumber) == "number")
{
nDocumentNumber = CompleteFileNameOrDocIndexNumber;
}
if (nDocumentNumber >= UltraEdit.document.length)
{
if (nOutputType == 2)
{
UltraEdit.messageBox("A document with index number "+nDocumentNumber+" does not exist!","GetFilePath Error");
}
else if (nOutputType == 1)
{
if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
UltraEdit.outputWindow.write("GetFilePath: A document with index number "+nDocumentNumber+" does not exist!");
}
return sFilePath;
}
if (nDocumentNumber < 0)
{
sFullFileName = UltraEdit.activeDocument.path;
if (UltraEdit.activeDocument.isFTP())
{
nLastDirDelim = sFullFileName.lastIndexOf("|");
if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("/");
}
}
else
{
sFullFileName = UltraEdit.document[nDocumentNumber].path;
if (UltraEdit.document[nDocumentNumber].isFTP())
{
nLastDirDelim = sFullFileName.lastIndexOf("|");
if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("/");
}
}
if (nLastDirDelim < 0) nLastDirDelim = sFullFileName.lastIndexOf("\\");
if (nLastDirDelim < 0)
{
if (nOutputType == 2)
{
UltraEdit.messageBox("File path can't be determined from a new file!","GetFilePath Error");
}
else if (nOutputType == 1)
{
if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
UltraEdit.outputWindow.write("GetFilePath: File path can't be determined from a new file!");
}
return sFilePath;
}
}
if (sFullFileName.charAt(nLastDirDelim) != '|')
{
nLastDirDelim++;
sFilePath = sFullFileName.substring(0,nLastDirDelim);
}
else
{
sFilePath = sFullFileName.substring(0,nLastDirDelim) + "/";
}
return sFilePath;
}
function GetListOfFiles (nFileList, sDirectory, sFileType, bSubDirs)
{
var sSummaryInfo = "Search complete. Found";
var sResultsDocTitle = "** Find Results ** ";
var bNoUnicode = true;
var nOutputType = (typeof(g_nDebugMessage) == "number") ? g_nDebugMessage : 2;
if ((nOutputType < 1) || (nOutputType > 2)) nOutputType = 0;
if (typeof(nFileList) != "number" || nFileList < 0 || nFileList > 4) nFileList = 0;
if (!nFileList)
{
if ((typeof(sDirectory) != "string") || (!sDirectory.length)) sDirectory = ".\\";
else if (sDirectory[sDirectory.length-1] != "\\") sDirectory += "\\";
if ((typeof(sFileType) != "string") || (!sFileType.length)) sFileType = "*";
if (typeof(bSubDirs) != "boolean") bSubDirs = false;
}
else
{
sDirectory = "";
sFileType = "";
bSubDirs = false;
}
var nRegexEngine = UltraEdit.regexMode;
UltraEdit.ueReOn();
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("");
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 && sSummaryInfo.length)
{
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)
{
case 1: UltraEdit.unixReOn(); break;
case 2: UltraEdit.perlReOn(); break;
default: UltraEdit.ueReOn(); break;
}
if (!bListCreated)
{
if (nOutputType == 2)
{
UltraEdit.messageBox('There is a problem with command frInFiles or the strings of the two script variables "sSummaryInfo" and "sResultsDocTitle" are not adapted to used version of UltraEdit/UEStudio!',"GetListOfFiles Error");
}
else if (nOutputType == 1)
{
if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
UltraEdit.outputWindow.write('GetListOfFiles: There is a problem with command frInFiles or the strings of the two script variables');
UltraEdit.outputWindow.write(' "sSummaryInfo" and "sResultsDocTitle" are not adapted to used version of UltraEdit/UEStudio!');
}
return false;
}
if (sSummaryInfo.length) UltraEdit.activeDocument.deleteLine();
UltraEdit.activeDocument.top();
if (bNoUnicode)
{
UltraEdit.activeDocument.key("RIGHT ARROW");
if (UltraEdit.activeDocument.currentPos > 1) UltraEdit.activeDocument.unicodeToASCII();
else UltraEdit.activeDocument.top();
}
if (UltraEdit.activeDocument.isEof())
{
if (nOutputType > 0)
{
var sMessage;
switch (nFileList)
{
case 0: sMessage = "No file " + sFileType + " was found in directory" +
((nOutputType == 2) ? "\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;
default: sMessage = ""; break;
}
if (nOutputType == 1)
{
if (!UltraEdit.outputWindow.visible) UltraEdit.outputWindow.showWindow(true);
UltraEdit.outputWindow.write("GetListOfFiles: "+sMessage);
}
else UltraEdit.messageBox(sMessage,"GetListOfFiles Error");
}
UltraEdit.closeFile(UltraEdit.activeDocument.path,2);
return false;
}
return true;
}
Now, how to integrate the above script with the new one that you wrote today?
The tree I'm looking for is like this:
Code: Select all
C:\Tests\
| Renamed 1.html
| Renamed 2.html
| Renamed 3.html
|
+---Renamed 1
| 09e21e0c407686fada8f091959db2a2afe67b008.png
| 147.jpg
| 304.png
| 49.gif
| 924d9a6e764d2010a92fc86415a214ce6317ca41.gif
| b0ac20351839be9c93a1d1d58bdd23fc5a2c205a.svg
| c0bf984b0de3861cebf7b94997a41b82d6421727.svg
|
+---Renamed 2
| FIc37VKXEAYs_fv.jpeg
| FIc51dRWQAMXnC1.jpeg
| font-awesome.min.css
| fontawesome-webfont-1.eot
| fontawesome-webfont.eot
| fontawesome-webfont.svg
|
\---Renamed 3
fontawesome-webfont.ttf
fontawesome-webfont.woff
fontawesome-webfont.woff2
hqdefault-1.jpg
hqdefault-2.jpg
hqdefault-3.jpg