2012-04-13 118 views
1

我想設置一個簡單的批處理文件,它將遍歷文件夾(放置批處理文件的文件夾)中的所有.txt文件,並添加相同的標題行到這些文件中的每一個。標題行在單獨的文本文件中定義。.bat文件循環瀏覽文件夾並追加文本文件

因此,舉例來說,假設我有:

c:\SomeFolder\Headings.txt 
    --> I want to add this to the top of each of the text files in: 

c:\SomeFolder\FolderWithTextFiles\ 
    --> ...by running the batch file: 

c:\SomeFolder\FolderWithTextFiles\BatchFile.batch 

附加說明:
- 無需遍歷子文件夾

回答

2

Windows批處理不具有本機命令編輯到位文件(除了向其添加數據)。因此,對於每個文件,您需要創建一個包含所需內容的臨時文件,然後刪除原始文件並將其重命名爲原始文件。刪除和重命名可以用一個MOVE命令完成。

@echo off 
set "header=c:\SomeFolder\Headings.txt" 
set "folder=c:\SomeFolder\FolderWithTextFiles" 
set "tempFile=%folder%\temp.txt" 
for %%F in ("%folder%\*.txt") do (
    type "%header%" >"%tempFile%" 
    type "%%F" >>"%tempFile%" 
    move /y "%tempFile%" "%%F" >nul 
)