【问题标题】:How to replace text from a source file into different files如何将源文件中的文本替换为不同的文件
【发布时间】:2015-04-23 18:17:27
【问题描述】:

所以我一直在使用 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 替换(全部)。保存更改。

想知道这样的事情是否可行?否则我需要一周的时间通过复制/粘贴甚至 Notepad++

中的替换功能手动完成此操作

我已经尝试过 Notepad++ 中的宏功能,但使用 CTRL-V 而不是粘贴剪贴板中的内容,宏似乎用宏记录的任何文本替换了 CTRL-V 功能。

所以只是添加一些额外的信息...

【问题讨论】:

  • 等等,你会用Artist 1Song 1Artist 1 - Song 1替换_1吗?
  • 所以我添加了更多信息并使用了这些文件中的真实示例。 @Monacraft - 我会将 _1 替换为 Artist 1 - Song 1 我在原始帖子中添加了更多带有真实数据的信息,希望对您有所帮助。

标签: file batch-file notepad++


【解决方案1】:

我没有安装 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++ 宏解决方案,或者您自己编写必要的代码。

【讨论】:

  • 谢谢,就像一个魅力 :) 我将尝试编辑代码以适应我拥有的其他情况,其中文件夹名称还包括歌曲名称。与我需要一周的时间相比,上面的代码在几秒钟内完成了更改。非常感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2014-02-07
  • 2020-02-01
  • 2014-11-18
  • 2022-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-04
相关资源
最近更新 更多