首页 > 解决方案 > 批处理文件 - 将文件名添加到 txt 的第一行

问题描述

我第一次使用批处理文件来满足特定需求。我有几个 txt 文件,每个文件都有很多行数据。我需要将文件名添加到每个 txt 文件的第一行。从搜索和玩耍中,我最接近的是:

@echo off 
setlocal enabledelayedexpansion
:: store modified files in subdirectory 'Processed'
md Processed
for %%a in (*.txt) do (
>temp echo %%a
copy temp+%%a Processed\"%%a"
)

当我运行它时,文件名被添加但格式被破坏(所有字符之间的空格和返回似乎被删除)。我在某处遗漏了一些简单的格式吗?

标签: batch-file

解决方案


Your for loop should look like this.

The first line writes the filename into the destination.

The second line appends the entire file with no modification into the destination.

for %%a in (*.txt) do (
 echo %%a > "Processed\%%a"
 type %%a >> "Processed\%%a"
)

推荐阅读