我没有安装 Notepad++,因为我最喜欢的文本编辑器是 UltraEdit(共享软件)。
虽然 Stack Overflow 不是免费的代码编写服务,我们希望提问者向我们展示已经为解决任务所做的一些编程工作,但我很容易为这个任务编写小 UltraEdit 脚本,因此这里是用于此任务的 UltraEdit 脚本。
脚本顶部的C:\\Temp\\Test\\ 必须替换为 *lyrics 文件夹的父文件夹路径。 UltraEdit 脚本使用 JavaScript 核心引擎执行。因此 UltraEdit 脚本中的字符串是 JavaScript 字符串,其中反斜杠是转义字符。因此需要将父文件夹路径中的每个反斜杠再转义一个反斜杠。
要在 UltraEdit 中运行此脚本,请将 Artists.txt 作为 UltraEdit 中的第一个文件打开。
作为第二个文件,使用 Ctrl+N 创建一个新的 ASCII 文件,将下面的行复制并粘贴到这个新文件中,在脚本代码中编辑父文件夹路径/名称,并将此脚本保存为例如名称 KeywordsReplace.js 到任何文件夹。
现在通过点击菜单Scripting上的命令Run Active Script来运行脚本。
脚本完成后,您可以在自动显示的输出窗口中看到Keywords.txt 文件中的替换次数。
if (UltraEdit.document.length > 0) // Is any file opened?
{
// Parent folder containing all the *lyrics folders.
var sParentFolder = "C:\\Temp\\Test\\";
// Define environment for this script.
UltraEdit.insertMode();
UltraEdit.columnModeOff();
// Select everything in first file.
UltraEdit.document[0].selectAll();
// Is first file not an empty file?
if (UltraEdit.document[0].isSel())
{
// Determine line terminator type for first file.
var sLineTerm = "\r\n";
if (UltraEdit.document[0].lineTerminator == 1) sLineTerm = "\n"
else if (UltraEdit.document[0].lineTerminator == 2) sLineTerm = "\r"
// Get all lines of first file into an array of strings
var asArtists = UltraEdit.document[0].selection.split(sLineTerm);
// Remove last string if it is empty because file ended with
// a line termination.
if (!asArtists[asArtists.length-1].length) asArtists.pop();
// Define once the parameters for all the replace in files executed
// below in the loop with changing directory and replace strings.
UltraEdit.frInFiles.filesToSearch=0;
UltraEdit.frInFiles.searchSubs=false;
UltraEdit.frInFiles.ignoreHiddenSubs=false;
UltraEdit.frInFiles.openMatchingFiles=false;
UltraEdit.frInFiles.searchInFilesTypes="Keywords.txt";
UltraEdit.frInFiles.regExp=false;
UltraEdit.frInFiles.matchCase=true;
UltraEdit.frInFiles.matchWord=false;
UltraEdit.frInFiles.logChanges=true;
UltraEdit.frInFiles.useEncoding=false;
UltraEdit.frInFiles.preserveCase=false;
// Run for each artist a replace of all occurrences of _1
// in the artists lyrics folder by name of the artist.
for (nArtist = 0; nArtist < asArtists.length; nArtist++)
{
// Build folder name by converting artists name to
// lower case and replacing all spaces by hyphens.
var sFolder = asArtists[nArtist].toLowerCase().replace(/ /g,"-");
// Define directory for replace in files by appending
// additionally the string "-lyrics" to folder name.
UltraEdit.frInFiles.directoryStart = sParentFolder + sFolder + "-lyrics\\";
UltraEdit.frInFiles.replace("_1",asArtists[nArtist]);
}
// The output window contains the summary information
// about the replaces made and therefore open it.
UltraEdit.outputWindow.showWindow(true);
}
}
使用提供的数据对脚本进行了测试,每个 Keywords.txt 在 6 个 *lyrics 文件夹中正好包含 3 次 _1。输出窗口的结果是:
Running script: C:\Temp\KeywordsReplace.js
============================================================
C:\Temp\Test\butta-mohamed-lyrics\Keywords.txt, 3
3 items replaced in 1 files.
C:\Temp\Test\daler-mehndi-lyrics\Keywords.txt, 3
3 items replaced in 1 files.
C:\Temp\Test\daljit-mattu-lyrics\Keywords.txt, 3
3 items replaced in 1 files.
C:\Temp\Test\darshan-khela-lyrics\Keywords.txt, 3
3 items replaced in 1 files.
C:\Temp\Test\davinder-deep-lyrics\Keywords.txt, 3
3 items replaced in 1 files.
C:\Temp\Test\davinder-deol-lyrics\Keywords.txt, 3
3 items replaced in 1 files.
Script succeeded.
如果您无法下载和安装 UltraEdit,您必须等待另一个答案提供批处理文件解决方案或 Notepad++ 宏解决方案,或者您自己编写必要的代码。