我想设置一个简单的批处理文件,该文件将循环遍历文件夹(放置批处理文件的文件夹)中的所有 .txt 文件,并向每个文件添加相同的标题行。标题行在单独的文本文件中定义。
例如,假设我有:
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
补充说明:
- 无需循环子文件夹
最佳答案
Windows 批处理没有本地命令来编辑文件(除了向文件 append 数据之外)。因此,对于每个文件,您需要创建一个包含所需内容的临时文件,然后删除原始文件并将临时文件重命名为原始文件。删除和重命名可以通过单个 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
)
关于loops - .bat 文件循环遍历文件夹并 append 文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10137402/