【问题标题】:Convert multiples .txt and into a single file?将多个 .txt 转换为单个文件?
【发布时间】:2013-07-13 21:43:32
【问题描述】:

我有很多文件夹。

在每个文件夹中大约有 1 到 20 个 .txt 文件

每个 .txt 文件(唯一名称)都包含一个标题(第 1 行),后跟一个 HTML 格式的文本(第 2 行)

示例 (1) 关于 .txt 的内部外观:

Frankfurter tail turkey doner
<p>Bacon ipsum dolor sit amet turkey sausage brisket pork.</p><p>Tongue swine turducken capicola shoulder hamburger pig.<p/><p>Ball tip jerky ham, doner <a href=""https://en.wikipedia.org/wiki/Meat"">filet mignon ham</a> hock bresaola jowl andouille pig cow</p>

示例 (2) 关于 .txt 的内部外观:

Batman
<p>You either die a hero or you live long enough to see yourself become the villain.</p>

简单解释,我想将 .txt 文件的内容合并到一个文件中,每行包含一个文件内容。

每一行也应该用引号括起来,并用逗号分隔。

从上面的示例中,输出文件应如下所示:

"Frankfurter tail turkey doner","<p>Bacon ipsum dolor sit amet turkey sausage brisket pork.</p><p>Tongue swine turducken capicola shoulder hamburger pig.<p/><p>Ball tip jerky ham, doner <a href=""https://en.wikipedia.org/wiki/Meat"">filet mignon ham</a> hock bresaola jowl andouille pig cow</p>"
"Batman","<p>You either die a hero or you live long enough to see yourself become the villain.</p>"

所以,我的问题只是如何以最简单和最快的方式完成。

我现在正在手动执行此操作,速度很慢,以这种音量复制粘贴使我的大脑膨胀。

Edit1:一直在做一些简单的研究;

  • Powershell、VBA 和 .BAT 文件看起来很像,但还没有找到任何可行的方法。
  • 我不想在代码中指定输入或输出文件的位置,解决方案的启动文件将放置在任何文件夹中,并且输出文件也应在此处生成。

试试 1#: 创建了一个包含以下内容的 Windows 批处理文件 (.bat):

for %f in (*.txt) do type "%f" >> combined.txt

放置在一个包含十几个 .txt 文件的文件夹中,但控制台只是打开和关闭。没有创建文件!

Edit2:现在我们正在做饭!

这个:

for %%f in (*.txt) do type "%%f" >> combined.txt

给出输出:

Batman
<p>You either die a hero or you live long enough to see yourself become the villain.</p>
Frankfurter tail turkey doner
<p>Bacon ipsum dolor sit amet turkey sausage brisket pork.</p><p>Tongue swine turducken capicola shoulder hamburger pig.<p/><p>Ball tip jerky ham, doner <a href=""https://en.wikipedia.org/wiki/Meat"">filet mignon ham</a> hock bresaola jowl andouille pig cow</p>

这与我想要的非常接近!

  • 现在添加引号并将换行符替换为逗号 没有解决。

最好的问候,

雷康

【问题讨论】:

    标签: powershell batch-file vbscript converter


    【解决方案1】:

    得到了外部帮助,这非常有效。

        @echo off
    cls
    setlocal
    set "combined=combined.txt"
    (
      for %%a in (*.txt) do (
        if not "%%a" == "%combined%" (
          echo %%a 1>&2
          set "firstLine=true"
          for /f "tokens=1,* delims=:   " %%b in ('findstr /n "^" %%a') do (
            if defined firstLine (
              set /p =""%%c",""
              set "firstLine="
            ) else if not "%%c" == "" (
              set /p =%%c
            )
          )
          echo "
        )
      )
    ) > %combined% <nul
    endlocal
    goto:eof
    

    【讨论】:

      猜你喜欢
      • 2018-11-05
      • 2017-11-14
      • 2020-03-20
      • 2019-01-18
      • 2022-06-07
      • 1970-01-01
      • 2018-11-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多