File 如何将源文件中的文本替换为不同的文件
所以我一直在用记事本++做一些小的清理任务,现在剩下最大的任务了 我有一个名为File 如何将源文件中的文本替换为不同的文件,file,batch-file,notepad++,File,Batch File,Notepad++,所以我一直在用记事本++做一些小的清理任务,现在剩下最大的任务了 我有一个名为Artists.txt的文件 Butta Mohamed Daler Mehndi Daljit Mattu Darshan Khela Davinder Deep Davinder Deol etc... 我有另一个文件名为Keywords.txt(位于数百个其他文件夹中)。这些文件夹的名称如下所示,它们都包含一个名为Keywords.txt的文本文件 butta-mohamed-lyrics daler-mehn
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歌词编辑关键字.txt查找_1替换(全部)为Butta Mohamed。保存更改。冲洗并重复,以便阅读Artists.txt获取下一行Daler Mehndi获取下一文件夹Daler Mehndi歌词编辑关键字.txt查找_1替换(全部)为Daler Mehndi。保存更改
想知道这样的事情是否可能?否则,我需要一周的时间才能通过复制/粘贴甚至记事本中的替换功能手动完成此操作++
我在记事本++中尝试了宏函数,但是CTRL-V而不是粘贴剪贴板中的内容。宏似乎用录制宏时使用的任何文本替换了CTRL-V函数
因此,只需添加一些额外信息…我没有安装记事本+,因为我最喜欢的文本编辑器是UltraEdit(共享软件) 虽然Stack Overflow不是一个免费的代码编写服务,我们希望提问者向我们展示一些已经为解决任务所做的编程工作,但是我很容易为这个任务编写一个小的UltraEdit脚本,因此这里有一个用于这个任务的UltraEdit脚本 脚本顶部的
C:\\Temp\\Test\\
必须替换为*歌词文件夹的父文件夹路径。UltraEdit脚本使用JavaScript核心引擎执行。因此,UltraEdit脚本中的字符串是JavaScript字符串,其中反斜杠是转义字符。因此,有必要将父文件夹路径中的每个反斜杠再转义一个反斜杠
要在UltraEdit中运行此脚本,请将Artists.txt
作为UltraEdit中的第一个文件打开
作为第二个文件,使用Ctrl+N创建一个新的ASCII文件,将下面的行复制并粘贴到此新文件中,在脚本代码中编辑父文件夹路径/名称,并使用nameKeywordsReplace.js将此脚本保存到任何文件夹中
现在,在命令运行活动脚本上单击菜单脚本,运行脚本
在脚本完成后,您可以在自动显示的输出窗口中看到有多少替换了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);
}
}
如果您不接受下载和安装UltraEdit,您必须等待提供批处理文件解决方案或记事本++宏解决方案的另一个答案,或者您自己编写必要的代码。等待,那么您是否将\u 1
替换为艺术家1
,歌曲1
或艺术家1-歌曲1
?因此我添加了更多信息,并使用了这些文件中的真实示例@Monacraft-我将用艺术家1替换_1-歌曲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.