如何将源文件中的文本替换为不同的文件
How to replace text from a source file into different files
所以我一直在使用 Notepad++ 做一些小的清理任务,现在剩下的是最大的任务..
我有一个名为 Artists.txt
的文件,看起来像
Butta Mohamed
Daler Mehndi
Daljit Mattu
Darshan Khela
Davinder Deep
Davinder Deol
etc...
我有另一个名为 Keywords.txt
的文件(位于其他数百个文件夹中)。这些文件夹的命名如下,它们都包含一个名为 Keywords.txt
的文本文件
butta-mohamed-lyrics
daler-mehndi-lyrics
daljit-mattu-lyrics
darshan-khela-lyrics
davinder-deep-lyrics
davinder-deol-lyrics
Keywords.txt 包含文本 _1(Keywords.txt 中的多个实例)。
我想做的是从 Artists.txt
中获取每一行并替换 _1。这些文件夹的顺序与 Artists.txt 相同。
所以阅读 Artists.txt 获取第一行 Butta Mohamed 获取第一个文件夹 butta-mohamed-lyrics 编辑 Keywords.txt 查找 _1 将(全部)替换为 Butta Mohamed。保存更改。冲洗并重复阅读 Artists.txt 获取下一行 Daler Mehndi 获取下一个文件夹 daler-mehndi-lyrics 编辑 Keywords.txt 查找 _1 将(全部)替换为 Daler Mehndi。保存更改。
想知道这样的事情是否可能?否则我需要一周的时间才能通过 copy/pasting 甚至 Notepad++
中的替换功能手动执行此操作
我试过 Notepad++ 中的宏功能,但 CTRL-V 而不是将内容粘贴到剪贴板中,宏似乎用宏记录时所用的任何文本替换了 CTRL-V 功能。
所以只需添加一些额外的信息...
我没有安装 Notepad++,因为我最喜欢的文本编辑器是 UltraEdit(共享软件)。
虽然 Stack Overflow 不是免费的代码编写服务,我们希望提问者向我们展示一些已经为解决任务所做的编程工作,但我很容易为这个任务编写小的 UltraEdit 脚本,因此这是此任务的 UltraEdit 脚本。
脚本顶部的 C:\Temp\Test\
必须替换为 *lyrics 文件夹的父文件夹路径。 UltraEdit 脚本使用 JavaScript 核心引擎执行。因此,UltraEdit 脚本中的字符串是 JavaScript 字符串,其中反斜杠是转义字符。因此有必要将父文件夹路径中的每个反斜杠再转义一个反斜杠。
要在 UltraEdit 中 运行 此脚本,在 UltraEdit 中将 Artists.txt
作为第一个文件打开。
作为第二个文件,使用 Ctrl+N 创建一个新的 ASCII 文件,将下面的行复制并粘贴到这个新文件中,在脚本代码中编辑父文件夹 path/name 并保存此脚本,例如名称为 KeywordsReplace.js
到任何文件夹。
现在 运行 通过在菜单 Scripting 中单击命令 运行 Active Script.
您可以在脚本完成后看到自动显示输出 window 在 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
。输出结果 window 为:
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++宏解决方案,或者你自己编写必要的代码。
所以我一直在使用 Notepad++ 做一些小的清理任务,现在剩下的是最大的任务..
我有一个名为 Artists.txt
的文件,看起来像
Butta Mohamed
Daler Mehndi
Daljit Mattu
Darshan Khela
Davinder Deep
Davinder Deol
etc...
我有另一个名为 Keywords.txt
的文件(位于其他数百个文件夹中)。这些文件夹的命名如下,它们都包含一个名为 Keywords.txt
butta-mohamed-lyrics
daler-mehndi-lyrics
daljit-mattu-lyrics
darshan-khela-lyrics
davinder-deep-lyrics
davinder-deol-lyrics
Keywords.txt 包含文本 _1(Keywords.txt 中的多个实例)。
我想做的是从 Artists.txt
中获取每一行并替换 _1。这些文件夹的顺序与 Artists.txt 相同。
所以阅读 Artists.txt 获取第一行 Butta Mohamed 获取第一个文件夹 butta-mohamed-lyrics 编辑 Keywords.txt 查找 _1 将(全部)替换为 Butta Mohamed。保存更改。冲洗并重复阅读 Artists.txt 获取下一行 Daler Mehndi 获取下一个文件夹 daler-mehndi-lyrics 编辑 Keywords.txt 查找 _1 将(全部)替换为 Daler Mehndi。保存更改。
想知道这样的事情是否可能?否则我需要一周的时间才能通过 copy/pasting 甚至 Notepad++
中的替换功能手动执行此操作我试过 Notepad++ 中的宏功能,但 CTRL-V 而不是将内容粘贴到剪贴板中,宏似乎用宏记录时所用的任何文本替换了 CTRL-V 功能。
所以只需添加一些额外的信息...
我没有安装 Notepad++,因为我最喜欢的文本编辑器是 UltraEdit(共享软件)。
虽然 Stack Overflow 不是免费的代码编写服务,我们希望提问者向我们展示一些已经为解决任务所做的编程工作,但我很容易为这个任务编写小的 UltraEdit 脚本,因此这是此任务的 UltraEdit 脚本。
脚本顶部的C:\Temp\Test\
必须替换为 *lyrics 文件夹的父文件夹路径。 UltraEdit 脚本使用 JavaScript 核心引擎执行。因此,UltraEdit 脚本中的字符串是 JavaScript 字符串,其中反斜杠是转义字符。因此有必要将父文件夹路径中的每个反斜杠再转义一个反斜杠。
要在 UltraEdit 中 运行 此脚本,在 UltraEdit 中将 Artists.txt
作为第一个文件打开。
作为第二个文件,使用 Ctrl+N 创建一个新的 ASCII 文件,将下面的行复制并粘贴到这个新文件中,在脚本代码中编辑父文件夹 path/name 并保存此脚本,例如名称为 KeywordsReplace.js
到任何文件夹。
现在 运行 通过在菜单 Scripting 中单击命令 运行 Active Script.
您可以在脚本完成后看到自动显示输出 window 在 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
。输出结果 window 为:
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++宏解决方案,或者你自己编写必要的代码。