将多个文本文件 _+ 文件名_ 组合成一个文本文件

Bra*_*nor 2 windows filenames org-mode

我想结合一些文本文件,但带有标题(编辑:文件名)。理想情况下,类似

* a filename 
contents of file
... 
* another filename 
contents of file 
... 

etc... 
Run Code Online (Sandbox Code Playgroud)

我使用的是 Windows(不是 DOS),但可以访问 powershell、pandoc、emacs、cygwin 或您推荐的任何其他工具。(显然我是一个尝试组织模式的新手。)

我可以轻松地将它们全部放在一个文件夹中。但我想避免输入每个文件的名称。如果推荐一个bat文件,我没用过,但愿意学习。

Mit*_*tch 5

我相信有更聪明的东西,但这里有一个 powershell 脚本将组合所有文件:

$files = (dir *.txt)
$outfile = "out.txt"

$files | %{
    $_.FullName | Add-Content $outfile
    Get-Content $_.FullName | Add-Content $outfile
}
Run Code Online (Sandbox Code Playgroud)

它有效率吗?不是很糟糕......但它会在紧要关头工作。

  • 您可以使用 `-f` 操作符任意格式化内容。有关详细信息,请参阅 http://blogs.technet.com/b/heyscriptingguy/archive/2013/03/12/use-powershell-to-format-strings-with-composite-formatting.aspx。要使文件名行成为 `** filename`,您可以指定 `("** {0}" -f $_.Name) | 添加内容 $outfile` (2认同)