Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops .bat文件循环遍历文件夹并附加文本文件_Loops_Batch File_Append_Command Prompt_Windows Xp Sp3 - Fatal编程技术网

Loops .bat文件循环遍历文件夹并附加文本文件

Loops .bat文件循环遍历文件夹并附加文本文件,loops,batch-file,append,command-prompt,windows-xp-sp3,Loops,Batch File,Append,Command Prompt,Windows Xp Sp3,我想设置一个简单的批处理文件,它将循环遍历文件夹(批处理文件所在的文件夹)中的所有.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 t

我想设置一个简单的批处理文件,它将循环遍历文件夹(批处理文件所在的文件夹)中的所有.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批处理没有本地命令来就地编辑文件(除了向其追加数据)。因此,对于每个文件,您需要创建一个包含所需内容的临时文件,然后删除原始文件并将临时文件重命名为原始文件。删除和重命名可以通过单个移动命令完成

@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
)