將多個文字檔案 _+ 檔案名稱_ 合併為一個文字文件

將多個文字檔案 _+ 檔案名稱_ 合併為一個文字文件

我想合併一些文字文件,但帶有標題(編輯:文件名)。理想情況下,類似

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

etc... 

我使用的是 Windows(不是 DOS),但可以存取 powershell、pandoc、emacs、cygwin 或您推薦的任何其他工具。 (顯然我是一個嘗試組織模式的新手。)

我可以輕鬆地將它們全部放在一個資料夾中。但我想避免輸入每個文件的名稱。如果推薦一個bat文件,我沒用過,但願意學習。

答案1

我確信有更聰明的東西,但這裡有一個 powershell 腳本將合併所有文件:

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

$files | %{
    $_.FullName | Add-Content $outfile
    Get-Content $_.FullName | Add-Content $outfile
}

有效率嗎?不是很嚴重……但在緊要關頭它會起作用。

答案2

受 Mitch 腳本結構的啟發,我為基於 Unix 的環境(例如 GNU/Linux 和 OS X)編寫了一個版本:

find -regex '.*\.\(docx?\|org\|rtf\|te?xt\)$' | while read file
do
    echo "* $file" >> target-file.org
    cat "$file" | pandoc -t org >> target-file.org
done

(如果您不想安裝pandoc,只需刪除管道和命令,即可| pandoc -t org。)

該腳本將查找當前目錄及其子目錄中具有所描述的檔案副檔名(.docx等)的所有檔案。

例如,如果清單包含fileA.textfileB.rtfin subdirectory subd/targetfile.org將收到如下行:

* ./subd/fileA.text
<fileA's contents converted to an org file by pandoc>
* ./subd/fileB.rtf
<fileB's contents converted to an org file by pandoc>

我認為這將為target-file.org在 Emacs 內進行改進留下一個非常好的狀態,而腳本不會太複雜。 (特別是如果您包含該pandoc步驟。)

相關內容